Skip to content

Instantly share code, notes, and snippets.

@ericakfranz
Created February 23, 2022 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericakfranz/208e9df95461bcd947f5352de3349b14 to your computer and use it in GitHub Desktop.
Save ericakfranz/208e9df95461bcd947f5352de3349b14 to your computer and use it in GitHub Desktop.
Set a cookie if the URL contains specific UTM parameters.
function setUTMCookie() {
// Search the URL
var url = window.location.search;
// Store the UTMs we're searching for in a variable
var emailCampaignUTMs = '?utm_source=digg&utm_medium=email&utm_campaign=free_plus_shipping&utm_content=hhc_bundle';
// Calculate the expiration date for our cookie (1 year from the date it's set)
var cookieDate = new Date;
cookieDate.setFullYear(cookieDate.getFullYear() + 1);
// If the URL contains the UTMs we're searching for, set a custom cookie named 'omCustomCookie' and store the UTM string as the cookie value, and expire it one year from the date it is set
if (url.indexOf(emailCampaignUTMs) !== -1)
document.cookie = 'omCustomCookie=' + emailCampaignUTMs + '; expires=' + cookieDate.toGMTString() + ';';
}
// Run our cookie function
setUTMCookie();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment