Macros: update

需要授權

更新 GTM 巨集。 立即試用查看範例

要求

HTTP 要求

PUT https://www.googleapis.com/tagmanager/v1/accounts/accountId/containers/containerId/macros/macroId

參數

參數名稱 說明
路徑參數
accountId string GTM 帳戶 ID。
containerId string GTM 容器 ID。
macroId string GTM 巨集 ID。
自選查詢參數
fingerprint string 如有提供,這個指紋必須與儲存空間中的巨集指紋相符。

授權

這項要求需要下列範圍的授權 (進一步瞭解驗證和授權)。

範圍
https://www.googleapis.com/auth/tagmanager.edit.containers

要求主體

在要求內文中,提供巨集資源並附上以下屬性:

資源名稱 說明 附註
必要屬性
parameter[].type string 參數類型。有效值如下:
  • boolean:這個值代表布林值,以「true」或「false」表示
  • integer:這個值代表 64 位元的帶正負號整數值,以 10 為底數
  • list:應指定參數清單
  • map:應指定參數對應
  • template:這個值代表任何文字,可能包括巨集參照 (甚至是可能會傳回非字串類型的巨集參照)


可接受的值為:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
可寫入
type string GTM 巨集類型。 可寫入
選用屬性
disablingRuleId[] list 僅適用於行動容器:列出停用條件巨集的規則 ID 清單;如果其中一項啟用規則為 true,而所有停用規則為 False,系統就會啟用該巨集。視為未排序的集合。 可寫入
enablingRuleId[] list 僅適用於行動容器:用於啟用條件巨集的規則 ID 清單;如果其中一項啟用規則為 true,而所有停用規則為 False,此巨集就會啟用。視為未排序的集合。 可寫入
name string 巨集顯示名稱。 可寫入
notes string 使用者如何在容器中套用這個巨集。 可寫入
parameter[] list 巨集的參數。 可寫入
parameter[].key string 唯一識別參數的已命名鍵。必須用於頂層參數和對應值。清單值會忽略。 可寫入
parameter[].list[] list 此清單參數的參數 (系統會忽略鍵)。 可寫入
parameter[].map[] list 此對應參數的參數 (必須有鍵,鍵不得重複)。 可寫入
parameter[].value string 符合指定類型的參數值 (可能包含「」等巨集參照)。 可寫入
scheduleEndMs long 排定巨集的結束時間戳記 (以毫秒為單位)。 可寫入
scheduleStartMs long 排定巨集的開始時間戳記 (以毫秒為單位)。 可寫入

回應

如果成功,這個方法會在回應主體中傳回巨集資源

範例

注意:這個方法適用的程式語言眾多,我們只在此提供部分程式碼範例,完整的支援語言清單請參閱用戶端程式庫頁面

Java

使用 Java 用戶端程式庫

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

/*
 * This request updates an existing macro for the authorized user.
 */

// Construct the macro object.
Macro macro = new Macro();
macro.setName("Updated 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().update("123456", "54321", "18", macro).execute();

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

/*
 * The results of the update method are stored in the response object.
 * The following code shows how to access the updated name and fingerprint.
 */
System.out.println("Updated Name = " + response.getName());
System.out.println("Updated Fingerprint = " + response.getFingerprint());

Python

使用 Python 用戶端程式庫

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

# This request updates an existing macro for the authorized user.
try:
  response = tagmanager.accounts().containers().macros().update(
      accountId='123456',
      containerId='54321',
      macroId='19',
      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 update method are stored in the response object.
# The following code shows how to access the updated name and fingerprint.
print 'Updated Name = %s' % response.get('name')
print 'Updated Fingerprint = %s' % response.get('fingerprint')

試試看!

您可以使用下方的 APIs Explorer,針對即時資料呼叫這個方法,然後查看回應。