TagManager Service

The Google Tag Manager service provides access to Tag Manager API data for an authorized user. This service allows Tag Manager users to manage Tag Manager accounts, containers, environments, versions, workspaces, folders, variables, triggers, tags, and user permissions.

Reference

For detailed information on this service, see the reference documentation for the Tag Manager API V2.

Like all advanced services in Apps Script, the Tag Manager service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below demonstrates how to use a few features of the Tag Manager service.

Open code in new window

Creates a container version with a variable, trigger, and tag.

The sample code below uses Tag Manager API V2 to first create a container with a name that is timestamped with the current date to improve the chances of it being unique. The sample then creates a workspace with 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 pixel to //example.com with a cache buster appended to the end of the URL. Last, the sample creates a container version with the above entities, logs the version and returns it for later use.

/**
 * Creates a container version for a particular account with the input
 * accountPath.
 */
function createContainerVersion(accountPath) {
  var date = new Date();
  // Creates a container in the account, using the current timestamp to make
  // sure the container is unique.
  var container = TagManager.Accounts.Containers.create(
      {
        'name': 'appscript tagmanager container ' + date.getTime(),
        'usageContext': ['WEB']
      },
      accountPath);
  var containerPath = container.path;
  // Creates a workspace in the container to track entity changes.
  var workspace = TagManager.Accounts.Containers.Workspaces.create(
      {'name': 'appscript workspace', 'description': 'appscript workspace'},
      containerPath);
  var workspacePath = workspace.path;
  // Creates a random value variable.
  var variable = TagManager.Accounts.Containers.Workspaces.Variables.create(
      {'name': 'apps script variable', 'type': 'r'},
      workspacePath);
  // Creates a trigger that fires on any page view.
  var 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.
  var 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.
  var version = TagManager.Accounts.Containers.Workspaces
                    .create_version(
                        {'name': 'apps script version'}, workspacePath)
                    .containerVersion;
  Logger.log(version);
  return version;
}

Publishes a container version and quick previews the current container draft.

The sample code below uses Tag Manager API V2 to accept a container version that may have been created in the above example, and retrieve the account, container, and version ids from the version. The sample 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 quick preview.

/** Retrieves the container path from a container version path. */
function grabContainerPath(versionPath) {
  var 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.
 */
function publishVersionAndQuickPreviewDraft(version) {
  var containerPath = grabContainerPath(version.path);
  // Publish the input container version.
  TagManager.Accounts.Containers.Versions.publish(version.path);
  var workspace = TagManager.Accounts.Containers.Workspaces.create(
      {'name': 'appscript workspace', 'description': 'appscript workspace'},
      containerPath);
  var workspaceId = workspace.path;
  // Quick previews the current container draft.
  var quickPreview = TagManager.Accounts.Containers.Workspaces.quick_preview(
      workspace.path);
  Logger.log(quickPreview);
}

Creates and reauthorizes a user environment.

The sample code below uses Tag Manager API V2 to accept a container version and extract the account, container, and version ids. The sample utilizes these ids to create a user environment that points to the input container version and logs the user environment. The sample concludes by logging a reauthorized user environment.

/** Retrieves the container path from a container version path. */
function grabContainerPath(versionPath) {
  var 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.
 */
function createAndReauthorizeUserEnvironment(version) {
  // Creates a container version.
  var containerPath = grabContainerPath(version.path)
  // Creates a user environment that points to a container version.
  var environment = TagManager.Accounts.Containers.Environments.create(
      {
        'name': 'test_environment',
        'type': 'user',
        'containerVersionId': version.containerVersionId
      },
      containerPath);
  Logger.log('Original user environment: ' + environment);
  // Reauthorizes the user environment that points to a container version.
  TagManager.Accounts.Containers.Environments.reauthorize({}, environment.path);
  Logger.log('Reauthorized user environment: ' + environment);
}

Logs all emails and container access permissions within an account.

The sample code below uses Tag Manager API V2 to find a list of all permissions within a Tag Manager account. The sample then logs the user's email address, the container id, and the types of container access permissions for each entry.

/**
 * Logs all emails and container access permission within an account.
 */
function logAllAccountUserPermissionsWithContainerAccess(accountPath) {
  var userPermissions =
      TagManager.Accounts.User_permissions.list(accountPath).userPermission;
  for (var i = 0; i < userPermissions.length; i++) {
    var userPermission = userPermissions[i];
    if ('emailAddress' in userPermission) {
      var containerAccesses = userPermission.containerAccess;
      for (var j = 0; j < containerAccesses.length; j++) {
        var containerAccess = containerAccesses[j];
        Logger.log(
            'emailAddress:' + userPermission.emailAddress + ' containerId:' +
            containerAccess.containerId + ' containerAccess:' +
            containerAccess.permission);
      }
    }
  }
}

发送以下问题的反馈:

此网页
Apps Script
Apps Script