Rules: create

נדרשת הרשאה

יצירת כלל GTM. אפשר לנסות עכשיו או לראות דוגמה.

בקשה

בקשת HTTP

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

פרמטרים

שם הפרמטר Value התיאור
פרמטרים של נתיב
accountId string מספר החשבון ב-GTM.
containerId string מזהה מאגר התגים של GTM.

הרשאות

בקשה זו מחייבת הרשאה בהיקף הבא (למידע נוסף על אימות והרשאה).

היקף
https://www.googleapis.com/auth/tagmanager.edit.containers

גוף הבקשה

בגוף הבקשה, מספקים משאב כללים עם המאפיינים הבאים:

שם הנכס Value התיאור הערות
מאפיינים נדרשים
condition[].parameter[] list רשימה של פרמטרים בעלי שם (מפתח/ערך), בהתאם לסוג התנאי. הערות:
  • באופרטורים בינאריים, כוללים פרמטרים בשם arg0 ו-arg1 לציון האופרנד הימני והשמאלי, בהתאמה.
  • בשלב זה, האופרנד השמאלי (arg0) חייב להיות הפניה למאקרו.
  • לצורך התאמת ביטוי רגולרי (regex) שאינה תלוית-רישיות, יש לכלול פרמטר בוליאני בשם 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 המשתמש מציין איך להחיל את הכלל הזה בכלי הקיבול. ניתן לכתיבה

תשובה

אם הפעולה בוצעה ללא שגיאות, השיטה הזו תחזיר משאב הכללים בגוף התגובה.

דוגמאות

הערה: דוגמאות הקוד הזמינות לשיטה זו לא מייצגות את כל שפות התכנות הנתמכות (רשימת השפות הנתמכות זמינה בדף של ספריות המשתמשים).

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

רוצה לנסות?

ניתן להשתמש ב-APIs Explorer שבהמשך כדי לקרוא לשיטה הזו בנתונים בזמן אמת ולראות את התגובה.