Variables: create

需要授权

创建 GTM 变量。 立即试用查看示例

请求

HTTP 请求

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

参数

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

授权

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

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

请求正文

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

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


可接受的值:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
  • "triggerReference"
可写
type string GTM 变量类型。 可写
可选属性
disablingTriggerId[] list 仅适用于移动容器:用于停用条件变量的触发器 ID 列表;如果某个启用触发器为 true,且所有停用触发器为 false,则会启用相应变量。处理时,会将其视为无序集。 可写
enablingTriggerId[] list 仅适用于移动容器:用于启用条件变量的触发器 ID 列表;如果某个启用触发器为 true,且所有停用触发器为 false,则会启用相应变量。处理时,会将其视为无序集。 可写
notes string 有关如何在容器中应用此变量的用户注释。 可写
parameter[] list 变量的参数。 可写
parameter[].key string 唯一标识参数的命名键。对于顶级参数及映射值,该键属于必需项。但对于列表值,会忽略该键。 可写
parameter[].list[] list 该列表参数的参数(键将被忽略)。 可写
parameter[].map[] list 该映射参数的参数(必须提供键;且键必须具有唯一性)。 可写
parameter[].value string 适用于指定类型的参数的值,可以包含变量引用,如“”。 可写
scheduleEndMs long 用于安排变量的结束时间戳(以毫秒为单位)。 可写
scheduleStartMs long 用于安排变量的开始时间戳(以毫秒为单位)。 可写

响应

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

示例

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

Java

使用 Java 客户端库

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

/*
 * This request creates a new variable for the authorized user.
 */

// Create the variable object.
Variable variable = new Variable();
variable.setName("randomNumber");
variable.setType("r"); // The random number type.

try {
  Variable response = tagmanager.accounts().containers().
      variables().create("123456", "54321", variable).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 the response object.
 * The following code shows how to access the created Id and Fingerprint.
 */
System.out.println("Variable Id = " + response.getVariableId());
System.out.println("Variable Fingerprint = " + response.getFingerprint());

Python

使用 Python 客户端库

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

# This request creates a new variable.
try:
  response = tagmanager.accounts().containers().variables().create(
      accountId='23732470',
      containerId='801231',
      body={
          'name': 'random_number',
          'type': 'r'
      }
  ).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 response object.
# The following code shows how to access the created id and fingerprint.
print 'Variable Id = %s' % response.get('variableId')
print 'Variable Fingerprint = %s' % response.get('fingerprint')

试试看!

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