建立自訂變數
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本指南將逐步說明如何建立自訂變數範本。時間
這個變數會擷取值陣列,並將其做為
以半形逗號分隔的字串。
如要開始建立第一個變數範本,請點選左側的「範本」
然後按一下「變數範本」下方的「新增」按鈕
專區。
在「Info」分頁中,定義變數的「Name」和「Description」。
名稱是導入程式碼時向使用者顯示的內容
變數,
「說明」顧名思義,就是簡短的「說明」(200 個半形字元或
。
按一下「重新整理」即可預覽範本。
欄位輸入的右側會顯示「Template Preview」視窗。
每次變更編輯器時,「重新整理」按鈕都會
顯示。按一下 [重新整理] ,看看您對表格外觀
變數。
按一下「欄位」,在變數範本中新增欄位。
範本編輯器的「欄位」分頁可讓您建立及編輯
變數範本欄位可用來輸入自訂資料,例如帳戶
編號。你可以新增標準表單元素,例如文字欄位、下拉式選單
選單、圓形按鈕和核取方塊
按一下「新增欄位」,然後選取「簡易表格」。替換預設名稱 (例如
"simpleTable1") 取代為 "list"。在「Template Preview」中,按一下「Refresh」。
對文字輸入重複這個步驟,並將其命名為「array」和兩個核取方塊。
稱為「use_array」和 "sort",以及名為 "分隔符號" 的文字輸入內容。
如要設定 "分隔符號,則請使用預設值「,」。方法是按一下齒輪圖示
將「預設值」切換為開啟,然後填入新的預設值
輸入欄位
按一下「Code」分頁標籤,然後在編輯器中輸入採用沙箱機制的 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
欄位。
系統正從全域 data
存取部分欄位。
data
會包含您在上一步設定的值。
因此,我們可以存取「data.use_array
」、「data.sort
」、
data.list
和data.delimiter
。
如果 data.delimiter
是 delimiter
,則將設為 ","
的預設值
沒有提供。建議您考慮為欄位設定預設值,
十分合理如此一來,使用者就能更輕鬆地使用變數
範本,因為他們不必填寫每個欄位
變數。
按一下「Save」(儲存) 即可儲存進度。這會載入所有偵測到的權限
複製到「範本編輯器」中
部分 Template API 具備相關聯的權限,而這些權限會決定哪些內容
能夠或不能執行的作業使用範本 API 時,如 sendPixel
這樣一來,代碼管理工具會在權限頁面顯示相關權限
分頁。
在「範本預覽」分頁中,新增一些「值」的值。輸入,按一下
執行程式碼,然後查看控制台,查看變數的輸出內容。
如有任何錯誤,將會顯示在「Console」視窗中。
按一下「儲存」,然後關閉範本編輯器
變數範本現在應已可供使用。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\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."]]