SOLVED

Capture GCLID On Website & Hidden Form

Go to solution
maryclayton
Level 1

Capture GCLID On Website & Hidden Form

Hi,

 

I'm wanting to capture the GCLID cookie on my website then transfer that to a hidden form field within my Marketo forms, so I can get the offline conversion attribution. I have already created the hidden form, set to capture based on cookie, but I haven't figured out how to get the cookie to be stored on my website.

 

Below is the code I have been given to capture the GCLID on my website; however, it is written for form ID - not form name. Does anyone know how to rewrite the code or know other alternatives, so I can capture the information and send the cookie GCLID information to that hidden field and appropriate form name?

 

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);

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Capture GCLID On Website & Hidden Form

Ouch, that code is incredibly hard to look at.... off in so many ways.

 

It's not the first time I've seen it, luckily. Just last week somebody asked about that same snippet and we rewrote it to actually work:

 

MktoForms2 :: gclid tracker refactored

 

Add the code in the CodePen JS pane after your form embed.

 

Just set gclidFormFieldName to the Marketo (SOAP) field name you're using to store the most recent gclid. In the demo, the field name is MostRecentGclid.

View solution in original post

1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Capture GCLID On Website & Hidden Form

Ouch, that code is incredibly hard to look at.... off in so many ways.

 

It's not the first time I've seen it, luckily. Just last week somebody asked about that same snippet and we rewrote it to actually work:

 

MktoForms2 :: gclid tracker refactored

 

Add the code in the CodePen JS pane after your form embed.

 

Just set gclidFormFieldName to the Marketo (SOAP) field name you're using to store the most recent gclid. In the demo, the field name is MostRecentGclid.

View solution in original post