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
Read the raw price from the page
The samples below carry their raw text in adata-raw-priceattribute. Inside your function, read it withdocument.querySelector('[data-raw-price]')(on the real site you would pass in a DOM Element variable holding that string).Create the Custom JavaScript variable
Go to Variables → New and choose Custom JavaScript. It must be an anonymous function thatreturns a value:function() { ... return cleaned; }.Strip symbols and separators, treat Free as 0
If the text isFree, return0. Otherwise remove everything except digits and the decimal point, then callNumber()on the result. Watch the European2.999,00format: the dot is a thousands separator and the comma is the decimal, so it should become2999, not2.999.Name it and save
Name itJS - clean priceand Save. No tag is needed for this lesson, the variable is the deliverable; on the real site you would send it as the GA4valueparameter.
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.
- In your GTM, click Preview.
- Paste the live URL above and click Connect.
- Interact with the live page and watch your tag fire in Tag Assistant.
What you should expect to see
{{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