I have this code to trigger a welcome message for returning users using localstorage. But it doesn't work, when I inspected the cookies used, it doesn't even show that the cookie am using is getting stored. This is the code
jQuery(document).ready(function( $ ){
var canClick = 1;
var ga_visits = localStorage.getItem('visits');
if (ga_visits == null) ga_visits = 1;
$("body").click(function () {
if (canClick==2) {
switch(ga_visits){
case 1:
alert("Welcome to our website");
visits=parseFloat(ga_visits)+1;
continue;
case 2:
alert("Welcome to back to our website");
visits=parseFloat(ga_visits)+1;
continue;
case 3:
alert("Welcome to back to our website, You really seem to like our website");
visits=parseFloat(ga_visits)+1;
continue;
else {
canClick=canClick+1;
}
});
});
This alert message will show only when it is second click , so that the immersion doesn't break.
It seems to me that you're trying to set the value of visits directly, instead of to the localStorage. Instead of visits=parseFloat(ga_visits)+1, use localStorage.setItem('visits', parseFloat(ga_visits)+1) to set the localStorage. As for why it's not in the cookies, keep in mind that cookies != localStorage, you should check the localStorage on devtools -> application -> localStorage to check the localStorage of the current site. Also your code seems to be poorly written, not sure if it even runs. I tried to fix it here as well:
jQuery(document).ready(function( $ ){
var canClick = 1;
var ga_visits = localStorage.getItem('visits');
if (ga_visits == null) ga_visits = 1;
$("body").click(function () {
if (canClick==2) {
switch(ga_visits){
case 1:
alert("Welcome to our website");
localStorage.setItem('visits', parseFloat(ga_visits)+1);
continue;
case 2:
alert("Welcome to back to our website");
localStorage.setItem('visits', parseFloat(ga_visits)+1);
continue;
case 3:
alert("Welcome to back to our website, You really seem to like our website");
localStorage.setItem('visits', parseFloat(ga_visits)+1);
continue;
}
} else {
canClick=canClick+1;
}
}
});