Rules: create

Requires authorization

Creates a GTM rule. Try it now or see an example.

Request

HTTP request

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

Parameters

Parameter name Value Description
Path parameters
accountId string The GTM Account ID.
containerId string The GTM Container ID.

Authorization

This request requires authorization with the following scope (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/tagmanager.edit.containers

Request body

In the request body, supply a Rules resource with the following properties:

Property name Value Description Notes
Required Properties
condition[].parameter[] list A list of named parameters (key/value), depending on the condition's type. Notes:
  • For binary operators, include parameters named arg0 and arg1 for specifying the left and right operands, respectively.
  • At this time, the left operand (arg0) must be a reference to a macro.
  • For case-insensitive Regex matching, include a boolean parameter named ignore_case that is set to true. If not specified or set to any other value, the matching will be case sensitive.
  • To negate an operator, include a Boolean parameter named negate and set it to true.
writable
condition[].parameter[].type string The parameter type. Valid values are:
  • boolean: The value represents a boolean, represented as 'true' or 'false'
  • integer: The value represents a 64-bit signed integer value, in base 10
  • list: A list of parameters should be specified
  • map: A map of parameters should be specified
  • template: The value represents any text; this can include macro references (even macro references that might return non-string types)


Acceptable values are:
  • "boolean"
  • "integer"
  • "list"
  • "map"
  • "template"
writable
condition[].type string The type of operator for this condition.

Acceptable values are:
  • "contains"
  • "cssSelector"
  • "endsWith"
  • "equals"
  • "greater"
  • "greaterOrEquals"
  • "less"
  • "lessOrEquals"
  • "matchRegex"
  • "startsWith"
  • "urlMatches"
writable
name string Rule display name. writable
Optional Properties
condition[] list The list of conditions that make up this rule (implicit AND between them). writable
condition[].parameter[].key string The named key that uniquely identifies a parameter. Required for top-level parameters, as well as map values. Ignored for list values. writable
condition[].parameter[].list[] list This list parameter's parameters (keys will be ignored). writable
condition[].parameter[].map[] list This map parameter's parameters (must have keys; keys must be unique). writable
condition[].parameter[].value string A parameter's value (may contain macro references such as "{{myMacro}}") as appropriate to the specified type. writable
notes string User notes on how to apply this rule in the container. writable

Response

If successful, this method returns a Rules resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

/*
 * 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

Uses the Python client library.

# 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')

Try it!

Use the APIs Explorer below to call this method on live data and see the response.