TagManager Service Stay organized with collections Save and categorize content based on your preferences.
Page Summary
The Google Tag Manager service allows authorized users to manage various Tag Manager data including accounts, containers, environments, and more via the Tag Manager API.
This is an advanced service that requires specific enabling before it can be used.
The Tag Manager service in Apps Script uses the same objects, methods, and parameters as the public API.
Sample code is provided to demonstrate how to create and manage Tag Manager components like container versions, environments, and user permissions.
The Google Tag Manager service provides access toTag ManagerAPI data for an authorized user. This serviceallows Tag Manager users to manage Tag Manageraccounts,containers,environments,versions,workspaces,folders,variables,triggers,tags, anduser permissions.
Note: This is an advanced service that must beenabled beforeuse.Reference
For detailed information on this service, see the reference documentation fortheTag Manager API V2.
Like all advanced services in Apps Script, the Tag Manager service uses the sameobjects, methods, and parameters as the public API. For more information, seeHow method signatures are determined.
To report issues and find other support, see theGoogle Tag Manager help center.
Sample code
The sample code below demonstrates how to use a few features of the Tag Managerservice.
Creates a container version with a variable, trigger, and tag.
The sample code below usesTag Manager API V2 tofirst create a container with a name that is timestamped with the current dateto improve the chances of it being unique. The sample then creates a workspacewith a random value variable, and a trigger that fires for any page view. Next,the sample uses the trigger to create an arbitrary pixel tag that fires a pixelto//example.com with a cache buster appended to the end of the URL. Last, thesample creates a container version with the above entities, logs the version andreturns it for later use.
/** * Creates a container version for a particular account * with the input accountPath. * @param {string} accountPath The account path. * @return {string} The tag manager container version. */functioncreateContainerVersion(accountPath){constdate=newDate();// Creates a container in the account, using the current timestamp to make// sure the container is unique.try{constcontainer=TagManager.Accounts.Containers.create({name:`appscript tagmanager container${date.getTime()}`,usageContext:["WEB"],},accountPath,);constcontainerPath=container.path;// Creates a workspace in the container to track entity changes.constworkspace=TagManager.Accounts.Containers.Workspaces.create({name:"appscript workspace",description:"appscript workspace"},containerPath,);constworkspacePath=workspace.path;// Creates a random value variable.constvariable=TagManager.Accounts.Containers.Workspaces.Variables.create({name:"apps script variable",type:"r"},workspacePath,);// Creates a trigger that fires on any page view.consttrigger=TagManager.Accounts.Containers.Workspaces.Triggers.create({name:"apps script trigger",type:"PAGEVIEW"},workspacePath,);// Creates a arbitary pixel that fires the tag on all page views.consttag=TagManager.Accounts.Containers.Workspaces.Tags.create({name:"apps script tag",type:"img",liveOnly:false,parameter:[{type:"boolean",key:"useCacheBuster",value:"true"},{type:"template",key:"cacheBusterQueryParam",value:"gtmcb",},{type:"template",key:"url",value:"//example.com"},],firingTriggerId:[trigger.triggerId],},workspacePath,);// Creates a container version with the variabe, trigger, and tag.constversion=TagManager.Accounts.Containers.Workspaces.create_version({name:"apps script version"},workspacePath,).containerVersion;console.log(version);returnversion;}catch(e){// TODO (Developer) - Handle exceptionconsole.log("Failed with error: %s",e.error);}}
Publishes a container version and quick previews the current container draft.
The sample code below usesTag Manager API V2to accept a container version that may have been created in the above example,and retrieve the account, container, and version ids from the version. Thesample utilizes these ids to publish a container version live to the world.Last, the sample creates a quick preview of a new workspace and logs the quickpreview.
/** * Publishes a container version publically to the world and creates a quick * preview of the current container draft. * @param {object} version The container version. */functionpublishVersionAndQuickPreviewDraft(version){try{constpathParts=version.path.split("/");constcontainerPath=pathParts.slice(0,4).join("/");// Publish the input container version.TagManager.Accounts.Containers.Versions.publish(version.path);constworkspace=TagManager.Accounts.Containers.Workspaces.create({name:"appscript workspace",description:"appscript workspace"},containerPath,);constworkspaceId=workspace.path;// Quick previews the current container draft.constquickPreview=TagManager.Accounts.Containers.Workspaces.quick_preview(workspace.path);console.log(quickPreview);}catch(e){// TODO (Developer) - Handle exceptionsconsole.log("Failed with error: $s",e.error);}}
Creates and reauthorizes a user environment.
The sample code below usesTag Manager API V2to accept a container version and extract the account, container, and versionids. The sample utilizes these ids to create a user environment that points tothe input container version and logs the user environment. The sample concludesby logging a reauthorized user environment.
/** * Creates and reauthorizes a user environment in a container that points * to a container version passed in as an argument. * @param {object} version The container version object. */functioncreateAndReauthorizeUserEnvironment(version){try{// Creates a container version.constpathParts=version.path.split("/");constcontainerPath=pathParts.slice(0,4).join("/");// Creates a user environment that points to a container version.constenvironment=TagManager.Accounts.Containers.Environments.create({name:"test_environment",type:"user",containerVersionId:version.containerVersionId,},containerPath,);console.log(`Original user environment:${environment}`);// Reauthorizes the user environment that points to a container version.TagManager.Accounts.Containers.Environments.reauthorize({},environment.path,);console.log(`Reauthorized user environment:${environment}`);}catch(e){// TODO (Developer) - Handle exceptionsconsole.log("Failed with error: $s",e.error);}}
Logs all emails and container access permissions within an account.
The sample code below usesTag Manager API V2to find a list of all permissions within a Tag Manager account. The sample thenlogs the user's email address, the container id, and the types of containeraccess permissions for each entry.
/** * Logs all emails and container access permission within an account. * @param {string} accountPath The account path. */functionlogAllAccountUserPermissionsWithContainerAccess(accountPath){try{constuserPermissions=TagManager.Accounts.User_permissions.list(accountPath).userPermission;for(leti=0;i <userPermissions.length;i++){constuserPermission=userPermissions[i];if("emailAddress"inuserPermission){constcontainerAccesses=userPermission.containerAccess;for(letj=0;j <containerAccesses.length;j++){constcontainerAccess=containerAccesses[j];console.log(`emailAddress:${userPermission.emailAddress} containerId:${containerAccess.containerId} containerAccess:${containerAccess.permission}`,);}}}}catch(e){// TODO (Developer) - Handle exceptionsconsole.log("Failed with error: $s",e.error);}}
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.