Rules: update

需要授權

更新 GTM 規則。 立即試用查看範例

要求

HTTP 要求

PUT https://www.googleapis.com/tagmanager/v1/accounts/accountId/containers/containerId/rules/ruleId

參數

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

授權

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

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

要求主體

在要求主體中,提供具有以下屬性的規則資源

資源名稱 說明 附註
必要屬性
condition[].parameter[] list 根據條件類型而定的已命名參數 (鍵/值) 清單。附註:
  • 針對二進位運算子,請加入名為 arg0arg1 的參數,分別指定左右運算元。
  • 目前,左運算元 (arg0) 必須是巨集的參照。
  • 對於不區分大小寫的規則運算式比對,請加入名為 ignore_case 的布林值參數,並設為 true。如未指定或設為任何其他值,比對會區分大小寫。
  • 如要否定運算子,請加入名為 negate 的布林參數,並將其設為 true
可寫入
condition[].parameter[].type string 參數類型。有效值如下:
  • boolean:這個值代表布林值,以「true」或「false」表示
  • integer:這個值代表 64 位元的帶正負號整數值,以 10 為底數
  • list:應指定參數清單
  • map:應指定參數對應
  • template:這個值代表任何文字,可能包括巨集參照 (甚至是可能會傳回非字串類型的巨集參照)


可接受的值為:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
可寫入
condition[].type string 此條件的運算子類型。

可接受的值為:
  • "contains"
  • "cssSelector"
  • "endsWith"
  • "equals"
  • "greater"
  • "greaterOrEquals"
  • "less"
  • "lessOrEquals"
  • "matchRegex"
  • "startsWith"
  • "urlMatches"
可寫入
選用屬性
condition[] list 組成這項規則的條件清單 (兩者之間隱含 AND)。 可寫入
condition[].parameter[].key string 唯一識別參數的已命名鍵。必須用於頂層參數和對應值。清單值會忽略。 可寫入
condition[].parameter[].list[] list 此清單參數的參數 (系統會忽略鍵)。 可寫入
condition[].parameter[].map[] list 此對應參數的參數 (必須有鍵,鍵不得重複)。 可寫入
condition[].parameter[].value string 符合指定類型的參數值 (可能包含「」等巨集參照)。 可寫入
name string 規則顯示名稱。 可寫入
notes string 使用者說明如何在容器中套用這項規則。 可寫入

回應

如果成功的話,這個方法會在回應主體中傳回規則資源

範例

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

Java

使用 Java 用戶端程式庫

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

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

// Construct the condition parameters.
Parameter arg0 = new Parameter();
arg0.setType("template");
arg0.setKey("arg0");
arg0.setValue("{{url}}");

Parameter arg1 = new Parameter();
arg1.setType("template");
arg1.setKey("arg1");
arg1.setValue(".*hellowworld.html");

// Construct the condition object
Condition condition = new Condition();
condition.setType("endsWith");
condition.setParameter(Arrays.asList(arg0, arg1));

// Construct the rule object.
Rule rule = new Rule();
rule.setName("Updated Rule");
rule.setCondition(Arrays.asList(condition));

try {
  Rule response = tagmanager.accounts().containers().
      rules().update("123456", "54321", "2", rule).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 rule for the authorized user.
try:
  response = tagmanager.accounts().containers().rules().update(
      accountId='123456',
      containerId='54321',
      ruleId='2',
      body={
          'name': 'Updated Ends with Rule',
          'condition': [
              {
                  'type': 'endsWith',
                  'parameter': [
                      {
                          'type': 'template',
                          'key': 'arg0',
                          'value': '{{url}}'
                      },
                      {
                          'type': 'template',
                          'key': 'arg1',
                          'value': '.*hellowworld.html'
                      }
                  ]
              }
          ]
      }
  ).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,針對即時資料呼叫這個方法,然後查看回應。