Part 1: Built-in Variables
What are Built-in Variables?
Built-in Variables are pre-configured variables provided by Google Tag Manager. You must enable them before using them in Tags or Triggers.
Built-in Variable Groups:
| Group | Common Variables | Description / Use Case |
| Page Variables | Page URL, Page Hostname, Page Path, Referrer | Track the current or previous page details |
| Click Variables | Click ID, Click Classes, Click Text, Click URL, Click Element | Capture what was clicked (buttons, links, etc.) |
| Form Variables | Form ID, Form Classes, Form Target, Form URL, Form Element | Trigger tags based on form submissions |
| Scroll Variables | Scroll Depth Threshold, Scroll Direction | Track how far users scroll down a page |
| Video Variables | Video Provider, Video Status, Video URL, Video Title, Video Percent, Video Duration, Video Current Time | Used when tracking YouTube video engagement |
| Error Variables | Error Message, Error URL, Error Line, Error Stack Trace | Capture JavaScript errors on your website |
| Visibility Variables | Percent Visible, Visible Element, Visible Classes, Visible ID | Trigger tags when specific elements become visible |
| History Variables | New History Fragment, Old History Fragment, New History State, Old History State, History Source | Used in Single Page Applications (SPA) to track URL changes |
| Utilities | Event, Container ID, Container Version, Debug Mode, Random Number, Timestamp, Environment Name | Helpful for debugging, environment tracking, or randomization |
How to Enable Built-in Variables:
- Go to Variables in GTM.
- Click “Configure” (top right).
- Check the boxes for the variables you want to use.
Part 2: User-Defined Variables
What are User-Defined Variables?
User-Defined Variables are variables that you manually create in GTM to extract or transform data for use in tags, triggers, and conditions.
User-Defined Variable Types:
| Type | Description | Example Use Case |
| Constant | Fixed value you can reuse across GTM | GA4 Measurement ID, Site Name |
| Data Layer Variable | Pulls data from dataLayer pushed by your website | Order value, user ID, product name |
| Auto-Event Variable | Retrieves properties of automatically tracked events (clicks, forms, etc.) | Capture clicked element’s ID or class |
| URL Variable | Extracts parts of the current page URL (hostname, path, query string) | Pull UTM parameters or page path |
| First Party Cookie | Reads a value stored in a browser cookie | Capture a session ID or language preference |
| JavaScript Variable | Accesses JavaScript values directly from the website’s global objects | Get window.location.pathname or other JS-based values |
| DOM Element | Gets content from an HTML element using CSS selectors | Capture <h1> title text or product name on the page |
| Lookup Table | Maps one value to another (simple value replacement) | Translate plan IDs to human-readable names |
| Regex Table | Similar to Lookup Table but uses regular expressions for complex matching | Map multiple URL structures to one standard value |
| Custom JavaScript | Lets you write your own JavaScript function that returns a value | Calculate values dynamically, combine logic, or return booleans |
How to Create a User-Defined Variable:
- Go to Variables in GTM.
- Click “New” under User-Defined Variables.
- Choose the variable type.
- Configure it as needed.
- Name your variable clearly and click Save.
Note:
- Use Preview mode to see the actual value of variables when testing.
- Label your variables clearly (e.g., DLV – Order Value or JS – Pathname) for easier management.
- Combine Lookup Tables and Variables to organize complex tracking logic.