บริการ Tag Manager

บริการ Google Tag Manager ให้สิทธิ์เข้าถึงข้อมูล Tag Manager API แก่ผู้ใช้ที่ได้รับอนุญาต บริการนี้ อนุญาตให้ผู้ใช้ Tag Manager จัดการ บัญชี containers สภาพแวดล้อม versions พื้นที่ทำงาน โฟลเดอร์ ตัวแปร ทริกเกอร์ แท็ก และ สิทธิ์ของผู้ใช้

ข้อมูลอ้างอิง

หากต้องการข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ โปรดดูเอกสารอ้างอิงสำหรับ Tag Manager API V2

บริการ Tag Manager จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script โปรดดูข้อมูลเพิ่มเติมที่หัวข้อวิธีกำหนดลายเซ็นของเมธอด

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูศูนย์ช่วยเหลือของ Google Tag Manager

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างแสดงวิธีใช้ฟีเจอร์บางรายการของบริการ Tag Manager

สร้างเวอร์ชันคอนเทนเนอร์ที่มีตัวแปร ทริกเกอร์ และแท็ก

โค้ดตัวอย่างด้านล่างใช้ Tag Manager API V2 ในการสร้างคอนเทนเนอร์ที่มีชื่อซึ่งมีการประทับเวลาพร้อมกับวันที่ปัจจุบันก่อน เพื่อเพิ่มโอกาสที่คอนเทนเนอร์นั้นไม่ซ้ำกัน จากนั้นตัวอย่างจะสร้างพื้นที่ทำงานด้วยตัวแปรค่าแบบสุ่ม และทริกเกอร์ที่เริ่มทำงานสำหรับการดูหน้าเว็บแต่ละครั้ง ถัดไป ตัวอย่างจะใช้ทริกเกอร์ในการสร้างแท็กพิกเซลที่กําหนดเองที่เริ่มการทำงานของพิกเซลไปยัง //example.com โดยมีระบบป้องกันแคชต่อท้าย URL สุดท้าย ตัวอย่างจะสร้างเวอร์ชันคอนเทนเนอร์ที่มีเอนทิตีข้างต้น จากนั้นบันทึกเวอร์ชันแล้วแสดงผลเพื่อใช้ในภายหลัง

advanced/tagManager.gs
/**
 * 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 เพื่อยอมรับเวอร์ชันคอนเทนเนอร์ที่อาจสร้างขึ้นในตัวอย่างด้านบน และเรียกข้อมูลบัญชี คอนเทนเนอร์ และรหัสเวอร์ชันจากเวอร์ชันดังกล่าว ตัวอย่างนี้ใช้รหัสเหล่านี้ในการนำเวอร์ชันคอนเทนเนอร์ไปใช้จริงทั่วโลก สุดท้ายนี้ ตัวอย่างจะสร้างตัวอย่างแบบคร่าวๆ ของพื้นที่ทำงานใหม่และบันทึกตัวอย่างแบบคร่าวๆ

advanced/tagManager.gs
/**
 * 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 เพื่อยอมรับเวอร์ชันคอนเทนเนอร์ และแยกบัญชี คอนเทนเนอร์ และรหัสเวอร์ชัน ตัวอย่างนี้ใช้รหัสเหล่านี้เพื่อสร้างสภาพแวดล้อมของผู้ใช้ที่ชี้ไปยังเวอร์ชันคอนเทนเนอร์อินพุตและบันทึกสภาพแวดล้อมของผู้ใช้ ตัวอย่างนี้จะสรุปด้วยการบันทึกสภาพแวดล้อมผู้ใช้ที่ได้รับอนุญาตอีกครั้ง

advanced/tagManager.gs
/**
 * 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 เพื่อค้นหารายการสิทธิ์ทั้งหมดภายในบัญชี Tag Manager จากนั้นตัวอย่างจะบันทึกอีเมลของผู้ใช้ รหัสคอนเทนเนอร์ และประเภทของสิทธิ์การเข้าถึงคอนเทนเนอร์สำหรับแต่ละรายการ

advanced/tagManager.gs
/**
 * 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);
  }
}