สร้างตัวแปรที่กําหนดเอง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คู่มือนี้จะแนะนำวิธีสร้างเทมเพลตตัวแปรที่กำหนดเอง วันและเวลา
ตัวแปรนี้ใช้ ดังนั้นจะใช้อาร์เรย์ของค่า แล้วแสดงผลเป็น
สตริงที่คั่นด้วยคอมมา
หากต้องการเริ่มเทมเพลตตัวแปรแรก ให้คลิกเทมเพลตทางด้านซ้าย
การนำทาง และคลิกปุ่มใหม่ใต้เทมเพลตตัวแปร
ในแท็บข้อมูล ให้กำหนดชื่อและคำอธิบายของตัวแปร
ชื่อ คือสิ่งที่จะแสดงต่อผู้ใช้เมื่อจะนำไปใช้งาน
ทั่วทั้งอินเทอร์เฟซผู้ใช้ Tag Manager
คำอธิบายคือชื่อที่อธิบาย - สรุป (200 อักขระหรือ
น้อยลง) คำอธิบายเกี่ยวกับสิ่งที่ตัวแปรนี้ทำ
คลิกรีเฟรชเพื่อดูตัวอย่างเทมเพลต
ทางด้านขวาของอินพุตช่องจะมีหน้าต่างตัวอย่างเทมเพลต
ทุกครั้งที่ทำการเปลี่ยนแปลงในตัวแก้ไข ปุ่มรีเฟรชจะ
ปรากฏขึ้น คลิก รีเฟรช เพื่อดูว่าการเปลี่ยนแปลงที่คุณทำกับรูปลักษณ์ของ
ตัวแปร
คลิกช่องเพื่อเพิ่มช่องลงในเทมเพลตตัวแปร
แท็บช่องของเครื่องมือแก้ไขเทมเพลตช่วยให้คุณสร้างและแก้ไขช่องใน
เทมเพลตตัวแปร ช่องใช้สำหรับป้อนข้อมูลที่กำหนดเอง เช่น บัญชี
ID คุณสามารถเพิ่มองค์ประกอบของแบบฟอร์มมาตรฐาน เช่น ช่องข้อความ เมนูแบบเลื่อนลง
เมนู ปุ่มตัวเลือก และช่องทำเครื่องหมาย
คลิกเพิ่มช่องแล้วเลือกตารางแบบง่าย แทนที่ชื่อเริ่มต้น (เช่น
"simpleTable1") ด้วย "list" คลิกรีเฟรชในตัวอย่างเทมเพลต
ทำขั้นตอนนี้ซ้ำกับอินพุตข้อความและเรียกว่า "array" ซึ่งเป็นช่องทำเครื่องหมาย 2 ช่อง
ชื่อว่า "use_array" และ "sort" และการป้อนข้อความชื่อ "delimiter"
สำหรับ "delimiter" ให้กำหนดค่าเริ่มต้นเป็น "," โดยคลิกไอคอนรูปเฟือง
สลับ "ค่าเริ่มต้น" เป็นเปิด แล้วกรอกค่าเริ่มต้นใหม่
ฟิลด์ป้อนข้อมูล
คลิกแท็บโค้ด แล้วป้อน JavaScript ที่ทำแซนด์บ็อกซ์ในตัวแก้ไข ดังนี้
var input = data.array;
if (!data.use_array) {
input = [];
for (var i = 0; i < data.list.length; i++) {
input.push(data.list[i].values);
}
}
if (data.sort) {
input.sort();
}
return input.join(data.delimiter || ',');
โค้ดสำหรับตัวแปรนี้ค่อนข้างตรงไปตรงมา แต่มี
ที่เราอยากแจ้งให้ทราบ
data
ช่อง
มีช่อง 2-3 ช่องที่กำลังเข้าถึงจากนอก data
ส่วนกลาง
data
จะมีค่าที่คุณตั้งค่าไว้ในขั้นตอนก่อนหน้า
ด้วยเหตุนี้ เราจึงสามารถเข้าถึง data.use_array
, data.sort
data.list
และ data.delimiter
delimiter
จะได้รับการตั้งค่าเป็นค่าเริ่มต้น ","
หาก data.delimiter
คือ
ไม่ได้ให้ไว้ คุณควรกำหนดค่าเริ่มต้นให้กับช่องในกรณีต่อไปนี้
ที่เหมาะสม ซึ่งจะช่วยให้ผู้ใช้ใช้ตัวแปรได้ง่ายขึ้น
เนื่องจากไม่จำเป็นต้องกรอกทุกฟิลด์เพื่อใช้
ตัวแปร
คลิกบันทึกเพื่อบันทึกความคืบหน้า การดำเนินการนี้จะโหลดสิทธิ์ทั้งหมดที่ตรวจพบ
ลงในเครื่องมือแก้ไขเทมเพลต
API เทมเพลตบางรายการมีสิทธิ์ที่เชื่อมโยงอยู่ ซึ่งจะกำหนดว่าสิ่งใด
ทำได้หรือไม่ได้ เมื่อคุณใช้ API เทมเพลต เช่น sendPixel
ใน
Tag Manager จะแสดงสิทธิ์ที่เกี่ยวข้องในสิทธิ์
ในแท็บตัวอย่างเทมเพลต ให้เพิ่มค่าสำหรับ "ค่า" ป้อน ให้คลิก
เรียกใช้โค้ด และดูคอนโซลเพื่อดูเอาต์พุตสำหรับตัวแปรของคุณ
หากมีข้อผิดพลาดเกิดขึ้น ข้อผิดพลาดนั้นจะปรากฏในหน้าต่างคอนโซล
คลิกบันทึก และปิดเครื่องมือแก้ไขเทมเพลต
และเทมเพลตตัวแปรก็พร้อมใช้งานแล้ว
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[[["\u003cp\u003eThis guide provides step-by-step instructions to create a custom variable template in Google Tag Manager.\u003c/p\u003e\n"],["\u003cp\u003eThis template transforms an array of values into a comma-separated string using JavaScript code within the template.\u003c/p\u003e\n"],["\u003cp\u003eThe template utilizes fields like 'list', 'array', 'use_array', 'sort', and 'delimiter' to customize its functionality.\u003c/p\u003e\n"],["\u003cp\u003eUsers can preview and test the template using the 'Template Preview' and 'Console' sections within the editor.\u003c/p\u003e\n"],["\u003cp\u003eOnce saved, the custom variable template is ready to be implemented within Google Tag Manager.\u003c/p\u003e\n"]]],["To create a variable template that returns a comma-delimited string from an array, start by creating a new template, defining its name and description in the *Info* tab. Then, in the *Fields* tab, add fields for a simple table (\"list\"), text input (\"array\"), checkboxes (\"use_array\", \"sort\"), and text input (\"delimiter\"). In the *Code* tab, enter sandboxed JavaScript to process these fields, joining the array with the specified delimiter, and save. Then preview the result and save the variable.\n"],null,["# Create a custom variable\n\nThis guide will walk you through how to create a custom variable template. When\nthis variable is used, it will take an array of values and return them as a\ncomma delimited string.\n\n1. To begin your first variable template, click Templates in the left\n navigation and click the *New* button under the *Variable Templates*\n section.\n\n2. In the *Info* tab, define the variable's *Name* and *Description*.\n\n **Name** is what will be presented to users when they go to implement this\n variable throughout the Tag Manager user interface.\n\n **Description** is just what it sounds like - a brief (200 characters or\n less) description of what this variable does.\n3. Click *Refresh* to preview your template.\n\n To the right of the field inputs, there is a *Template Preview* window.\n Every time a change is made in the editor, the **Refresh** button will\n appear. Click Refresh to see what your changes do to the appearance of your\n variable.\n4. Click *Fields* to add fields to your variable template.\n\n The Template Editor's **Fields** tab lets you create and edit fields in the\n variable template. Fields are used to enter custom data, such as an account\n ID. You can add the standard form elements like text fields, drop down\n menus, radio buttons, and checkboxes.\n5. Click *Add Field* and select *Simple table* . Replace the default name (e.g.\n *\"simpleTable1\"* ) with *\"list\"* . In the *Template Preview* , click *Refresh*.\n\n Repeat this step for a *Text Input* and call it *\"array\"* , two *Checkboxes*\n called \"*use_array* \" and *\"sort\"* , and a *Text Input* called *\"delimiter\"* .\n For *\"delimiter\"* , give it a default value of \",\" by clicking the gear icon,\n toggling *\"Default value\"* to on, and then filling in the new Default Value\n input field.\n6. Click the *Code* tab and enter sandboxed JavaScript in the editor:\n\n var input = data.array;\n\n if (!data.use_array) {\n input = [];\n for (var i = 0; i \u003c data.list.length; i++) {\n input.push(data.list[i].values);\n }\n }\n\n if (data.sort) {\n input.sort();\n }\n\n return input.join(data.delimiter || ',');\n\n The code for this variable is fairly straightforward, but there are a few\n things worth pointing out.\n - `data` fields.\n\n There are a few fields that are being accessed off of the `data` global.\n `data` will contain the values that you set up in the previous step.\n That's why we're able to access `data.use_array`, `data.sort`,\n `data.list`, and `data.delimiter`.\n - `delimiter` is set to a default value of `\",\"` if `data.delimiter` is\n not provided. It's a good practice to set default values for a field if\n one makes sense. This makes it easier for users to use the variable\n template, since they don't have to fill out every field in order to use\n the variable.\n\n7. Click *Save* to save your progress. This will load any detected permissions\n into the Template Editor.\n\n Some Template APIs have [permissions](/tag-platform/tag-manager/templates/permissions) associated with them that dictate what\n they can or cannot do. When you use a template API such as `sendPixel` in\n your code, Tag Manager will show relevant permissions in the **Permissions**\n tab.\n8. In the *Template Preview* tab, add some values for the \"values\" input, click\n **Run Code** , and look at the *Console* to see the output for your variable.\n\n If there are any errors, they will appear in the **Console** window.\n9. Click *Save*, and close the Template Editor\n\n The variable template should now be ready for use."]]