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!
Solved! Go to Solution.
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.
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.
Thanks for supplying the script. Please could you let me know how to configure the new field within the form to ensure the GCLID is passed through correctly?
Thanks,
Mason
you don't need to add the field to the form. @SanfordWhiteman's code adds the field behind the scenes.
All you need to do is know the SOAP name of the field you are storing the GCLID in, and update this code here:
// (SOAP) name of your form field
const gclidFormFieldName = "MostRecentGclid";
Change MostRecentGclid to whatever field name you want to store the most recent GCLID in.
Cheers
Jo
Very helpful!
I've managed to do some further testing and it seems to be working successfully.
Hi @SanfordWhiteman, this code is EXCELLENT! We're trying to extend this functionality to also capture standard UTM.
Our goal is to cookie utm_campaign, utm_source, utm_medium & utm_term values for the duration on the browser session to cover the use case when a paid source may bring someone to homepage, and/or the user doesn't immediately covert on page with the referral UTM link.
Would you recommend doing this with the same code, or do you have another set of code handle cookieing this info?
Would you recommend doing this with the same code, or do you have another set of code handle cookieing this info?
You can’t do that with this same lightweight Auto-Fill code. There’s a whole other codebase we use for that.
@SanfordWhiteman Is that codebase proprietary or are you willing to share with the community?
Is that codebase proprietary or are you willing to share with the community?
It’s not proprietary (in the sense of not allowed to be used by other parties) yet I’m unable to share it due to Community rules.
Hi @SanfordWhiteman - I was reviewing this thread, and was curious if you knew the best method to set the cookie to expire at the end of the current browser session. Would setting "var expiryPeriod = 0" effectively do that?
For context, after conferring with my Marketing team, we decided that we only want to attribute the ad click to conversions directly driven through that session, so there's no need to keep the cookie active for 90 day like you have set it up in your code.
Thank you for all that you do for the community Sanford!
You can use this extended code: https://codepen.io/figureone/pen/bGqrNEp
Set storage to "sessionStorage".
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.
Do you need to support IE 10 or lower?
Thank you for responding! No we do not.
Regardless, looks like GTM won't support later-model let and const, even though they are supported in IE 11. (The error is from Closure Compiler, not from GTM per se, but GTM uses CC.)
Unfortunately, you're going to have to change these both to var wherever they appear if you deploy via GTM. I don't like this at all — frankly I would not paste this code in GTM if it has to be made old-fashioned like this.
Sanford - this code has been working for a while, thank you! Only problem I have now is that I need all scripts in GTM in order to manage cookies in a CMP for GDPR. If I swap out "var" for "cont" and "let" will the code stop working? Thanks!
No need to change anything. Just use <script type="text/gtmscript"> instead of <script>.
Hello Sanford - I just wanted to let you know that this code is working perfectly. Appreciate the help!
OK thank you very much for the information, that's very helpful.