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

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

  1. Confirm the global exists

    On the live page, open the browser console and type siteData. You should see the object with userType: "member". That's the global your variable will read.
  2. Create the JavaScript Variable

    Go to Variables → New and choose JavaScript Variable, the one that reads a global, not Custom JavaScript.
  3. Point it at the dotted path

    In Global Variable Name, type siteData.userType (dotted path, no window. prefix). Name it JS - siteData.userType and Save.
  4. 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, a function() { 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.

  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 the Variables tab, {{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

0 of 6 lessons complete