Rules: create

ต้องมีการให้สิทธิ์

สร้างกฎ GTM ลองใช้เลยหรือดูตัวอย่าง

ส่งคำขอ

คำขอ HTTP

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

พารามิเตอร์

ชื่อพารามิเตอร์ ค่า คำอธิบาย
พารามิเตอร์เส้นทาง
accountId string รหัสบัญชี GTM
containerId string รหัสคอนเทนเนอร์ GTM

การให้สิทธิ์

คำขอนี้ต้องได้รับสิทธิ์ด้วยขอบเขตต่อไปนี้ (อ่านเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์)

ขอบเขต
https://www.googleapis.com/auth/tagmanager.edit.containers

เนื้อหาของคำขอ

ในเนื้อหาคำขอ ให้ระบุทรัพยากรกฎพร้อมด้วยพร็อพเพอร์ตี้ต่อไปนี้

ชื่อพร็อพเพอร์ตี้ ค่า คำอธิบาย Notes
พร็อพเพอร์ตี้ที่จำเป็น
condition[].parameter[] list รายการพารามิเตอร์ที่มีชื่อ (คีย์/ค่า) โดยขึ้นอยู่กับประเภทของเงื่อนไข หมายเหตุ:
  • สำหรับโอเปอเรเตอร์ไบนารี ให้ใส่พารามิเตอร์ชื่อ 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 รายการเงื่อนไขที่เป็นส่วนประกอบของกฎนี้ (โดยนัย และ ระหว่างเงื่อนไข) เขียนได้
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')

ลองใช้เลย

ใช้ API Explorer ด้านล่างเพื่อเรียกใช้เมธอดนี้กับข้อมูลแบบเรียลไทม์และดูการตอบสนอง