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

Lesson 12 of 15

1St-Party Cookie 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

Cookies are how sites remember things between page loads: a logged-in flag, an A/B bucket, a campaign source. When you need one of those values in a tag, GTM has a purpose-built variable for reading them, the 1st-Party Cookie variable.

You give it the cookie's name and it returns that cookie's value, no JavaScript required. It only reads cookies set on your own domain (first-party), which is exactly the kind your site controls.

Goal

Build a 1st-Party Cookie variable that reads the ht_demo cookie.

Build it in GTM

  1. Set the demo cookie

    Click the button below to write ht_demo=42 for this origin. The variable has nothing to read until the cookie exists.
  2. Create the 1st-Party Cookie variable

    Go to Variables → New and choose 1st-Party Cookie.
  3. Point it at ht_demo

    Set Cookie Name to ht_demo, the cookie's name, not its value. Leave URI-decode cookie at its default. Name it Cookie - ht_demo and Save.
  4. Use it where you need it

    No tag is required for this lesson, the variable is the deliverable. On the real site you would send {{Cookie - ht_demo}} as a GA4 parameter or user property.

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

After you set the cookie, {{Cookie - ht_demo}} resolves to 42 in Tag Assistant's Variables tab. Before the cookie is set it returns undefined.

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