One Script That Captures Every Ad Platform's Click ID

Stop adding a new capture script every time you add a channel. Here is a single script that grabs them all and stores them properly.

This post gives you one capture script that grabs every ad platform’s click ID plus your UTM parameters, stores them safely, and puts them into your forms. Build it once and adding a new channel later becomes a one line change instead of a project.

Most setups grow the other way. Google gets added, then Meta six months later, then Microsoft, each with its own snippet and its own field. By the time there are four, nobody knows which one is authoritative.

When this fits

  • You run more than one ad platform, or you will within a year.

  • Your conversions happen after the visit, so you need identifiers stored for later.

  • You would rather not revisit this every time marketing tries a new channel.

If you run exactly one platform and always will, the single platform guides are simpler: GCLID, fbclid, msclkid.

What you will build

One script, loaded on every page through Google Tag Manager, that:

  1. Reads every known click ID and UTM parameter from the URL.

  2. Writes them to first party cookies, without ever overwriting a real value with a blank one.

  3. Records first touch and last touch separately.

  4. Populates hidden form fields from those cookies.

The parameters to capture

Click IDs, one per platform:

Plus the five UTM parameters, the landing page path and the referrer. Those cover the channels that do not have a click ID at all.

How it works

Three rules make the difference between a script that works and one that quietly ruins your attribution.

Rule one: never overwrite with nothing

If the URL has no gclid, leave the existing cookie alone. A returning visitor typing your domain directly must not erase the ad click from last week.

This single rule is responsible for more recovered attribution than anything else on this page.

Rule two: first touch is written once

Keep two sets of cookies. The first touch set is written only if empty. The last touch set updates whenever a real source is present.

Now you can see that a lead first arrived from a blog post six weeks ago and came back through a branded search ad yesterday. Full version here: first touch and last touch.

Rule three: set cookies for long enough

Ninety days covers most sales cycles and matches the window Google Ads accepts uploads in. Set them from the server if you can, because Safari caps script set cookies at seven days.

Let’s implement it

  1. Create a Custom HTML tag in Google Tag Manager and set it to fire on Consent Initialization or All Pages, as early as you can.

  2. In it, loop over your list of parameter names. For each, read it from the URL. If present, write a last touch cookie, and write a first touch cookie only if none exists.

  3. Store everything under a small number of cookies rather than fifteen separate ones. A single JSON object keeps it tidy.

  4. Add hidden fields to every form, named to match your CRM fields.

  5. Populate those fields on page load from the cookie, not on submit.

  6. Push the values into the data layer too, so your tags can use them without re reading cookies.

Getting it into embedded forms

If your form lives in an iframe from HubSpot, Typeform or similar, your cookie is not readable from inside it.

Every serious form tool supports passing values in as URL parameters on the embed. Read the cookie in your page, append the values to the iframe source, and the form receives them as prefilled hidden fields. Details per tool here: hidden UTM fields on any form.

Before you finish

  • Click a real ad from each platform you run and confirm the right identifier lands on a test record.

  • Visit directly the next day and confirm nothing was overwritten.

  • Check field lengths in your CRM. These strings are long and short fields truncate silently.

  • Confirm the script runs before your forms render.

  • Test with your consent banner active, not just with it dismissed.

FAQ

Is capturing click IDs allowed under GDPR?

This is a question for your own legal advice, not for me. What I can tell you is that most implementations treat click IDs as marketing data and gate them behind marketing consent, and that your privacy policy should describe what you collect and who you share it with.

Should I store these in local storage instead of cookies?

Cookies are usually better here because they can be set from your server, which gives them a much longer life on Safari. More detail: where to store click IDs.

What if I have no CRM to send this to?

No CRM at all, and you want the simplest possible version? I wrote a full walkthrough of that for Stape using nothing but a spreadsheet: Google Ads offline conversion tracking without a CRM. Click to read it here.

Do I need all of these if I only run Google?

Capture them anyway. The cost is a few lines and a few empty fields, and the day someone tries LinkedIn you will already be collecting what you need.

Need this built properly?

One capture layer beats four half finished ones. I set up and repair tracking for ecommerce stores and lead generation businesses, so the numbers in your ad accounts match the money in your bank account.

Have a look at what I do, or book a call and we can go through your setup together.

Need accurate tracking?

Its Free

500+ Happy Clients ⭐️

Keep Reading

See exactly where you’re wasting ad spend and how better tracking can double your sales.

See exactly where you’re wasting ad spend and how better tracking can double your sales.

Tracking in 3 Hours

Tracking in 3 Hours

I Manage Everything

I Manage Everything

24/7 Expert Support

24/7 Expert Support

TESTIMONIALS

Trusted by 500+ satisfied clients

Discover how we’ve driven growth and innovation.

500+ Tracking

500+ Tracking

Adélaïde De Vos

3,922 followers

I had the pleasure to work with Abdul and I can say that he exceeded my expectations. He was professional, patient, and clear in his explanations and committed to client satisfaction. If you're looking for someone who is not only highly skilled but also reliable and committed to client satisfaction, I highly recommend Kayium!

Dean Watson

1,030 followers

Kayium setup our tracking for Google Ads and Meta Ads, which helped us get 20%+ more conversions on the same ad spend. He didn’t just handle the technical setup,he also took the time to explain why it works and how it improves results. If you’re looking for someone skilled, reliable, and committed to delivering real results, I highly recommend him!

David Friend

CEO: Profitable Sites

Kayium set up accurate tracking that helped us scale ads fast and boost conversions by over 25%. He gave us clear insight into which ads drive revenue.

Lalita Sundari

7,924 followers

I've worked with Kayium; his strategic mindset and expertise in GTM, GA4, Pixel & Server Side Tracking, are invaluable! I highly recommend Kayium!