Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// source: https://jennamolby.com/how-to-use-cookies-to-capture-url-parameters/
(function(){
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var 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 referrer = getCookie("referrer");
let utm_campaign = getCookie("utm_campaign");
let utm_medium = getCookie("utm_medium");
let utm_source = getCookie("utm_source");
let utm_term = getCookie("utm_term");
let utm_journey = getCookie("utm_journey");
document.getElementById("referrer").value = referrer || "";
document.getElementById("utm_campaign").value = utm_campaign || "";
document.getElementById("utm_medium").value = utm_medium || "";
document.getElementById("utm_source").value = utm_source || "";
document.getElementById("utm_term").value = utm_term || "";
document.getElementById("utm_journey").value = utm_journey || "";
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment