Skip to content

Instantly share code, notes, and snippets.

@jeffdrumgod
Last active July 20, 2021 13:44
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 jeffdrumgod/5f4cfd89ed0e49a8633826b2f8a81ed5 to your computer and use it in GitHub Desktop.
Save jeffdrumgod/5f4cfd89ed0e49a8633826b2f8a81ed5 to your computer and use it in GitHub Desktop.
Customize VTEX IPS cookie - forcing expiration time
(function vtexCustomIPSCookie() {
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else {
var expires = "";
}
var domain = window.location.hostname;
document.cookie = name + "=" + value + expires + ";domain=." + domain + ";path=/";
}
$.ajaxSetup({
beforeSend: function (xhr,settings) {
try {
if (settings.url.indexOf('Site/Track.aspx') > -1) {
var url = new URL(window.location.protocol + '//' + window.location.hostname + settings.url);
var Parceiro = url.searchParams.get("utm_source") || '';
var Midia = url.searchParams.get("utm_medium") || '';
var Campanha = url.searchParams.get("utm_campaign") || '';
if (Parceiro || Midia || Campanha) {
xhr.vtexCustomIPSCookieRequest = function() { createCookie('IPS', 'Midia=' + Midia + '&Campanha=' + Campanha + '&Parceiro=' + Parceiro, 30); };
}
}
} catch(e) {
console.error(e)
}
},
complete: function (xhr, jqxhr, textStatus) {
try {
if (xhr.vtexCustomIPSCookieRequest) {
xhr.vtexCustomIPSCookieRequest();
}
} catch (e) {
console.error(e);
}
}
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment