Google 代碼管理工具服務可讓授權使用者存取 Tag Manager API 資料。這項服務可讓代碼管理工具使用者管理代碼管理工具帳戶、containers、環境、versions、工作區、資料夾、變數、觸發條件、使用者權限和使用者權限。
參考資料
如要進一步瞭解這項服務,請參閱 Tag Manager API 第 2 版的參考說明文件。
如同 Apps Script 的所有進階服務,代碼管理工具服務會使用與公用 API 相同的物件、方法和參數。詳情請參閱「如何判定方法簽章」一文。
如要回報問題及尋找其他支援,請參閱 Google 代碼管理工具說明中心。
程式碼範例
以下程式碼範例說明如何使用代碼管理工具服務中的幾項功能。
使用變數、觸發條件和代碼建立容器版本。
以下程式碼範例使用 Tag Manager API V2 先建立名稱以目前日期時間戳記的容器,以提升容器重複的可能性。接著,此範例會建立包含隨機值變數的工作區,以及對任何網頁瀏覽啟動的觸發條件。接下來,這個範例使用觸發條件建立任意像素標記,進而在網址結尾附加快取破壞,向 //example.com
觸發像素。最後,範例會建立含有上述實體的容器版本,並記錄該版本並傳回供日後使用。
/** * 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. */ function createContainerVersion(accountPath) { const date = new Date(); // Creates a container in the account, using the current timestamp to make // sure the container is unique. try { const container = TagManager.Accounts.Containers.create( { 'name': 'appscript tagmanager container ' + date.getTime(), 'usageContext': ['WEB'] }, accountPath); const containerPath = container.path; // Creates a workspace in the container to track entity changes. const workspace = TagManager.Accounts.Containers.Workspaces.create( {'name': 'appscript workspace', 'description': 'appscript workspace'}, containerPath); const workspacePath = workspace.path; // Creates a random value variable. const variable = TagManager.Accounts.Containers.Workspaces.Variables.create( {'name': 'apps script variable', 'type': 'r'}, workspacePath); // Creates a trigger that fires on any page view. const trigger = 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. const tag = 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. const version = TagManager.Accounts.Containers.Workspaces .create_version( {'name': 'apps script version'}, workspacePath) .containerVersion; console.log(version); return version; } catch (e) { // TODO (Developer) - Handle exception console.log('Failed with error: %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. */ function grabContainerPath(versionPath) { const pathParts = versionPath.split('/'); return pathParts.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. */ function publishVersionAndQuickPreviewDraft(version) { try { const containerPath = grabContainerPath(version.path); // Publish the input container version. TagManager.Accounts.Containers.Versions.publish(version.path); const workspace = TagManager.Accounts.Containers.Workspaces.create( {'name': 'appscript workspace', 'description': 'appscript workspace'}, containerPath); const workspaceId = workspace.path; // Quick previews the current container draft. const quickPreview = TagManager.Accounts.Containers.Workspaces .quick_preview(workspace.path); console.log(quickPreview); } catch (e) { // TODO (Developer) - Handle exceptions console.log('Failed with error: $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. */ function grabContainerPath(versionPath) { const pathParts = versionPath.split('/'); return pathParts.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. */ function createAndReauthorizeUserEnvironment(version) { try { // Creates a container version. const containerPath = grabContainerPath(version.path); // Creates a user environment that points to a container version. const environment = 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 exceptions console.log('Failed with error: $s', e.error); } }
記錄帳戶中的所有電子郵件和容器存取權限。
下方程式碼範例會使用 Tag Manager API V2,找出代碼管理工具帳戶中的所有權限清單。然後,範例會記錄每個項目的使用者電子郵件地址、容器 ID 和容器存取權限類型。
/** * Logs all emails and container access permission within an account. * @param {string} accountPath The account path. */ function logAllAccountUserPermissionsWithContainerAccess(accountPath) { try { const userPermissions = TagManager.Accounts.User_permissions.list(accountPath).userPermission; for (let i = 0; i < userPermissions.length; i++) { const userPermission = userPermissions[i]; if ('emailAddress' in userPermission) { const containerAccesses = userPermission.containerAccess; for (let j = 0; j < containerAccesses.length; j++) { const containerAccess = containerAccesses[j]; console.log( 'emailAddress:' + userPermission.emailAddress + ' containerId:' + containerAccess.containerId + ' containerAccess:' + containerAccess.permission); } } } } catch (e) { // TODO (Developer) - Handle exceptions console.log('Failed with error: $s', e.error); } }