Show lightbox every X visit to site

Anonymous
Not applicable

Show lightbox every X visit to site

Hey everyone,

Just wondering if anyone has had any success implementing rules when to show a lightbox?

We want to show lightbox every 4th time a user visits the site.

Appreciate any help here!

Many thanks,
Michelle

Tags (2)
1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Show lightbox every X visit to site

Michelle, here's a handy utility function that lets you check if a visitor is on their ​n​th visit to the site (where visit is defined as a new browser session):

<script>

(function(global) {

  var propCurrent = 'MM_Session',

    propAll = 'MM_SessionHistory',

    currentSession = sessionStorage.getItem(propCurrent),

    allSessions = JSON.parse(localStorage.getItem(propAll) || '[]'),

    ts = new Date().getTime();

  if (!currentSession) {

    currentSession = sessionStorage.setItem(propCurrent, ts);

    allSessions.push(ts), localStorage.setItem(propAll, JSON.stringify(allSessions));

  }

  global.isEveryNthSession = function isEveryNthSession(n) {

    return (allSessions.length % n == 0);

  }

})(window);

</script>

You use the function like this:

if ( isEveryNthSession(4) ) {

  // do something on every 4th visit, like pop up a lightbox

}