/** * 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':'appscripttagmanagercontainer'+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':'appscriptworkspace','description':'appscriptworkspace'},containerPath);constworkspacePath=workspace.path;// Creates a random value variable.constvariable=TagManager.Accounts.Containers.Workspaces.Variables.create({'name':'appsscriptvariable','type':'r'},workspacePath);// Creates a trigger that fires on any page view.consttrigger=TagManager.Accounts.Containers.Workspaces.Triggers.create({'name':'appsscripttrigger','type':'PAGEVIEW'},workspacePath);// Creates a arbitary pixel that fires the tag on all page views.consttag=TagManager.Accounts.Containers.Workspaces.Tags.create({'name':'appsscripttag','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':'appsscriptversion'},workspacePath).containerVersion;console.log(version);returnversion;}catch(e){// TODO (Developer) - Handle exceptionconsole.log('Failedwitherror:%s',e.error);}}
发布容器版本并快速预览当前容器草案。
以下示例代码使用 Tag Manager API V2 接受了上述示例代码中创建的容器版本,并从该版本中检索账号、容器和版本 ID。然后,利用这些 ID 实时发布容器版本。最后,创建并记录新工作区的快速预览版本。
/** * Retrieves the container path from a container version path. * @param {string} versionPath The version path. * @return {string} The container path. */functiongrabContainerPath(versionPath){constpathParts=versionPath.split('/');returnpathParts.slice(0,4).join('/');}/** * 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{constcontainerPath=grabContainerPath(version.path);// Publish the input container version.TagManager.Accounts.Containers.Versions.publish(version.path);constworkspace=TagManager.Accounts.Containers.Workspaces.create({'name':'appscriptworkspace','description':'appscriptworkspace'},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('Failedwitherror:$s',e.error);}}
创建并重新授权用户环境。
以下示例代码使用 Tag Manager API V2 接受容器版本,并提取账号、容器和版本 ID。然后,利用这些 ID 创建指向相关输入容器版本的用户环境,并予以记录该用户环境。最后,记录已重新授权的用户环境。
/** * Retrieves the container path from a container version path. * @param {string} versionPath The version path. * @return {string} The container path. */functiongrabContainerPath(versionPath){constpathParts=versionPath.split('/');returnpathParts.slice(0,4).join('/');}/** * 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.constcontainerPath=grabContainerPath(version.path);// 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('Originaluserenvironment:'+environment);// Reauthorizes the user environment that points to a container version.TagManager.Accounts.Containers.Environments.reauthorize({},environment.path);console.log('Reauthorizeduserenvironment:'+environment);}catch(e){// TODO (Developer) - Handle exceptionsconsole.log('Failedwitherror:$s',e.error);}}
记录账号中的所有电子邮件和容器访问权限。
以下示例代码使用 Tag Manager API V2 查找跟踪代码管理器账号中所有权限的列表。然后,记录用户的电子邮件地址、容器 ID 以及每个条目的容器访问权限类型。
/** * 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('Failedwitherror:$s',e.error);}}
[null,null,["最后更新时间 (UTC):2024-12-21。"],[[["The Google Tag Manager service allows authorized users to manage various aspects of their Tag Manager accounts programmatically via the Tag Manager API."],["This advanced service enables users to create, update, and manage accounts, containers, workspaces, tags, triggers, variables, and user permissions."],["Developers can use Apps Script to interact with the Tag Manager API, leveraging its objects, methods, and parameters for automating Tag Manager tasks."],["Sample code is provided to demonstrate how to create and publish container versions, manage environments, and control user permissions."],["For detailed information and support, refer to the Tag Manager API V2 reference documentation and the Google Tag Manager help center."]]],[]]