Tech for Retail 2025 Workshop: From SEO to GEO – Gaining Visibility in the Era of Generative Engines

Back to blog

Google Tag Manager Variables: A Practical Guide

SEO

Discover Incremys

The 360° Next Gen SEO Platform

Request a demo
Last updated on

22/2/2026

Chapter 01

Example H2
Example H3
Example H4
Example H5
Example H6

Google Tag Manager Variables: How to Understand, Configure and Use Your Tracking Data

 

 

Introduction: connecting variables to the logic of the data layer and the main guide

 

If you already understand how the data layer works, you have the best possible foundation for grasping the role of Google Tag Manager variables. While the data layer structures and pushes information (key/value pairs and events), GTM variables are used to read those values, transform them and make them usable in your tags and triggers.

This article focuses deliberately on variables — their configuration, types, practical GA4 examples and governance — to complement the main article without repeating its detailed explanation of the data layer.

 

What are variables in Google Tag Manager? Definition, purpose and how they differ from tags and triggers

 

In GTM, a variable is an information holder that provides a reusable value to another component (a trigger, a tag, or even another variable). Variables are typically used to:

  • fire a tag using a condition (e.g. URL, clicked text, event);
  • capture information from the site (page, click, form, video, etc.);
  • populate a tag with a dynamic parameter (ID, label, value, category, etc.).

A simple click example: the built-in variable "Click URL" can return https://www.notyoursite.com and be used either as a trigger condition or as data sent to analytics.

The operational difference is clear:

  • Trigger: decides when to act (which event / under which condition);
  • Tag: performs the action (send a GA4 event, fire a conversion, etc.);
  • Variable: provides what to send (and sometimes how to compute it).

 

Built-in vs User-Defined Variables: Choosing the Right Approach for Your Goals

 

 

Built-in GTM variables: common use cases and frequent limitations

 

Built-in variables are available natively in GTM and are not customisable. They cover standard collection and condition needs, including:

  • Pages: full URL, hostname, path, referrer;
  • Clicks: clicked element, classes, ID, target, URL, text;
  • Forms: ID, classes, action URL, text;
  • Scroll and visibility (via gtm.* keys): depth, units, visible ratio, time on screen;
  • Videos (via gtm.video*): status, duration, progress, URL, title;
  • Errors: message, URL, line, debug mode.

The main limitation is less technical than methodological: these variables often describe the interface (DOM, URL, text) rather than the business context. As soon as a component changes — redesign, A/B test, wording variation — your conditions can become fragile.

 

User-defined variables: governance, clarity and maintainability

 

User-defined variables are created when no built-in variable fits, or when you want your setup to be clearer and easier to maintain. They are particularly useful to:

  • standardise a value (e.g. normalise a button label);
  • make business data explicit (e.g. lead_type, cta_location);
  • factorise a configuration (an ID, event name or mapping rule);
  • reduce duplication across similar tags.

For healthy governance, the aim is not to have a large number of variables, but to have variables that are well named, documented and reused for stable use cases.

 

A key choice: reading a value from the data layer vs generating a custom value

 

A GTM variable can:

  • read an existing value (e.g. a key pushed into the data layer);
  • generate a value (e.g. concatenate, anonymise, normalise, map or calculate).

In practice, you gain reliability when business data already exists as a stable key and GTM simply reads it. Transformation variables — Custom JavaScript, tables — then become finishing tools rather than workarounds.

 

The Key Variable Types to Master for GA4

 

 

Data Layer variable: retrieving a stable value from the data layer

 

A Data Layer variable reads a specific key (and its value) exposed in window.dataLayer. A critical point: key names are case-sensitive. If the site pushes form_name and you configure formName, the variable will return an empty value.

A common pattern is to push an event plus parameters, then map those parameters to GA4. Useful B2B keys include content_theme, page_type, funnel_stage, lead_type and form_name.

 

A good habit: align keys to a schema for a reliable analytics data layer

 

More than the variable type itself, it is consistency that makes the difference. Two simple, high-impact rules:

  • same name, same type everywhere (e.g. value stays numeric, currency remains a string);
  • one naming convention (camelCase or snake_case, without mixing).

This schema makes mapping in GA4 easier (parameters and custom dimensions) and avoids lengthy debugging sessions caused by variables that are almost — but not quite — identical.

 

Custom JavaScript variable: calculating, normalising and safeguarding values in the browser

 

A Custom JavaScript variable returns the result of a function. Use it when you need to calculate or transform a value that does not exist as-is: reading a field, extracting a URL fragment, formatting, fallback logic, and so on.

 

Guardrails: null values, performance, exceptions and team review

 

A poorly controlled Custom JavaScript variable can harm maintainability. Good practices include:

  • define fallbacks (default values) when data is missing;
  • handle exceptions with try...catch where appropriate;
  • avoid expensive operations (heavy DOM loops) to prevent performance issues;
  • have functions reviewed by both tracking and development teams to limit side effects.

 

Custom JavaScript examples: mapping, fallback and formatting

 

Three practical examples (adapt these to your own context):

  • Business fallback: if lead_type is missing, return "unknown" to avoid empty GA4 parameters.
  • Normalisation: convert a label into a stable format (lowercase, replacing spaces) to keep reporting consistent.
  • Extraction: isolate a URL segment (e.g. a section) when you cannot yet push a dedicated key to the data layer.

If you can push a clean value into the data layer, do so: Custom JavaScript should be the exception rather than the rule.

 

Constant variable: centralising an ID, parameter or naming convention

 

A Constant variable stores a fixed value that can be reused. It is ideal for:

  • centralising an identifier (e.g. a configuration ID);
  • standardising a prefix or convention (e.g. environment name, property name);
  • avoiding hard-coded duplication across multiple tags.

It is a small investment that significantly reduces the risk of inconsistencies during updates.

 

Lookup table: structured mapping (page → category, form → conversion)

 

A lookup table maps an input to an output: "if X, then Y". It is especially useful for multi-site or multi-domain setups — for example, mapping a hostname to the correct tag ID rather than maintaining several near-identical tags.

This approach reduces code duplication, lowers deployment errors and makes the container easier to read. Typical mappings include:

  • hostname → measurement ID (by domain);
  • form_name → conversion name;
  • page_type → editorial category to enrich GA4.

 

Regex table: transforming values at scale without writing code

 

A regex table transforms a value based on pattern rules. Use it to normalise inconsistent formats, for example:

  • turn facebook.fr into Facebook;
  • harmonise label variants (accents, case, suffixes).

It becomes particularly useful when data sources are inconsistent (CMS, components, manual inputs) and you want clean reporting without multiplying scripts.

 

How Variables Feed Your Tags and Triggers

 

 

Tagging plan: connecting tags, triggers and dynamic values properly

 

A robust tagging plan follows a simple logic: a stable business signal (often a pushed event) → a clear triggertags that reuse variables to populate parameters. The goal is twofold: reliability and de-duplication.

 

Variables in triggers: targeting an event, URL, click or component

 

Variables are often used as trigger conditions:

  • custom event: trigger when the event key equals a specific value (case-sensitive);
  • URL: trigger only on a specific path or fragment;
  • click: trigger based on "Click URL", "Click Text" or "Click Classes" (useful, but more fragile if the UI changes);
  • visibility / scroll: trigger based on gtm.* variables (depth, visible ratio, etc.).

When the trigger has a business impact (lead, demo request, purchase), prefer a condition based on a business event rather than an interface detail.

 

Variables in tags: filling GA4 parameters and reducing duplicates

 

For GA4, the most robust pattern is to:

  • fire a GA4 event tag on a business event (e.g. lead_submit);
  • populate parameters using Data Layer variables (e.g. form_name, lead_type, funnel_stage).

If a parameter is not standard, you will usually need to register it as a custom definition in GA4 (exact name, case-sensitive) to analyse it easily in reports.

To go deeper on measurement and reporting, see our article on Google Analytics 4.

 

Injecting values in custom HTML: inserting data without weakening security

 

In a Custom HTML tag, you can inject a GTM variable using the {{VariableName}} syntax. This lets you replace a hard-coded value (ID, label, parameter) with a value managed within GTM.

A practical example: in a multi-domain context, you can make an identifier (e.g. a pixel or configuration parameter) depend on the hostname via a lookup table, then inject it into the Custom HTML code rather than duplicating the tag for each site.

An important validation point: GTM Preview does not always show the final resolved code. To confirm the output, inspect the code actually rendered on the page.

 

Custom HTML best practices: escaping, consent and execution limits

 

Custom HTML is powerful and therefore sensitive. To keep it under control:

  • limit Custom HTML to cases where it is genuinely needed (use native templates where possible);
  • escape values when you concatenate them into code strings;
  • ensure consent is respected before running tags that require permission;
  • document the setup precisely (purpose, required variables, dependencies).

 

Variable Configuration: A Short, Robust Method in GTM

 

 

Step 1: choose the right type (Data Layer, Custom JavaScript, Constant, tables)

 

First decide whether you need to read or transform:

  • read a stable key: Data Layer variable;
  • fixed shared value: Constant;
  • explicit mapping: lookup table;
  • pattern-based normalisation: regex table;
  • specific computation: Custom JavaScript (last resort).

 

Step 2: finalise configuration (fallback rules and the format GA4 expects)

 

Before you even test, lock down:

  • the expected format (string, number, array);
  • a fallback (default value) if the data does not exist;
  • the exact case of keys read from the data layer.

 

Step 3: validate in Preview (DebugView) without polluting production data

 

Effective validation happens in two steps:

  • in GTM: Preview mode, the "Variables" tab to confirm the value appears at the right moment;
  • in GA4: DebugView to confirm the event and parameters are received correctly.

If you see inconsistent values, check the order of pushes in dataLayer via the console to confirm the data is available before the trigger fires.

 

Step 4: document for stress-free maintenance (names, descriptions, versions)

 

A useful variable today should still be understandable in six months. As a minimum:

  • use explicit names (prefix by family, e.g. DLV -, CJS -, CONST -);
  • add a description: source, format, use case, dependencies;
  • use container versioning (publish via versions) to reduce regressions.

 

B2B Examples: GA4, Conversions and Data Quality

 

 

Sending a GA4 event with parameters from the data layer (content, offer, intent)

 

A typical B2B use case: you want to segment performance not only by page, but by intent and editorial context. You can push an enriched page view on load (e.g. page type, theme, funnel stage), then reuse these keys as parameters for engagement events (CTA click, demo request).

The benefit: in GA4, you can analyse which content drives meaningful actions rather than relying solely on page views.

 

Firing a conversion on a form: conditions, de-duplication and business value

 

To make form conversions reliable:

  • trigger on a real success signal (ideally after server confirmation, or on a thank-you page following a redirect);
  • push one business event per action (avoid double sending via hard-coded tracking plus GTM);
  • add business parameters (e.g. lead type, stage, offer) via Data Layer variables.

This reduces overcounting and makes troubleshooting straightforward: one event, one trigger, clearly identified tags.

 

Normalising labels (CTAs, buttons, steps) with a lookup table or a regex table

 

In practice, labels change: "Request a demo", "Get a demo", "Speak to an expert". If you send raw labels into GA4, your reporting quickly becomes difficult to read.

A practical approach:

  • lookup table for a finite list of mappings (input → output);
  • regex table to cover variants using patterns (case, accents, suffixes, domains, etc.).

 

Connecting business data to GA4: Constant variables and mapping for segmentation

 

A Constant variable can hold a cross-cutting convention (e.g. environment, brand, segment) that you reuse as a GA4 parameter across multiple tags. Combined with a lookup table, it helps you build clean segmentations without multiplying tags: the logic stays inside the variables.

 

Making the most of an analytics data layer: naming consistency and analysis in Google Analytics 4

 

Once parameters are being sent, the decisive step is analysis: confirm that custom definitions (where needed) match the names sent exactly, then structure explorations around events and business dimensions (intent, content type, funnel stage).

 

Reliability, Compliance and Quality: Common Pitfalls with Variables

 

 

Missing values: why a variable returns "undefined" and how to safeguard

 

The most common causes are:

  • incorrect case (e.g. a misspelt key name);
  • a push at the wrong time (the value arrives after the trigger fires);
  • an invalid JavaScript object (missing quotes, incorrect syntax);
  • expecting cross-page persistence: a variable only lives on the current page unless you push it again.

To safeguard: add a fallback (Constant or JS), test in Preview, and inspect dataLayer in the console to confirm message order.

 

Website changes: avoiding breakages after redesigns, A/B tests and dynamic components

 

When a variable depends on the DOM (classes, text, structure), it can break easily. To reduce that risk:

  • push business information into the data layer as a stable key;
  • use Data Layer variables rather than DOM selectors;
  • if you must use the DOM, target stable attributes (IDs dedicated to tracking).

 

GDPR and consent: what a variable should not collect by default

 

A variable can technically read a great deal, but it should never become a catch-all collector of sensitive data. As a rule, do not push personally identifiable information (PII) or anything unnecessary for your measurement plan. Consent status also directly affects what your tags may send, so ensure it is aligned at the moment tags fire.

 

GEO Angle: Impact on Visibility in Generative AI Answers

 

 

Defining what is truly attributable and measurable in a GEO context

 

GTM tracking does not "create" visibility in generative AI answers, but it helps you measure what happens after the visit: engagement, funnel progression and conversions. With the rise of zero-click journeys (60% of searches according to Semrush, 2025), the share of traffic reaching your site may shift; when it does, it becomes crucial to assess the real value of those sessions.

To ground your analysis, you can cross-reference your results against macro benchmarks from our SEO statistics, SEA statistics and GEO statistics resources (trends, behaviours and how search interfaces are evolving).

 

Structuring reusable parameters without multiplying tags and triggers

 

In GEO as in SEO, it is tempting to stack up tags and triggers. A more robust approach is to:

  • define a small set of stable business events (e.g. key CTA click, validated lead);
  • add reusable parameters (theme, intent, funnel stage);
  • centralise rules (normalisation, mapping) in variables (lookup/regex) rather than duplicating tags.

 

Linking tracking and editorial performance: from data to action (SEO, GEO, ROI)

 

The most useful outcome is connecting content that attracts (acquisition) to content that moves users forward (engagement) and then to actions (conversion). With well-designed variables — stable keys, normalised labels — you gain an actionable view: which themes and formats drive qualified leads, and at which stage of the journey.

 

A Brief Word on Incremys

 

 

Centralising GA4 and Search Console via API to connect content, tracking and ROI

 

Once your GTM/GA4 setup is reliable (events, parameters and variables), Incremys can centralise Google Analytics and Google Search Console data via API to connect content performance, behavioural signals and business outcomes. The goal is not to replace GTM, but to make use of clean data in order to prioritise content improvements and track editorial ROI from end to end.

 

FAQ on Google Tag Manager Variables

 

 

What do Google Tag Manager variables do in practical terms?

 

They retrieve or generate a value (URL, event name, business attribute, identifier, label) so it can be used in a trigger (as a condition) or in a tag (as a parameter). They reduce hard-coding and make tracking considerably easier to maintain.

 

How do you create a user-defined variable without making the container overly complex?

 

Create it only when it has a clear, repeatable use case (ideally used at least twice), give it an explicit name, document its source and format, and prefer reading from the data layer over DOM extraction. Where possible, group transformation logic into tables (lookup/regex) rather than scripts.

 

What is the difference between a built-in GTM variable and a user-defined variable?

 

A built-in variable is provided by GTM, ready to use and not customisable. A user-defined variable is created and configurable: it can read business keys (often from the data layer) or generate values (constants, mapping, transformations, calculations).

 

When should you use a Data Layer variable rather than a Custom JavaScript variable?

 

Use a Data Layer variable whenever the value can be exposed cleanly on the website as a stable key. Reserve Custom JavaScript for cases where you genuinely need to calculate or transform a value, or manage complex fallback logic.

 

What is a Constant variable used for in a GTM container?

 

It centralises a fixed value (identifier, parameter, convention) so you can reuse it across multiple tags and avoid inconsistencies during updates.

 

What is the difference between a lookup table and a regex table?

 

A lookup table performs an exact match: "if input = X, then output = Y". A regex table applies regular-expression rules to transform values and cover variations (formats, domains, case, etc.).

 

How do you use a variable in a GA4 tag to populate event parameters?

 

In the GA4 event tag, add parameters (e.g. form_name, lead_type) and set each value to a GTM variable (often a Data Layer variable). Then verify receipt in GA4 DebugView, and if needed create a custom definition in GA4 to analyse the parameter.

 

How do you make a trigger more reliable with variables without creating false positives?

 

Base firing on a business event (the event key) rather than an interface detail, and add simple, stable conditions (e.g. lead_step = success). Avoid overly broad conditions on CSS classes that may change.

 

How do you use variables in custom HTML without creating security risks?

 

Inject only what is necessary via {{VariableName}}, avoid complex dynamic code generation, escape strings if you concatenate values, and ensure execution respects consent. Document the tag and keep its scope tight.

 

Why does my variable return "undefined" in Preview mode?

 

Typical causes include a misspelt key (case-sensitive), a push happening after the trigger fires, or an invalid data layer object. Check the Preview timeline, then use the console to inspect dataLayer and confirm message order and key presence.

 

How do you avoid duplicate events and conversions in GTM?

 

Do not send the same event both hard-coded and via GTM. On the website side, push one signal per real action (watch for double clicks, SPA behaviour and re-renders). In GTM, use one trigger per business event and clearly named, separate tags.

 

How do you test a new variable configuration without impacting production?

 

Test in GTM Preview mode, verify variable values and fired tags, then validate receipt in GA4 DebugView. Publish as a new GTM version after QA — ideally on a staging environment before production.

 

What naming conventions help keep variables maintainable over time?

 

Use type-based prefixes (e.g. DLV -, CONST -, CJS -, LT -, RT -), business-oriented names (not UI-driven), and descriptions stating the source (data layer key, built-in variable used, mapping rule, expected format).

 

How do you prepare tracking that is useful for GEO in order to measure the impact of generative AI visibility?

 

Do not try to "measure AI" directly via GTM. Instead, measure post-visit signals and qualified conversions, adding reusable parameters (theme, intent, stage) to key events. Then connect these signals to editorial performance to identify which content genuinely contributes to ROI.

To keep exploring SEO, GEO and performance measurement, visit the Incremys blog.

Discover other items

See all

Next-gen GEO/SEO starts here

Complete the form so we can contact you.

The new generation of SEO
is on!

Thank you for your request, we will get back to you as soon as possible.

Oops! Something went wrong while submitting the form.