Expand my Community achievements bar.

Join us for an upcoming in-person Adobe Target Skill Builders event ~~> We're hosting these live learning opportunities to equip you with the knowledge and skills to leverage Target successfully. Learn more to see if we'll be coming to a city near you!
SOLVED

Run experience once only

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Level 2

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');
     };
});

View solution in original post

2 Replies

Avatar

Correct answer by
Level 2

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');
     };
});

Avatar

Level 2

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.