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

Lesson 4 of 6

URL & Query Variables

① 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 built-in Page URL gives you the whole address, but often you want one piece of it. The URL variable carves the URL into parts, protocol, host, port, path, query and fragment, and hands back just the slice you ask for.

Set its Component Type to Query and a Query Key and it returns a single parameter's value. That's how attribution works: read utm_source from the landing URL to know which campaign sent a visitor. Leave the Query Key blank to grab the entire query string instead.

Goal

Build a URL variable (Component “Query”, Query Key utm_source) that returns the campaign source.

Build it in GTM

  1. Get UTMs onto the URL

    On the live page, click Add UTM parameters (real visitors arrive with them already on the link). The URL now carries ?utm_source=newsletter and friends.
  2. Create the URL variable

    Go to Variables → New and choose URL.
  3. Pick the Query component

    Set Component Type to Query. This tells GTM to return one query parameter rather than the whole URL or just the path.
  4. Name the parameter to read

    In Query Key, type utm_source, the name before the =, not its value. Name the variable URL - utm_source and Save. (Leave Query Key blank to capture the whole query string.)

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

With ?utm_source=newsletter on the URL, the Variables tab shows {{URL - utm_source}} = newsletter. Remove the parameter and it goes empty, which is exactly how you'd branch a tag on campaign source.

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