Capture GCLID On Website & Hidden Form | Community
Skip to main content
New Participant
April 8, 2021
Solved

Capture GCLID On Website & Hidden Form

  • April 8, 2021
  • 1 reply
  • 11386 views

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!

Best answer by SanfordWhiteman

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.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
April 8, 2021

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.

New Participant
May 12, 2021

Hi Sanford, thank you for the suggestion, but when I try and add that code to my Google Tag Manager I get the error message:

 

"This language feature is only supported for ECMASCRIPT_2015 mode or better: const declaration."

Do you know if something should be changed if you are trying to add this to Google Tag Manager? I would like to add this sitewide so it works on every form if possible. 

 

 

SanfordWhiteman
New Participant
May 12, 2021

Do you need to support IE 10 or lower?