Simply add this script into JS file and change the cookies name with your own requirements and append these cookies with Pardot form fields.
function getCookie(cname) {
let name = cname + “=”;
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(‘;’);
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ‘ ‘) {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return “”;
}
let searchParams = new URLSearchParams(window.location.search);
var d = new Date();
d.setTime(d.getTime() + (24*60*60*1000));
var expires = “expires=”+ d.toUTCString();
let source;
if( getCookie(“_utm_source”) == ” ) {
if(searchParams.has(‘utm_source’)){
//console.log(“Search Param Cookie: “, searchParams.get(‘utm_source’) );
document.cookie = “_utm_source=” + searchParams.get(‘utm_source’) + “;domain=citrusbits.com;” + expires + “;path=/”;
}
else {
let prevKey = document.referrer;
if( prevKey == ” || prevKey == undefined ) {
prevKey = “Direct Browser”;
}
else {
let prevKey = document.referrer.split(‘/’)[2].replace(‘www.’,”);
prevKey = prevKey.substr(0, prevKey.indexOf(‘.’));
prevKey = prevKey.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
}
document.cookie = “_utm_source=” + prevKey + “;domain=citrusbits.com;” + expires + “;path=/”;
}
}
if( getCookie(“_utm_medium”) == ” ) {
if(searchParams.has(‘utm_medium’)){
document.cookie = “_utm_medium=” + searchParams.get(‘utm_medium’) + “;domain=citrusbits.com;” + expires + “;path=/”;
}
else {
document.cookie = “_utm_medium=N/A;domain=citrusbits.com;” + expires + “;path=/”;
}
}
if( getCookie(“_utm_campaign”) == ” ) {
if(searchParams.has(‘utm_campaign’)){
document.cookie = “_utm_campaign=” + searchParams.get(‘utm_campaign’) + “;domain=citrusbits.com;” + expires + “;path=/”;
}
else {
document.cookie = “_utm_campaign=N/A;domain=citrusbits.com;” + expires + “;path=/”;
}
}
if( getCookie(“gclid”) == ” ) {
if(searchParams.has(‘gclid’)){
document.cookie = “_gclid=” + searchParams.get(‘gclid’) + “;domain=citrusbits.com;” + expires + “;path=/”;
}
else {
document.cookie = “_gclid=N/A;domain=citrusbits.com;” + expires + “;path=/”;
}
}
if(searchParams.has(‘utm_source’)){
source = ‘utm_source=’;
}
else {
source = ‘source=’;
}
let formFilledOn = window.location.href.split(‘?’)[0];
let formSrc = jQuery(“iframe.pardotform”).attr(“src”);
var params = ‘?’ + source + getCookie(‘_utm_source’) + ‘&utm_Medium=’ + getCookie(‘_utm_medium’) + ‘&utm_campaign=’ + getCookie(‘_utm_campaign’) + ‘&form_filled_on=’ + formFilledOn + ‘&gclid=’ + getCookie(‘_gclid’);
jQuery(“iframe.pardotform”).attr(“src”, formSrc + params);
});
Thanks for your blog, nice to read. Do not stop.