Dịch vụ Trình quản lý thẻ

Dịch vụ Trình quản lý thẻ của Google cung cấp quyền truy cập vào dữ liệu API Trình quản lý thẻ cho một người dùng được uỷ quyền. Dịch vụ này cho phép người dùng Trình quản lý thẻ quản lý tài khoản, containers, môi trường, versions, không gian làm việc, thư mục, biến, trình kích hoạt, thẻquyền của người dùng

Tài liệu tham khảo

Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo cho API Trình quản lý thẻ phiên bản 2.

Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Trình quản lý thẻ sử dụng cùng đối tượng, phương thức và tham số như API công khai. Để biết thêm thông tin, hãy xem phần Cách xác định chữ ký phương thức.

Để báo cáo vấn đề và tìm thông tin hỗ trợ khác, hãy xem Trung tâm trợ giúp Trình quản lý thẻ của Google.

Mã mẫu

Mã mẫu dưới đây minh hoạ cách sử dụng một vài tính năng của dịch vụ Trình quản lý thẻ.

Tạo phiên bản vùng chứa có biến, trình kích hoạt và thẻ.

Mã mẫu bên dưới sử dụng API Trình quản lý thẻ phiên bản 2 để tạo một vùng chứa có tên có dấu thời gian với ngày hiện tại nhằm tăng khả năng là vùng chứa duy nhất. Sau đó, mẫu sẽ tạo không gian làm việc với một biến giá trị ngẫu nhiên và một trình kích hoạt sẽ kích hoạt cho bất kỳ lượt xem trang nào. Tiếp theo, mẫu sử dụng điều kiện kích hoạt để tạo một thẻ pixel tuỳ ý kích hoạt một pixel cho //example.com với một trình chặn truy xuất bộ nhớ đệm được nối thêm vào cuối URL. Cuối cùng, mẫu sẽ tạo một phiên bản vùng chứa với các thực thể ở trên, ghi nhật ký phiên bản và trả về để sử dụng sau.

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);
  }
}

Xuất bản phiên bản vùng chứa và xem trước nhanh bản nháp vùng chứa hiện tại.

Mã mẫu bên dưới sử dụng API Trình quản lý thẻ phiên bản 2 để chấp nhận phiên bản vùng chứa có thể đã được tạo trong ví dụ trên, đồng thời truy xuất mã tài khoản, mã vùng chứa và phiên bản từ phiên bản đó. Mẫu sử dụng các mã nhận dạng này để phát hành phiên bản vùng chứa trực tiếp ra toàn thế giới. Cuối cùng, mẫu sẽ tạo một bản xem trước nhanh của không gian làm việc mới và ghi lại bản xem trước nhanh.

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);
  }
}

Tạo và uỷ quyền lại môi trường người dùng.

Mã mẫu bên dưới sử dụng API Trình quản lý thẻ phiên bản 2 để chấp nhận phiên bản vùng chứa và trích xuất mã tài khoản, mã vùng chứa và phiên bản. Mẫu sử dụng các mã nhận dạng này để tạo môi trường người dùng trỏ đến phiên bản vùng chứa đầu vào và ghi nhật ký môi trường người dùng. Mẫu kết thúc bằng cách ghi lại một môi trường người dùng được uỷ quyền lại.

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);
  }
}

Ghi nhật ký tất cả email và quyền truy cập vùng chứa vào một tài khoản.

Mã mẫu bên dưới sử dụng API Trình quản lý thẻ phiên bản 2 để tìm danh sách tất cả các quyền trong tài khoản Trình quản lý thẻ. Sau đó, mẫu sẽ ghi nhật ký địa chỉ email của người dùng, mã vùng chứa và các loại quyền truy cập vùng chứa cho từng mục nhập.

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);
  }
}