# Gclid tracking via cookie

**URL:** <https://community.hubspot.com/t/gclid-tracking-via-cookie/11471>\
**Category:** APIs & Integrations\
**Tags:** forms\
**Created:** [November 20, 2018, 2:48pm UTC](https://community.hubspot.com/t/gclid-tracking-via-cookie/11471 "2018-11-20T14:48:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 20, 2018, 2:48pm UTC](https://community.hubspot.com/t/gclid-tracking-via-cookie/11471/1 "2018-11-20T14:48:25Z")

</div>

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: [Simple HubSpot gclid tracking code integration · GitHub](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.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/5/e/5ece32cc997517bd0f7529e47698e55a18eaae82.png)  
\*\*

Many thanks!

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [November 20, 2018, 10:42pm UTC](https://community.hubspot.com/t/gclid-tracking-via-cookie/11471/2 "2018-11-20T22:42:12Z")

</div>

Hi [@Kaythlin\_Das](https://community.hubspot.com/u/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](https://integrate.hubspot.com/t/capture-gclid-stored-in-cookie/3092) 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: [UTM Codes: How to Create UTM Tracking URLs on Google Analytics](https://blog.hubspot.com/marketing/what-are-utm-tracking-codes-ht)

![]() [knowledge.hubspot.com](https://knowledge.hubspot.com/articles/kcs_article/reports/how-does-hubspot-categorize-visits-contacts-and-customers-in-the-sources-report)

### [Categorize sessions, contacts, and customers in the sources report](https://knowledge.hubspot.com/articles/kcs_article/reports/how-does-hubspot-categorize-visits-contacts-and-customers-in-the-sources-report)

HubSpot analyzes each session on your website and applies a series of rules to determine its source.

Hope this helps set you on the right path ![]()

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 21, 2018, 1:41pm UTC](https://community.hubspot.com/t/gclid-tracking-via-cookie/11471/3 "2018-11-21T13:41:26Z")

</div>

Thanks [@Connor\_Barley](https://community.hubspot.com/u/connor_barley)! I’ve tested a few things and believe I got it to work correctly now  
 ![slight_smile.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/3/33fb2a14544119c2351fe774141779702299e6a6.png)

---

<div class="post-metadata">

**Author:** ![Danielle1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/danielle1/32/75439_2.png) [@Danielle1](https://community.hubspot.com/u/Danielle1)\
**Post date:** [March 4, 2023, 5:33am UTC](https://community.hubspot.com/t/gclid-tracking-via-cookie/11471/4 "2023-03-04T05:33:55Z")

</div>

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.
