Analytics and Tracking

User-defined Variable

A variable you create yourself in Google Tag Manager to capture a value your tags or triggers need, such as an order total or form name.

Quick facts: User-defined Variable

Category
Analytics and Tracking
Level
Intermediate
Affects
Conversion values, transaction IDs, GA4 parameters, trigger conditions
Where to see it
Google Tag Manager (Variables and preview mode), Tag Assistant, browser developer tools
In this article4
  1. How user-defined variables work
  2. Why it matters
  3. Common mistakes
  4. How to act on it

A user-defined variable is a variable you create yourself in Google Tag Manager to pick up a specific value, such as an order total, a form name or a cookie, so your tags and triggers can use it. It sits alongside the built-in variables Google provides, which cover common values like Page URL and Click Text.

How user-defined variables work

In the Variables section of a GTM container, you click New under “User-Defined Variables”, choose a type and tell it where to find the value. Wherever you then write the variable’s name in double curly braces, for example {{DLV – order value}}, GTM swaps in the current value at the moment a tag fires or a trigger is checked.

The types used most often are:

  • Data Layer Variable: reads a value your website pushes into the data layer, such as transaction ID or basket value. This is the most reliable source because the developer controls it.
  • Constant: stores a fixed value such as a GA4 measurement ID, so you change it once rather than in every tag.
  • Lookup Table and RegEx Table: translate one value into another, for example mapping each thank-you page path to a form name.
  • 1st Party Cookie: reads a cookie on your own domain.
  • DOM Element: reads text or an attribute from the page itself.
  • Custom JavaScript: runs a short function and returns its result, for cases nothing else covers.

Why it matters

Most of the valuable data in analytics and ad platforms comes through user-defined variables. A Google Ads purchase conversion without a value, or a GA4 purchase without a transaction ID, usually means a variable is missing or returning nothing. When that happens, bidding is based on conversion counts rather than revenue, and duplicate orders slip through unnoticed.

The source matters too. Values scraped from page text break the day someone redesigns the checkout or changes “Total” to “Order total”. For a small UK shop on Shopify or WooCommerce, a redesign can silently stop revenue tracking for weeks before anyone spots that Google Ads is reporting zero sales value.

Common mistakes

  • Relying on DOM Element variables for revenue or IDs when the data layer could provide them.
  • Vague names such as “value” or “variable 3”, which make a container impossible to audit a year later.
  • Getting the data layer key wrong by a single character or capital letter, so the variable returns “undefined”.
  • Sending prices as text with a pound sign, for example “£49.99”, when platforms expect a plain number.
  • Custom JavaScript that reads personal details, such as an email field, and passes them into tags without consent or a lawful basis.

How to act on it

Agree a naming convention and stick to it. Prefixing by type, such as DLV for data layer variables, CJS for custom JavaScript and LUT for lookup tables, makes containers far easier to read. Ask your developer to put important values into the data layer rather than leaving you to scrape the page.

Test every new variable in preview mode, checking the Variables tab on the exact event where your tag fires, not on page load. Then delete variables no tag uses any more.

When conversion values look wrong, the cause is often one of these variables, and checking them is a standard part of how I set up tracking for Google Ads management.

Do and do not

Do

  • Read important values from the data layer
  • Use a clear naming convention with a type prefix
  • Test each variable in preview mode on the event where the tag fires

Do not

  • Scrape revenue or IDs from page text when the data layer could hold them
  • Send prices as text with a pound sign
  • Pass personal details into tags through Custom JavaScript

Questions people ask about this

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

Built-in variables are ready-made by Google and only need switching on, such as Page Path or Click URL. User-defined variables are ones you create and configure yourself to capture something specific to your site, such as an order value from the data layer. Most serious tracking needs both.

Why does my GTM variable show undefined?

Usually the variable is being read before the value exists, or the name does not exactly match. Check in preview mode on the event where your tag fires, not the earlier page load, and compare the data layer key letter by letter, including capitals. If the data layer never contains the value, the fix belongs with your developer.

Should I use Custom JavaScript variables?

Use them sparingly. They are useful for small tasks such as reformatting a value, but they are harder to audit and can break when the page changes. If you find yourself writing many of them, the data layer is probably missing information your developer could supply directly.

Related terms

Found this useful?

Share it, or ask an AI to summarise it

Back to the glossary

Knowing the term is the easy part

Applying it to your own site and budget is the work. Book a call and I will tell you what actually applies to you.