Kickflip Analytics

Professional analytics consulting from an unprofessional skateboarder.

GTM Tag Temaplate: GTM Atrribution Cookie (v0.3) hero image

GTM Tag Temaplate: GTM Atrribution Cookie (v0.3)

Published: May 23, 2025 | Updated: August 5, 2025
Google Tag Manager
Templates

A lightweight GTM tag template that captures UTM parameters, click IDs and referrer data, then stores them in a first-party cookie and localStorage for consistent attribution across sessions. Simple setup, reliable tracking, and optional dataLayer pushes.


Changelog

Version Changes
v0.1 - Initial release with support for core UTM parameters (utm_source, utm_medium, utm_campaign, utm_content, and utm_term)
- Captured core click IDs (gclid, fbclid, msclkid) with option to add in custom list
- Optional localStorage backup with cookie-localStorage sync functionality
- Advanced options for overriding cookie settings
- Optional browser console logs
v0.2 - Added support for additional UTM parameters (utm_id, utm_source_platform, utm_creative_format, utm_marketing_tactic)
- Added optional DataLayer push feature (with configurable event name)
v0.3 (current) - Added support for capturing referrer info (referrer.full and referrer.domain)
- Added optional attribution update on referrers with no UTMs or Click IDs in the URL
future plans - Optional custom mapping for traffic source/medium by click ID
- TBC, let me know!

Why I Built It

If you’ve ever struggled to keep your campaign attribution consistent across sessions, you’ll know how messy things can get when UTM parameters, click IDs and referral details vanish after the first page load. That’s why I built a Google Tag Manager (GTM) custom tag template designed to capture, store, and sync attribution data automatically across pages and sessions.

This means valuable information like utm_campaign, gclid, or fbclid can be lost before they ever reach your analytics tools or backend systems. My template fixes that by writing attribution data into a first-party cookie (and optionally localStorage), making it available across pages, sessions, and tools.

Why It’s Useful

Beyond analytics, this attribution persistence is especially valuable for lead generation workflows. For example, you can inject the stored values into hidden form fields so that when a visitor converts, the campaign and ad click details are captured alongside their lead details.

This makes it much easier to connect marketing spend to pipeline and revenue, enrich CRM records with source data, and optimise campaigns based on actual downstream performance.

What It Captures

The template automatically collects:

  • UTM parameters – including extended fields like utm_id, utm_source_platform, and utm_marketing_tactic.
  • Ad click IDs – Google (gclid), Meta (fbclid), Microsoft (msclkid), plus any custom IDs you want.
  • Referrer details – both the full URL and domain.

This data is formatted into a JSON object, which can be:

  • Stored in a cookie (or localStorage as backup).
  • Synced between cookie and localStorage for continuity.
  • Optionally pushed to the dataLayer as an event for GA4 or other GTM workflows.

Here’s what the stored attribution object looks like in the cookie and/or localStorage:

Example JSON
{
  "utm_source": "google",
  "utm_medium": "cpc",
  "utm_campaign": "spring_sale",
  "click_id": {
    "type": "gclid",
    "value": "abc123"
  },
  "referrer": {
    "full": "https://example.com/page",
    "domain": "example.com"
  }
}

Setup

You can either:

  1. Import the prebuilt GTM container – fast and ready to go with template, tag, and triggers.
  2. Add the template manually – paste the code, configure options (like cookie lifetime, ignored domains, or event names), and publish.

Whatever way you choose to go, you can get the code and more details on GitHub, and let me know what features you’d like to see in future versions.