Skip to content

Instantly share code, notes, and snippets.

@codiller
Last active April 2, 2019 01:09
Show Gist options
  • Save codiller/1cd4970742b97091115b047a6e00cc05 to your computer and use it in GitHub Desktop.
Save codiller/1cd4970742b97091115b047a6e00cc05 to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($){
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
var content = getParameterByName('utm_content');
var term = getParameterByName('utm_term');
var email = getParameterByName('email');
var initialReferrer = document.referrer;
var lastReferrer = document.referrer;
var initialPage = window.location.pathname;
var sourceCookie = Cookies.get('utm_source');
var mediumCookie = Cookies.get('utm_medium');
var campaignCookie = Cookies.get('utm_campaign');
var contentCookie = Cookies.get('utm_content');
var termCookie = Cookies.get('utm_term');
var emailCookie = Cookies.get('email');
var initialReferrerCookie = Cookies.get('initial_referrer');
var initialPageCookie = Cookies.get('initial_page');
// Set the cookies
if( sourceCookie === undefined && source !== '' ) {
Cookies.set('utm_source', source, { expires: 30 });
}
if( mediumCookie === undefined && medium !== '' ) {
Cookies.set('utm_medium', medium, { expires: 30 });
}
if( campaignCookie === undefined && campaign !== '' ) {
Cookies.set('utm_campaign', campaign, { expires: 30 });
}
if( contentCookie === undefined && content !== '' ) {
Cookies.set('utm_content', content, { expires: 30 });
}
if( termCookie === undefined && term !== '' ) {
Cookies.set('utm_term', term, { expires: 30 });
}
if( emailCookie === undefined && email !== '' ) {
Cookies.set('email', email, { expires: 30 });
}
if( initialReferrerCookie === undefined && initialReferrer !== '' ) {
Cookies.set('initial_referrer', initialReferrer, { expires: 30 });
}
if( lastReferrer !== '' ) {
Cookies.set('last_referrer', lastReferrer, { expires: 30 });
}
if( initialPageCookie === undefined && initialPage !== '' ) {
Cookies.set('initial_page', initialPage, { expires: 30 });
}
$('.initial_landing_page').val(Cookies.get('initial_page'));
$('.referrer').val(Cookies.get('initial_referrer'));
$('.last_referrer').val(Cookies.get('last_referrer'));
$('.utm_campaign').val(Cookies.get('utm_campaign'));
$('.utm_content').val(Cookies.get('utm_content'));
$('.utm_term').val(Cookies.get('utm_term'));
$('.utm_source').val(Cookies.get('utm_source'));
$('.utm_medium').val(Cookies.get('utm_medium'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment