简体   繁体   中英

How do I use sessions to display a popup after Nth page?

I have a popup from Survey Monkey but it pops up straight away. That's not really any use for anyone as research about a user's experience of a website relies on the user having used the website for at least a few minutes.

I want to run this code only on the Nth (let's say 7th) page on that session to the site. So only after a user has delved deeper into a site will they be requested to fill out a survey.

I'm a novice but I know that javascript resets on every pageload/refresh so that on its own seems to be out for using setTimeout.

I was thinking if I could somehow track number of pages using a session then only load the javascript when number of pages viewed = x then it would solve the problems but not really sure where to start?

Any ideas would be greatly appreciated, the script for the popup is below:

(function(t,e,n,s){var c,o,l;t.SMCX=t.SMCX||[],e.getElementById(s)||(c=e.getElementsByTagName(n),o=c[c.length-1],l=e.createElement(n),l.type="text/javascript",l.async=!0,l.id=s,l.src=["https:"===location.protocol?"https://":"http://","widget.surveymonkey.com/collect/website/js/surveyidnumbergoeshere.js"].join(""),o.parentNode.insertBefore(l,o))})(window,document,"script","smcx-sdk");

Put this code in head of your site:

session_start();
if(!isset($_SESSION['survey_counter'])
    $_SESSION['survey_counter']=1;

$showSurvey=false;
if($_SESSION['survey_counter']++ >= 7)
{
    $showSurvey=true;
    $_SESSION['survey_counter']=0;
}

and this code where you want to echo JS:

if($showSurvey) echo 'YOUR_JAVSCRIPT_CODE';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM