Back to Blog
GTM Variables Explained: Built-In, User-Defined, and Data Layer
Engineering
Sep 30, 2025
Ludde

GTM Variables Explained: Built-In, User-Defined, and Data Layer

The complete guide to Google Tag Manager variables. Extract a DOM element text using a custom Javascript variable or grab a URL query variable in seconds.

Variables in Google Tag Manager are containers that hold values — a page URL, a click text, a product price, a user ID. They're used inside tags (to send data) and triggers (to decide when tags fire). Understanding variables is the difference between basic GTM usage and precise, powerful tracking.

The Three Types of GTM Variables

  • Built-In Variables: Pre-configured by Google. Capture page URL, click data, form data, video interactions. Must be enabled before use.
  • User-Defined Variables: Custom variables you create. Can reference the data layer, DOM elements, cookies, JavaScript, lookup tables, and more.
  • Event Data Variables: Used in server-side GTM to access event data sent from the client container.

Built-In Variables You Should Always Enable

Go to Variables → Configure and enable these:

  • Page Variables: Page URL, Page Hostname, Page Path, Referrer
  • Click Variables: Click Element, Click Classes, Click ID, Click URL, Click Text
  • Form Variables: Form Element, Form Classes, Form ID, Form URL
  • Utility: Container ID, Container Version, Debug Mode, HTML ID

User-Defined Variable Types (And When to Use Each)

Data Layer Variable — The most important type

Reads values from the dataLayer object. If your developer pushes dataLayer.push({user_type: 'premium'}), create a Data Layer Variable with key user_type to access this value in tags and triggers.

JavaScript Variable

Reads a global JavaScript variable. Example: if your site sets window.userId = '12345', a JavaScript Variable with key userId captures it. ⚠️ This reads the variable at the moment of evaluation, which can lead to timing issues.

Custom JavaScript

Runs a function and returns the result. Useful for computed values like extracting a product ID from a URL path or formatting a date. Always wrap in an anonymous function: function() { return ...; }

Lookup Table

Maps input values to output values. Example: map page paths to content categories — /blog/* → "Blog", /products/* → "Products". Great for enriching GA4 events with human-readable labels.

RegExp Table

Like a lookup table but with regex matching. More powerful for pattern-based mapping where exact matches aren't possible.

1st Party Cookie

Reads a first-party cookie value. Useful for grabbing consent status, A/B test variants, or user preferences stored in cookies.

DOM Element

Reads the content of an HTML element by ID or CSS selector. Example: capture the text of a heading or the value of a form field. Use sparingly — DOM reads are fragile and break when layouts change.

5 Must-Have Custom Variables

  1. Content Group Variable: Lookup Table mapping page paths to content categories (Blog, Product, Support, etc.)
  2. Logged-In Status: Data Layer Variable reading user_logged_in (true/false)
  3. User Type: Data Layer Variable reading user_type (free, premium, enterprise)
  4. Page Type: Lookup Table or Custom JavaScript categorizing pages (homepage, PDP, PLP, checkout)
  5. Debug Mode Flag: Custom JavaScript that checks for a URL parameter like ?debug=true

Variable Scoping and Timing

Variables are evaluated at the moment they're needed, not when defined. This matters for Data Layer Variables. If your data layer push happens after the tag fires, the variable will be empty. Use custom event triggers to ensure proper timing:

  1. Developer pushes event: dataLayer.push({event: 'user_data_ready', user_type: 'premium'})
  2. Create a Custom Event trigger for user_data_ready
  3. Attach your tag to this trigger — now the variable is guaranteed to have a value

GTM Variable Check

NiceLookingData audits your GTM container for orphaned variables, data layer configuration issues, and missing variable connections. Run a free GTM audit.

Free Tool

Check your GTM container for free

Upload your GTM export or connect live. Our auditor checks 27+ best practices and gives you actionable fixes.

Thanks for reading!