Home / Admin / Set a cookie if the URL contains specific UTM parameters.
Duplicate Snippet

Embed Snippet on Your Site

Set a cookie if the URL contains specific UTM parameters.

The below script checks the URL for specific UTM parameters and if found, saves a cookie allowing you to target the cookie in your campaign.

Code Preview
js
function setUTMCookie() {
    // Search the URL
    var url = window.location.search;
    // Store the UTMs we're searching for in a variable - update the UTM value based on the UTM parameter(s) you want to save as a cookie
    var emailCampaignUTMs = '?utm_medium=email';
    // 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 sstring 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();

Comments

Add a Comment