Tags and triggers often get the spotlight, but variables make your GTM setup flexible, reliable, and future-proof. This guide covers:
- What variables are in GTM
- Built-in versus user-defined variables
- Creating and using variables step by step
- Event parameters for GA4 purchase tracking
- Best practices and common pitfalls
1. What is a Variable in GTM
In Google Tag Manager, a variable is a placeholder for a value that is resolved at runtime. Instead of hard-coding values in tags or triggers, you reference variables that can:
- Read data from the page or the
dataLayer
- Expose built-in values such as click and form details
- Transform or map values for cleaner logic
When a trigger evaluates or a tag fires, GTM substitutes each variable with its current value.
2. Built-in vs User-Defined Variables
2.1 Built-in GTM Variables
GTM provides a set of built-in variables that are available to enable in the Variables screen. Examples include:
- Page URL, Page Path, Page Hostname
- Click ID, Click Classes, Click Text
- Form Element, Form Classes, Form ID
Enable only the ones you need to keep debugging concise.
2.2 User-Defined Variables
Create user-defined variables when you need data beyond the built-ins:
- Data Layer Variable (DLV) — reads values pushed to
dataLayer
, for exampleecommerce.purchase.value
- JavaScript Variable — references a value on the page such as
window.userId
- Auto-Event Variable — extracts dynamic info from events, e.g. the clicked link URL
- Constant — stores a static value like your Measurement ID
- Lookup Table — maps one value to another for cleaner logic
- Custom JavaScript — small function to compute or transform a value when no other type fits

3. Create and Use Variables
3.1 Enable Relevant Built-ins
- Go to Variables → Configure
- Enable the variables you plan to use, for example for click and form tracking:
Click ID, Click Classes, Click Text, Form ID, Form Classes, Page URL, Page Path, Page Hostname
Tip: Enable only what you need for a cleaner Preview panel.
3.2 Create a Data Layer Variable (DLV)
If your site pushes structured e-commerce data into the dataLayer
, you can read it with Data Layer Variables (DLVs).
Example push implemented by development:
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
event: 'purchase',
ecommerce: {
purchase: {
value: 129.99,
currency: 'EUR',
transaction_id: 'ORD-48321',
items: [
{ item_id: 'SKU-123', item_name: 'Widget A', price: 79.99, quantity: 1 },
{ item_id: 'SKU-456', item_name: 'Widget B', price: 50.00, quantity: 1 }
]
}
}
});
</script>
How the DLV path works
ecommerce
→ first level (object)purchase
→ second level (nested object)value
→ third level (property inside purchase)
Together this becomes ecommerce.purchase.value
. The dot notation tells GTM to “drill down” into each level of the object.
Steps in GTM:
- Variables → New → Data Layer Variable
- Name:
DLV – ecommerce.purchase.value
- Data Layer Variable Name:
ecommerce.purchase.value
- Version: 2 (default)
- Default Value:
0
(optional, useful if value is missing)

ecommerce.purchase.value
. Similar DLVs can be created for currency
and transaction_id
.
3.3 Use the DLVs in a GA4 Purchase Tag
- Tags → New → Google Analytics: GA4 Event
- Configuration Tag: your GA4 configuration tag
- Event Name:
purchase
- Event Parameters:
value
→{{DLV – ecommerce.purchase.value}}
currency
→{{DLV – ecommerce.purchase.currency}}
transaction_id
→{{DLV – ecommerce.purchase.transaction_id}}
items
→{{DLV – ecommerce.items}}
(if provided)
- Trigger: Custom Event with
purchase
as the event name


3.4 Event Parameters for GA4 E-commerce
Event parameters are key–value pairs that give GA4 the context it needs. Without them, monetization reports remain incomplete.
Recommended parameters
Parameter | Purpose | Priority |
---|---|---|
transaction_id |
Uniquely identifies the order to prevent duplicates | Required for accurate revenue |
value |
Total order value | Required for revenue |
currency |
Currency for monetary fields | Required for revenue |
items |
Array of purchased items with IDs, names, price, quantity | Required for item-level reporting |
shipping , tax , affiliation , coupon |
Optional details that improve analysis | Recommended |
Best practice: Map parameters from the
dataLayer
rather than hard-coding values. Keep names case-consistent with GA4 documentation. Validate in GTM Preview and GA4 DebugView.
3.5 Helpful Variable Types
- Constant — for a single source of truth, for example
CONST – GA4 Measurement ID
- Lookup Table — map hostnames to environments:
dev.example.com → dev staging.example.com → staging www.example.com → prod
4. Validation Workflow
- Run Preview, complete a purchase test and inspect the Variables panel
- Confirm the GA4 Purchase tag fires once
- Verify the event and parameters in GA4 DebugView
5. Common Pitfalls
- Undefined values — check the exact DLV path and consider a Default Value
- Tag not firing — event name mismatch or timing of the dataLayer push
- Totals off — confirm tax and shipping values from the backend
- Duplicate purchases — avoid multiple pushes in single-page applications
6. Naming, Folders and Documentation
Suggested prefixes: DLV –
for Data Layer Variables, JS –
for JavaScript Variables, CONST –
for constants, LT –
for Lookup Tables.
Group variables in folders such as Variables / Ecommerce and Variables / Platform. Maintain a short README that lists each variable, its source and where it is used.
7. Conclusion
Variables connect tags and triggers to the right data and keep implementations scalable. With a clear naming convention, minimal custom code and consistent validation, your GTM setup remains robust and easier to maintain.