需要授权
请求
HTTP 请求
POST https://www.googleapis.com/tagmanager/v1/accounts/accountId/containers/containerId/macros
参数
参数名称 | 值 | 说明 |
---|---|---|
路径参数 | ||
accountId |
string |
GTM 帐号 ID。 |
containerId |
string |
GTM 容器 ID。 |
授权
此请求需要获得下列范围的授权(详细了解身份验证和授权)。
范围 |
---|
https://www.googleapis.com/auth/tagmanager.edit.containers |
请求正文
在请求正文中,提供具有以下属性的 Macros 资源:
属性名称 | 值 | 说明 | 备注 |
---|---|---|---|
必需属性 | |||
name |
string |
宏显示名。 | 可写 |
parameter[].type |
string |
参数类型。有效值:
可接受的值:
|
可写 |
type |
string |
GTM 宏类型。 | 可写 |
可选属性 | |||
disablingRuleId[] |
list |
仅适用于移动容器:用于停用条件宏的规则 ID 列表;如果某个启用规则为 true,且所有停用规则为 false,则会启用相应宏。处理时,会将其视为无序集。 | 可写 |
enablingRuleId[] |
list |
仅适用于移动容器:用于启用条件宏的规则 ID 列表;如果某个启用规则为 true,且所有停用规则为 false,则会启用相应宏。处理时,会将其视为无序集。 | 可写 |
notes |
string |
有关如何在容器中应用此宏的用户注释。 | 可写 |
parameter[] |
list |
宏的参数。 | 可写 |
parameter[].key |
string |
唯一标识参数的命名键。对于顶级参数及映射值,该键属于必需项。但对于列表值,会忽略该键。 | 可写 |
parameter[].list[] |
list |
该列表参数的参数(键将被忽略)。 | 可写 |
parameter[].map[] |
list |
该映射参数的参数(必须提供键;且键必须具有唯一性)。 | 可写 |
parameter[].value |
string |
适用于指定类型的参数的值,可以包含宏引用,例如“”。 | 可写 |
scheduleEndMs |
long |
用于安排宏的结束时间戳(以毫秒为单位)。 | 可写 |
scheduleStartMs |
long |
用于安排宏的开始时间戳(以毫秒为单位)。 | 可写 |
响应
如果成功,此方法将在响应正文中返回 Macros 资源。
示例
注意:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。
Java
使用 Java 客户端库。
/* * Note: This code assumes you have an authorized tagmanager service object. */ /* * This request creates a new macro for the authorized user. */ // Construct the macro object. Macro macro = new Macro(); macro.setName("Sample URL Macro"); macro.setType("u"); // Construct the parameters. Parameter arg0 = new Parameter(); arg0.setType("template"); arg0.setKey("component"); arg0.setValue("URL"); Parameter arg1 = new Parameter(); arg1.setType("template"); arg1.setKey("customUrlSource"); arg1.setValue("{{element}}"); // set the parameters on the macro. macro.setParameter(Arrays.asList(arg0, arg1)); try { Macro response = tagmanager.accounts(). containers().macros().create("123456", "54321", macro).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("Macro Id = " + response.getMacroId()); System.out.println("Macro Fingerprint = " + response.getFingerprint());
Python
使用 Python 客户端库。
# Note: This code assumes you have an authorized tagmanager service object. # This request creates a new macro for the authorized user. try: response = tagmanager.accounts().containers().macros().create( accountId='123456', containerId='54321', body={ 'name': 'Sample URL Macro', 'type': 'u', 'parameter': [ { 'type': 'template', 'key': 'component', 'value': 'URL' }, { 'type': 'template', 'key': 'customUrlSource', 'value': '{{element}}' } ] } ).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 'Macro Id = %s' % response.get('macroId') print 'Macro Fingerprint = %s' % response.get('fingerprint')
试试看!
请使用下面的 API Explorer 对实时数据调用此方法并查看响应。