יצירת משתנה מותאם אישית
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
במדריך הזה נסביר איך ליצור תבנית של משתנים מותאמים אישית. מתי
הוא תיקח מערך של ערכים ויחזיר אותם
מחרוזת שמופרדת בפסיקים.
כדי להתחיל את תבנית המשתנים הראשונה, לוחצים על Templates (תבניות) בצד ימין.
וללחוץ על הלחצן חדש בקטע תבניות משתנה.
.
בכרטיסייה Info (מידע), מגדירים את הערכים Name ו-Description של המשתנה.
שם הוא מה שיוצג למשתמשים כשהם ייכנסו לתוקף
משתנה בכל ממשק המשתמש של Tag Manager.
תיאור מופיע בדיוק כמו שהוא נשמע – תיאור קצר (200 תווים או
פחות) של מה שהוא עושה.
לוחצים על רענון כדי לראות תצוגה מקדימה של התבנית.
משמאל לקלט של השדה מופיע חלון תצוגה מקדימה של תבניות.
בכל פעם שמבוצע שינוי בעורך, הלחצן רענון
יופיעו. לחץ על 'רענון' כדי לראות איך השינויים שביצעת במראה
מותאם אישית.
לוחצים על שדות כדי להוסיף שדות לתבנית המשתנים.
בכרטיסייה שדות בעורך התבניות אפשר ליצור ולערוך שדות
תבנית של משתנים. השדות משמשים להזנת נתונים בהתאמה אישית, כמו חשבון
ID. אפשר להוסיף את רכיבי הטופס הרגילים, כמו שדות טקסט, באמצעות תפריט נפתח
תפריטים, לחצני בחירה ותיבות סימון.
לוחצים על הוספת שדה ובוחרים באפשרות טבלה פשוטה. מחליפים את שם ברירת המחדל (למשל,
"simpleTable1") עם "list". בתצוגה המקדימה של התבנית, לוחצים על רענון.
חוזרים על השלב הזה עבור קלט טקסט וקוראים לו "מערך", עם שתי תיבות סימון
שנקרא 'use_array' ו-"sort", וקלט טקסט שנקרא "delimiter".
בשדה 'delimiter', מזינים את ערך ברירת המחדל ',' על ידי לחיצה על סמל גלגל השיניים,
מפעילים את ההגדרה Default value (ערך ברירת המחדל) ומזינים את ערך ברירת המחדל החדש
שדה להזנת קלט.
לוחצים על הכרטיסייה קוד ומזינים JavaScript בארגז חול (sandbox) בעורך:
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
.
delimiter
מוגדר לערך ברירת המחדל של ","
אם data.delimiter
הוא
לא צוין. מומלץ להגדיר ערכי ברירת מחדל לשדה אם
הגיוני. כך למשתמשים יהיה קל יותר להשתמש במשתנה
מכיוון שהם לא צריכים למלא את כל השדות כדי להשתמש
של המשתנה.
לוחצים על Save כדי לשמור את ההתקדמות. הפעולה הזו תטען את ההרשאות שזוהו
בעורך התבניות.
לחלק מממשקי ה-API של תבניות משויכים הרשאות שקובעות את התוכן
מה הם יכולים או לא יכולים לעשות. כשמשתמשים בתבנית API כמו sendPixel
ב-
את הקוד, Tag Manager יציג הרשאות רלוונטיות בקטע הרשאות
.
בכרטיסייה תצוגה מקדימה של התבנית, מוסיפים ערכים ל'ערכים'. קלט, לחיצה
Run Code ומעיינים במסוף כדי לראות את הפלט של המשתנה.
אם יש שגיאות, הן יופיעו בחלון המסוף.
לוחצים על שמירה וסוגרים את עורך התבניות
תבנית המשתנים אמורה להיות מוכנה עכשיו לשימוש.
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 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."]]