需要授权
请求
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 |
参数类型。有效值:
可接受的值:
|
可写入 |
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 对实际数据调用此方法,然后查看响应。