Gclid with auto hubspot parameters

Hello,

I’m using Hubspot and Google Ads together.

Hubspot added his paramaters like shown here: Ad tracking in HubSpot

However, by doing this, Hubspot is removing the default Gclid param.

Is there a way to keep the Gclid on my ads?

Hello @RobinSwapcard

Currently, the GCLID value from Google is not passed to Hubspot.

If your goal is to pass the GCLID value to Hubspot, it is possible to do via the cookie and using a hidden form field on your conversion form.

This external blog post describes how to add the GCLID to your cookies via javascript and add it to your forms as a hidden field.

I was also able to find more about it in this Community post here.

Kindly,

Pam

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 storing 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.

Thanks for sharing @Danielle1!