Answer the question
In order to leave comments, you need to log in
How to write last utm values in cookies and not first ones?
I connected 2 files to the site to work with cookies.
1. jquery.cookie.js
2. The second file contains
// Parse the URL
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');
if($.cookie('utm_source') == null || $.cookie('utm_source') == "") {
$.cookie('utm_source', source, {expires: 365, path: '/' });
}
if($.cookie('utm_medium') == null || $.cookie('utm_medium') == "" || $.cookie('utm_medium') == null || $.cookie('utm_medium') == "") {
$.cookie('utm_medium', medium, {expires: 365, path: '/' });
}
if($.cookie('utm_campaign') == null || $.cookie('utm_campaign') == "") {
$.cookie('utm_campaign', campaign, {expires: 365, path: '/' });
}
if($.cookie('utm_content') == null || $.cookie('utm_content') == "") {
$.cookie('utm_content', content, {expires: 365, path: '/' });
}
if($.cookie('utm_term') == null || $.cookie('utm_term') == "") {
$.cookie('utm_term', term, {expires: 365, path: '/' });
}
Answer the question
In order to leave comments, you need to log in
Result - utm from the first link with utm is written into cookies, if you change them, then new utm is not displayed.
if($.cookie('utm_term') == null || $.cookie('utm_term') == "")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question