Skip to content

Instantly share code, notes, and snippets.

@medius
Created February 7, 2014 02:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save medius/8856352 to your computer and use it in GitHub Desktop.
Save medius/8856352 to your computer and use it in GitHub Desktop.
Capture UTM parameters in a form
<script type="text/javascript" charset="utf-8">
// From // http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
// From http://stackoverflow.com/questions/1688657/how-do-i-extract-google-analytics-campaign-data-from-their-cookie-with-javascript
function parseAnalyticsCookie() {
var values = {};
var cookie = readCookie("__utmz");
if (cookie) {
var z = cookie.split('.');
if (z.length >= 4) {
var y = z[4].split('|');
for (i=0; i<y.length; i++) {
var pair = y[i].split("=");
values[pair[0]] = pair[1];
}
}
}
return values;
}
$(document).ready(function () {
function addFormElem(paramValue, fieldName) {
var $utmEl = $("<input type='hidden' name='" + fieldName + "' value='" + paramValue + "'>");
if (paramValue != "") {
$("form").first().prepend($utmEl);
}
}
// Note: Only change the values after ":" if they are different for you
// They are the same as you setup as tags in your email marketing provider
var utmParams = {
"utmcsr" : "USOURCE",
"utmcmd" : "UMEDIUM",
"utmccn" : "UCAMPAIGN",
"utmcct" : "UCONTENT",
"utmctr" : "UTERM"
};
function parseAndSetUTM() {
for (var cookieName in parseAnalyticsCookie()) {
addFormElem(parseAnalyticsCookie()[cookieName], utmParams[cookieName]);
}
}
setTimeout(parseAndSetUTM, 500);
});
</script>
@murich
Copy link

murich commented Nov 26, 2017

Does this still works? Can't see __utmz cookie, while utm param supplied in url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment