A
A
amazinginternetsites2021-09-09 15:55:25
JavaScript
amazinginternetsites, 2021-09-09 15:55:25

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: '/' });
}


Result - utm from the first link with utm is written into cookies, if you change them, then new utm is not displayed. The previous ones are displayed.

Removing the conditions leads to the fact that when you re-enter the site, there are no values ​​in the cookie.


The method used was https://jennamolby.com/how-to-use-cookies-to-captu...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Derepko, 2021-09-10
@uDenX

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') == "")

You check for the absence of cookies, and if there are none, then save them. You also need to overwrite if they already exist (unless of course I understood correctly)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question