ต้องมีการให้สิทธิ์
สร้างกฎ 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 |
รายการพารามิเตอร์ที่มีชื่อ (คีย์/ค่า) โดยขึ้นอยู่กับประเภทของเงื่อนไข หมายเหตุ:
|
เขียนได้ |
condition[].parameter[].type |
string |
ประเภทพารามิเตอร์ ค่าที่ถูกต้องคือ:
ค่าที่ยอมรับมีดังนี้
|
เขียนได้ |
condition[].type |
string |
ประเภทของโอเปอเรเตอร์สำหรับเงื่อนไขนี้
ค่าที่ยอมรับมีดังนี้
|
เขียนได้ |
name |
string |
ชื่อที่แสดงของกฎ | เขียนได้ |
พร็อพเพอร์ตี้ที่ไม่บังคับ | |||
condition[] |
list |
รายการเงื่อนไขที่เป็นส่วนประกอบของกฎนี้ (โดยนัย และ ระหว่างเงื่อนไข) | เขียนได้ |
condition[].parameter[].key |
string |
คีย์ที่มีชื่อซึ่งระบุพารามิเตอร์โดยไม่ซ้ำกัน ต้องระบุสำหรับพารามิเตอร์ระดับบนสุดและค่าแมป ละเว้นสำหรับค่ารายการ | เขียนได้ |
condition[].parameter[].list[] |
list |
พารามิเตอร์ของพารามิเตอร์รายการนี้ (ระบบจะไม่สนใจคีย์) | เขียนได้ |
condition[].parameter[].map[] |
list |
พารามิเตอร์ของพารามิเตอร์แมปนี้ (ต้องมีคีย์ คีย์ต้องไม่ซ้ำกัน) | เขียนได้ |
condition[].parameter[].value |
string |
ค่าของพารามิเตอร์ (อาจมีการอ้างอิงมาโคร เช่น "") ที่เหมาะสมกับประเภทที่ระบุ | เขียนได้ |
notes |
string |
ผู้ใช้บันทึกเกี่ยวกับวิธีใช้กฎนี้ในคอนเทนเนอร์ | เขียนได้ |
คำตอบ
หากสำเร็จ เมธอดนี้จะแสดงทรัพยากรกฎในเนื้อหาการตอบสนอง
ตัวอย่าง
หมายเหตุ: ตัวอย่างโค้ดที่มีสำหรับวิธีการนี้ไม่ได้แสดงถึงภาษาโปรแกรมที่รองรับทั้งหมด (ดูรายการภาษาที่รองรับได้ในหน้าไลบรารีของไคลเอ็นต์)
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
# 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 ด้านล่างเพื่อเรียกใช้เมธอดนี้กับข้อมูลแบบเรียลไทม์และดูการตอบสนอง