Rules: create

認証が必要です

GTM ルールを作成します。 今すぐ試すまたは例を見る

リクエスト

HTTP リクエスト

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

パラメータ

パラメータ名 説明
パスパラメータ
accountId string GTM アカウント ID。
containerId string GTM コンテナ ID。

承認

このリクエストは、次のスコープでの承認が必要です(認証と承認の詳細をご確認ください)。

スコープ
https://www.googleapis.com/auth/tagmanager.edit.containers

リクエスト本文

リクエストの本文には、以下のプロパティを使用して Rules リソースを指定します。

プロパティ名 説明 メモ
必須プロパティ
condition[].parameter[] list 条件の種類に応じた名前付きパラメータ(Key-Value)のリスト。備考:
  • 二項演算子の場合は、arg0 および arg1 というパラメータで、左と右のオペランドをそれぞれ指定します。
  • このとき、左側のオペランド(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"
書き込み可能
name string ルールの表示名。 書き込み可能
省略可能なプロパティ
condition[] list このルールを構成する条件のリスト(条件は暗黙的に「AND」で連結)。 書き込み可能
condition[].parameter[].key string パラメータを一意に識別する名前付きキー。トップレベルのパラメータとマップ値では必須となります。リスト値では無視されます。 書き込み可能
condition[].parameter[].list[] list このリスト パラメータのパラメータ(キーは無視されます)。 書き込み可能
condition[].parameter[].map[] list このマップ パラメータのパラメータ(一意のキーが必要)。 書き込み可能
condition[].parameter[].value string 指定された型のパラメータ値(「」などマクロの参照も使用できます)。 書き込み可能
notes string コンテナでこのルールを利用する方法についてのユーザーのメモ。 書き込み可能

レスポンス

成功すると、このメソッドによりレスポンスの本文で Rules リソースが返されます。

注: このメソッドで使用可能なコード例では、サポートされているプログラミング言語すべての例を示しているわけではありません(サポートされている言語の一覧については、クライアント ライブラリ ページをご覧ください)。

Java

Java クライアント ライブラリを使用します。

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

/*
 * This request creates a new 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("Ends With Rule");
rule.setCondition(Arrays.asList(condition));

try {
  Rule response = tagmanager.accounts().
      containers().rules().create("123456", "54321", rule).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("Rule Id = " + response.getRuleId());
System.out.println("Rule Fingerprint = " + response.getFingerprint());

Python

Python クライアント ライブラリを使用します。

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

# This request creates a new rule for the authorized user.
try:
  response = tagmanager.accounts().containers().rules().create(
      accountId='123456',
      containerId='54321',
      body={
          'name': '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 create method are stored in the response object.
# The following code shows how to access the created id and fingerprint.
print 'Rule Id = %s' % response.get('ruleId')
print 'Rule Fingerprint = %s' % response.get('fingerprint')

試してみよう:

以下の API Explorer を使用して、ライブデータに対してこのメソッドを呼び出し、レスポンスを確認してください。