Run experience once only | Community
Skip to main content
Level 1
August 10, 2018
Solved

Run experience once only

  • August 10, 2018
  • 2 replies
  • 2647 views

Hi,

I have a Target activity with control experience, and then 2 other (B and C) experiences im serving.

For the 3rd or C experience, I want it to run only once per user session.

Example: The user comes to the page, sees the experience, navigates away from the page, THEN comes back to the page. When the user comes BACK to the page, i do not want the experience to run again.

Is there a quick easy way to do this? Possibly a code example or snippet?

thank you,

David

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by yannickschwarz

Hi David,

My suggestion would be to cookie those visitors on first visit to the experience, and then add an IF/ELSE rule to prevent your code from firing, if the cookie is found, on their second visit.

I am sure there are other more advanced solutions, but this has worked for me in the past .

Good luck:

CODE:

/* Create Cookie */


    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 = "";

        document.cookie = name+"="+value+expires+"; path=/";

    };

    /* Read Cookie */

    function readCookie(name) {

        var nameEQ = name + "=";

        var ca = document.cookie.split(';');

        for(var i=0;i < ca.length;i++) {

            var c = ca[i];

            while (c.charAt(0)==' ') c = c.substring(1,c.length);

            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

        }

        return null;

    };

$(document).ready(function () {

     if (readCookie('cookiename') == 'cookievalue') {

          console.log('do nothing')

     } else {

          // Put the rest of your experience code here //

         createCookie('cookiename','cookievalue',90)

          console.log(' code was fired and cookie was created');
     };
});

2 replies

yannickschwarz
yannickschwarzAccepted solution
Level 2
August 10, 2018

Hi David,

My suggestion would be to cookie those visitors on first visit to the experience, and then add an IF/ELSE rule to prevent your code from firing, if the cookie is found, on their second visit.

I am sure there are other more advanced solutions, but this has worked for me in the past .

Good luck:

CODE:

/* Create Cookie */


    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 = "";

        document.cookie = name+"="+value+expires+"; path=/";

    };

    /* Read Cookie */

    function readCookie(name) {

        var nameEQ = name + "=";

        var ca = document.cookie.split(';');

        for(var i=0;i < ca.length;i++) {

            var c = ca[i];

            while (c.charAt(0)==' ') c = c.substring(1,c.length);

            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

        }

        return null;

    };

$(document).ready(function () {

     if (readCookie('cookiename') == 'cookievalue') {

          console.log('do nothing')

     } else {

          // Put the rest of your experience code here //

         createCookie('cookiename','cookievalue',90)

          console.log(' code was fired and cookie was created');
     };
});

mohan_vamsy
Level 2
October 18, 2019

Pasting the whole HTML code helps in this case? And if I set my cookie expiry to 1 instead of 90, the same user who comes next day will be able to see the experience again? Actual Intention is to show a popup only once in a day for a user( if he closes the popup without performing the desired CTA). The same experience should be shown to him on the next day, but never on the same day.