skip to Main Content

I want to load javascript functions on a wordpress site based on user visit, if it’s the first time, then one function will run, if its second visit another code will run., I found that using cookies and local storage we can set it, but only track user if visited or not., I want to count the user visit, and based on that run the functions.

var first_visit = false;
checkFirstVisit();
function checkFirstVisit(){
    if(localStorage.getItem('was_visited')){
        return;
    }
    first_visit = true;
    localStorage.setItem('was_visited', 1);
}
console.log(first_visit);

this code is to track the first visit, How do I make it to track the number of visits?

2

Answers


  1. it is the same thing
    set a cookie for visiting
    if it is not set so it is the first visit so set it to 1
    if it is 1 so second visit so set it to 2
    if it is n so it is n + 1 visit so incrment n

    Login or Signup to reply.
  2. You can use session storage in PHP (https://www.w3schools.com/php/php_sessions.asp) to store in a variable whether this is the user’s first visit or not.
    Then using an if-else statement, display the required code.
    The good thing about PHP session storage is that it can retain the storage even after the user closes the browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search