Insights and advice for government leaders. Stay informed on the latest trends, find new solutions, or join us for an upcoming webinar. We’re here to help you drive government performance in the digital age. See our most recent resources below, or keep scrolling to view our entire resource library.
Upcoming Webinar
Where Revenue and Safety Slip Through the Cracks in Building Permits | Live on 1/28
Start Your Journey and Power Effective, Accountable Government
tag
* ppcUrlCookiePart1 and ppcUrlCookiePart2 must be called — see bottom of script
* Update ppcUrlCookiePart1 and ppcUrlCookiePart2 to match your querystring and form field names
*/
// Leave this as true to always use querystring values if they exist;
// if no querystring, will attempt to get cookie values
var ppcUseLatestValues = true;
// Function to get cookie value
function getCookie(param_name) {
var i, x, y, cookie = document.cookie.split(";");
for (i = 0; i < cookie.length; i++) {
x = cookie[i].substr(0, cookie[i].indexOf("="));
y = cookie[i].substr(cookie[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == param_name) {
return unescape(y);
}
}
}
// Function to create cookie
function setCookie(param_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; domain=opengov.com; path=/; expires=" + exdate.toUTCString());
document.cookie = param_name + "=" + c_value;
}
// Function to check if cookie exists and, if not, grab value from URL and store it
function checkCookie(param_name, param_url_name) {
var param_value = getCookie(param_name);
if ((param_value != null && param_value !== "" && param_value !== "undefined") && ppcUseLatestValues === false) {
// cookie exists and we don't want to overwrite
} else {
var pageURL = window.location.search.substring(1);
var URLVariables = pageURL.split('&');
for (var i = 0; i < URLVariables.length; i++) {
var parameterName = URLVariables[i].split('=');
if (parameterName[0] == param_url_name) {
param_value = parameterName[1].split("#")[0];
}
}
if (param_value && param_value !== "undefined") {
setCookie(param_name, param_value, 365);
}
}
}
// Store UTM & gclid cookies from querystring
function ppcUrlCookiePart1() {
var param_names = [
'utm_source;utm_source',
'utm_medium;utm_medium',
'utm_campaign;utm_campaign',
'utm_adgroup;utm_adgroup',
'utm_term;utm_term',
'utm_content;utm_content',
'gclid;gclid' // ✅ Corrected: looks for ?gclid= in URL
];
for (var i = 0; i < param_names.length; i++) {
var param_object = param_names[i].split(";");
checkCookie(param_object[0], param_object[1]);
}
}
// Grab cookie value for form injection
function mGetCookie(param_name) {
return getCookie(param_name);
}
// Fallback for non-Marketo forms (uses getElementsByName)
function mCheckCookie(param_name, param_field_name) {
var param_value = mGetCookie(param_name);
if (param_value != null && param_value !== "" && param_value !== "undefined") {
try {
var obj1 = document.getElementsByName(param_field_name);
obj1[0].value = param_value;
return true;
} catch (err) {
return false;
}
}
return false;
}
// Fallback injection for non-Marketo forms
function ppcUrlCookiePart2() {
var param_names = [
'utm_source;utm_source',
'utm_medium;utm_medium',
'utm_campaign;utm_campaign',
'utm_adgroup;utm_adgroup',
'utm_term;utm_term',
'utm_content;utm_content',
'gclid;Google Click ID'
];
for (var i = 0; i < param_names.length; i++) {
var param_object = param_names[i].split(";");
mCheckCookie(param_object[0], param_object[1]);
}
}
// Helper to get gclid from either cookie or _gcl_aw
function getGclid() {
var direct = getCookie('gclid');
if (direct) return direct;
var gclAw = getCookie('_gcl_aw');
if (!gclAw) return null;
var parts = gclAw.split('.');
return parts.length >= 4 ? parts[3] : null;
}
// 🔒 Always attempt to set cookies if params exist (even if not passed through nav)
ppcUrlCookiePart1();
// ✅ When Marketo form is ready, inject values into correct field names
try {
MktoForms2.whenReady(function (form) {
// Delay slightly in case cookies are still being set
setTimeout(function () {
const formValues = {
'utm_source__c': mGetCookie('utm_source'),
'utm_medium__c': mGetCookie('utm_medium'),
'utm_campaign__c': mGetCookie('utm_campaign'),
'utm_term__c': mGetCookie('utm_term'),
'utm_content__c': mGetCookie('utm_content'),
'Google_Click_ID__c': getGclid()
};
console.log('[Injecting values into Marketo form]', formValues);
form.setValues(formValues);
}, 250); // Delay just in case async scripts slow cookie creation
});
} catch (err) {
// If error on Marketo form, try regular fallback
ppcUrlCookiePart2();
}