Tags: create

需要授权

创建 GTM 代码。 立即试用查看示例

请求

HTTP 请求

POST https://www.googleapis.com/tagmanager/v1/accounts/accountId/containers/containerId/tags

参数

参数名称 说明
路径参数
accountId string GTM 帐号 ID。
containerId string GTM 容器 ID。

授权

此请求需要获得下列范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/tagmanager.edit.containers

请求正文

在请求正文中,提供具有以下属性的 Tags 资源

属性名称 说明 备注
必需属性
name string 代码的显示名称。 可写
parameter[].type string 参数类型。有效值:
  • boolean:该值表示布尔值,以“true”或“false”表示
  • integer:该值表示 64 位的正负十进制整数值
  • list:应指定的参数的列表
  • map:应指定的参数的映射
  • template:该值表示任何文本;可以包括变量引用,甚至是可能返回非字符串类型的变量引用
  • trigger_reference:该值表示触发器,以触发器 ID 表示


可接受的值:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
  • "triggerReference"
可写
priority.type string 参数类型。有效值:
  • boolean:该值表示布尔值,以“true”或“false”表示
  • integer:该值表示 64 位的正负十进制整数值
  • list:应指定的参数的列表
  • map:应指定的参数的映射
  • template:该值表示任何文本;可以包括变量引用,甚至是可能返回非字符串类型的变量引用
  • trigger_reference:该值表示触发器,以触发器 ID 表示


可接受的值:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
  • "triggerReference"
可写
type string GTM 代码类型。 可写
可选属性
blockingRuleId[] list 屏蔽规则 ID。如果列出的任何规则的计算结果为 true,将不触发代码。 可写
blockingTriggerId[] list 屏蔽型触发器 ID。如果列出的任何触发器的计算结果为 true,将不触发代码。 可写
firingRuleId[] list 触发规则 ID。如果列出的任何规则为 true 且其所有 blockingRuleIds(如果指定)均为 false,则将触发代码。 可写
firingTriggerId[] list 触发型触发器 ID。如果列出的任何触发器为 true 且其所有 blockingTriggerIds(如果指定)均为 false,则将触发代码。 可写
liveOnly boolean 如果设置为 true,则此代码仅在实际环境中触发,即不在预览或调试模式下触发。 可写
notes string 有关如何在容器中应用此代码的用户注释。 可写
parameter[] list 代码的参数。 可写
parameter[].key string 唯一标识参数的命名键。对于顶级参数及映射值,该键属于必需项。但对于列表值,会忽略该键。 可写
parameter[].list[] list 该列表参数的参数(键将被忽略)。 可写
parameter[].map[] list 该映射参数的参数(必须提供键;且键必须具有唯一性)。 可写
parameter[].value string 适用于指定类型的参数的值,可以包含变量引用,如“”。 可写
paused boolean 如果代码已暂停,则为 true。 可写
priority nested object 用户指定的代码优先级数值。代码会按优先级顺序异步触发。代码的优先级数值越大,触发顺序越靠前。代码的优先级可以是正值,也可以是负值。默认值为 0。 可写
priority.key string 唯一标识参数的命名键。对于顶级参数及映射值,该键属于必需项。但对于列表值,会忽略该键。 可写
priority.list[] list 该列表参数的参数(键将被忽略)。 可写
priority.map[] list 该映射参数的参数(必须提供键;且键必须具有唯一性)。 可写
priority.value string 适用于指定类型的参数的值,可以包含变量引用,如“”。 可写
scheduleEndMs long 用于安排代码的结束时间戳(以毫秒为单位)。 可写
scheduleStartMs long 用于安排代码的开始时间戳(以毫秒为单位)。 可写

响应

如果成功,此方法将在响应正文中返回 Tags 资源

示例

注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

Java

使用 Java 客户端库

/*
 * Note: This code assumes you have an authorized tagmanager service object.
 */

/*
 * This request creates a new tag.
 */

// Construct the parameters.
Parameter arg0 = new Parameter();
arg0.setType("template");
arg0.setKey("trackingId");
arg0.setValue("UA-123456-1");

Parameter arg1 = new Parameter();
arg1.setType("template");
arg1.setKey("type");
arg1.setValue("TRACK_TRANSACTION");

// Construct the tag object.
Tag tag = new Tag();
tag.setName("Sample Universal Analytics Tag");
tag.setType("ua");
tag.setLiveOnly(false);
tag.setParameter(Arrays.asList(arg0, arg1));

try {
  Tag response = tagmanager.accounts().containers().
      tags().create("123456", "54321", tag).execute();

} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}


/*
 * The results of the create method are stored in response object.
 * The following code shows how to access the created id and fingerprint.
 */
System.out.println("Tag id = " + response.getTagId());
System.out.println("Tag Fingerprint = " + response.getFingerprint());

Python

使用 Python 客户端库

# Note: This code assumes you have an authorized tagmanager service object.

# This request creates a new container tag.
try:
  response = tagmanager.accounts().containers().tags().create(
      accountId='123456',
      containerId='54321',
      body={
          'name': 'Universal Analytics Tag',
          'type': 'ua',
          'liveOnly': False,
          'parameter': [
              {
                  'type': 'template',
                  'key': 'trackingId',
                  'value': 'UA-123456-1'
              },
              {
                  'type': 'template',
                  'key': 'type',
                  'value': 'TRACK_TRANSACTION'
              }
          ]
      }
  ).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

# The results of the create method are stored in the response object.
# The following code shows how to access the created id and fingerprint.
print response.get('tagId')
print response.get('fingerprint')

试试看!

请使用下面的 API Explorer 对实时数据调用此方法并查看响应。