APIs & Integrations

Kaythlin_Das
Member

Gclid tracking via cookie

SOLVE

Hi there,

I'm looking into a solution to track the Gclid parameter (and potentially other UTM parameters) via the cookie and hidden field in Hubspot.

I've found the following post on GitHub that describes a good solution: https://gist.github.com/jakebellacera/54d30aa662b706aecf633664b96c60a6

It suggests implementing this code on every page AFTER the HubSpot tracking code.

I have two questions here:

  • We've implemented the HubSpot tracking code via GTM and therefore don't have the HS tracking code in our CMS directly. It's therefore unclear to me where we should install the Gclid tracking code. Can we still install it like it is (into our CMS or GTM?) or do I first need to replace the GTM tag by installing the initial HS tracking code on all pages and then paste this code after HS tracking code?
  • Would I be able to expand this code in order to track other UTM parameters via the cookie, are there examples of code that I can install?

*** Important note, I'm not a developer and don't have much coding skills :smiley: **

Many thanks!

0 Upvotes
1 Accepted solution
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

Gclid tracking via cookie

SOLVE

Hi @Kaythlin_Das, that github repo you linked is an external resource that we can't really support as it's not maintained by us and might be prone to break. That said, if you use GTM, you should be at least loading all your scripts at some location, so conceivably adding the code below that line would be your best bet. I did find an old forum post on this topic that links to an article here so you may want to check that out as well.

For your second question, this blog post might help out with using utm parameters in HubSpot: https://blog.hubspot.com/marketing/what-are-utm-tracking-codes-ht

Hope this helps set you on the right path :slight_smile:

View solution in original post

0 Upvotes
3 Replies 3
Danielle1
Participant

Gclid tracking via cookie

SOLVE

I figured out how to store and capture the GCLID using the code below in Tagmanager for contacts in Hubspot. However, I still need code to do the same thing for the MSCLKID (Microsoft Click ID for Microsoft Ads). If you know how to fix this problem, please paste the code for tagmanager in the reply.

 

If you need help with store the GCLID, paste the code below into tagmanager using all pages as the trigger.

 

<script>


function getParam(p) {
var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function getExpiryRecord(value) {
var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds

var expiryDate = new Date().getTime() + expiryPeriod;
return {
value: value,
expiryDate: expiryDate
};
}

function addGclid() {
var gclidParam = getParam('gclid');
var gclidFormFields = ['gclid_field', 'foobar']; // all possible gclid form field ids here
var gclidRecord = null;
var currGclidFormField;

var gclsrcParam = getParam('gclsrc');
var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1;

gclidFormFields.forEach(function (field) {
if (document.getElementById(field)) {
currGclidFormField = document.getElementById(field);
}
});

if (gclidParam && isGclsrcValid) {
gclidRecord = getExpiryRecord(gclidParam);
localStorage.setItem('gclid', JSON.stringify(gclidRecord));
}

var gclid = gclidRecord || JSON.parse(localStorage.getItem('gclid'));
var isGclidValid = gclid && new Date().getTime() < gclid.expiryDate;

if (currGclidFormField && isGclidValid) {
currGclidFormField.value = gclid.value;
}
}

window.addEventListener('load', addGclid);

</script>

 

You will also have to create a property in Hubspot called gclid and change it to hidden on your forms. 

0 Upvotes
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

Gclid tracking via cookie

SOLVE

Hi @Kaythlin_Das, that github repo you linked is an external resource that we can't really support as it's not maintained by us and might be prone to break. That said, if you use GTM, you should be at least loading all your scripts at some location, so conceivably adding the code below that line would be your best bet. I did find an old forum post on this topic that links to an article here so you may want to check that out as well.

For your second question, this blog post might help out with using utm parameters in HubSpot: https://blog.hubspot.com/marketing/what-are-utm-tracking-codes-ht

Hope this helps set you on the right path :slight_smile:

0 Upvotes
Kaythlin_Das
Member

Gclid tracking via cookie

SOLVE

Thanks @Connor_Barley! I've tested a few things and believe I got it to work correctly now :slight_smile: