Abstract blue tech illustration with geometric shapes and circuit-like lines

Variables in Google Tag Manager | Complete Guide with Examples

Tags and triggers often get the spotlight, but variables make your GTM setup flexible, reliable, and future-proof. This guide covers:

  1. What variables are in GTM
  2. Built-in versus user-defined variables
  3. Creating and using variables step by step
  4. Event parameters for GA4 purchase tracking
  5. 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.

Google Tag Manager variables overview with enabled Click and Form variables in the Default Workspace

Built-in GTM variables that can be enabled via the Variables screen.

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 example ecommerce.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

Google Tag Manager variables overview showing built-in variables and a user-defined Data Layer Variable ecommerce.purchase.value

User-defined Data Layer Variable alongside built-in variables.

3. Create and Use Variables

3.1 Enable Relevant Built-ins

  1. Go to Variables → Configure
  2. 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)

Google Tag Manager Data Layer Variable configuration for ecommerce.purchase.value

Example of a Data Layer Variable configuration for ecommerce.purchase.value. Similar DLVs can be created for currency and transaction_id.

3.3 Use the DLVs in a GA4 Purchase Tag

  1. Tags → New → Google Analytics: GA4 Event
  2. Configuration Tag: your GA4 configuration tag
  3. Event Name: purchase
  4. 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)
  5. Trigger: Custom Event with purchase as the event name

Google Tag Manager GA4 Purchase event configuration with event name and parameters such as value and currency highlighted

GA4 Purchase tag with key parameters mapped from DLVs.

Google Tag Manager GA4 Purchase event configuration with custom event trigger purchase

Custom Event trigger that fires the purchase tag.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top