Lesson 5 of 6
JavaScript Variable
① Connect your GTM container
Paste your container ID to load it into this page. It only ever runs here.
Advanced: use a specific environment
The dataLayer is the clean way to pass data to GTM, but plenty of sites already stash useful values on a global object, window.siteData, window.dataLayer aside. A JavaScript Variable reads one of those globals directly, by dotted path, no dataLayer push required.
Don't confuse it with the Custom JavaScript variable: that one runs a function you write and returns its result, for logic. The plain JavaScript Variable just looks up a global by name. The catch is timing: the global has to exist at the moment GTM reads it, so it suits values the page sets early, like a logged-in user's type.
Goal
Build a JavaScript Variable for the global path siteData.userType and confirm it reads “member”.
Build it in GTM
Confirm the global exists
On the live page, open the browser console and typesiteData. You should see the object withuserType: "member". That's the global your variable will read.Create the JavaScript Variable
Go to Variables → New and choose JavaScript Variable, the one that reads a global, not Custom JavaScript.Point it at the dotted path
In Global Variable Name, typesiteData.userType(dotted path, nowindow.prefix). Name itJS - siteData.userTypeand Save.Reach for Custom JavaScript only when you need logic
If you needed to transform the value (uppercase it, combine two fields), you'd use a Custom JavaScript variable, afunction() { return … }instead. Here a straight read is enough.
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 - siteData.userType}} resolves to member. If it's undefined, the global hadn't loaded yet, check it's set before the tag reads it.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