Don't have one? Create one ↗
← All courses

Lesson 9 of 15

Normalize DOM Data

① Connect your GTM container

Paste your container ID to load it into this page. It only ever runs here.

Advanced: use a specific environment

GA4 wants prices as plain numbers, but pages show them as strings dressed up with currency symbols, thousands separators and words like "Free". A DOM Element variable would hand you that raw string verbatim, which GA4 cannot do math on.

So you reach for a Custom JavaScript variable: a small function that reads the raw text, strips everything that is not a digit or decimal point, and returns a clean Number. This is the workhorse pattern for cleaning up values GTM cannot reshape on its own.

Goal

Build a Custom JavaScript variable that turns a messy price string into a clean Number.

Build it in GTM

  1. Read the raw price from the page

    The samples below carry their raw text in a data-raw-price attribute. Inside your function, read it with document.querySelector('[data-raw-price]') (on the real site you would pass in a DOM Element variable holding that string).
  2. Create the Custom JavaScript variable

    Go to Variables → New and choose Custom JavaScript. It must be an anonymous function that returns a value: function() { ... return cleaned; }.
  3. Strip symbols and separators, treat Free as 0

    If the text is Free, return 0. Otherwise remove everything except digits and the decimal point, then call Number() on the result. Watch the European 2.999,00 format: the dot is a thousands separator and the comma is the decimal, so it should become 2999, not 2.999.
  4. Name it and save

    Name it JS - clean price and Save. No tag is needed for this lesson, the variable is the deliverable; on the real site you would send it as the GA4 value parameter.

Debug in Tag Assistant

Copy this lesson's live URL and paste it into GTM Preview, that is the page Tag Assistant connects to. It has the clickable elements, so this page stays clean for reading.

  1. In your GTM, click Preview.
  2. Paste the live URL above and click Connect.
  3. Interact with the live page and watch your tag fire in Tag Assistant.

What you should expect to see

In Tag Assistant's Variables tab, {{JS - clean price}} resolves to 1299 for $1,299.00, 49.5 for USD 49.50, 2999 for 2.999,00 € and 0 for Free, all numbers, never strings.

Verify your container

Built it? Export your container, Admin → Export Container, choose your workspace, then drop the JSON here to check it against this lesson.

Drop your container .json here

or browse · checked in your browser, nothing is uploaded

0 of 15 lessons complete