2 Replies Latest reply on Mar 16, 2018 2:38 AM by Kundan Shukla

    How to track visitor's time on page, so that it can be used as filter in smart campaign?

    Kundan Shukla

      Hi,

       

      I am wondering if (known) visitor's time on page can be tracked/measured. Like, visitor is on page for at least 30 seconds.

       

      Intention behind this is to use this for behavior scoring. Like, if the visitor spends at least 30 seconds on any page (either company web site or Marketo landing page) increment his/her behavior score by 1.

       

      Is there any way to do this? If yes, I would deeply appreciate any help with this.

       

      Thanks.

          • Re: How to track visitor's time on page, so that it can be used as filter in smart campaign?
            Kundan Shukla

            Hi Sanford Whiteman,

            Thanks for the hint. I did search with the suggested term in community and found couple of your's comments and reference links.

            To use 'Visibility API' was a valid suggestion. I have come up with a working code (with help of some other resources and code snippets) that suffice my requirement.

            Following is the working code. Hope it helps somebody having similar requirement. (Also attached is the javascript file with code.)

            $(document).ready(function(){
                 var timeOutComplete=false;
                 var timeoutCounter=0;
                 var currentURL = window.location.href;
                 if( document.referrer!=='undefined' || document.referrer!==null ){
                      var referrerURL =  document.referrer;
                 }else{referrerURL=''}
                 var visProp = getHiddenProp();
                 var evtname = visProp.replace(/[H|h]idden/,'') + 'visibilitychange';
                 if(visProp){
                      var sessionTimer=setTimeout(function(){
                           sendMunchkinURLparams(currentURL,referrerURL);
                           timeOutComplete=true;
                           timeoutCounter++;
                           console.log('Min session time complete on first landing.');
                      },31000);
                      document.addEventListener(evtname, function(){
                           if(isHidden()){
                                clearTimeout(sessionTimer);
                                console.log('Session time count interupted.');
                           }else{
                                if(timeOutComplete==false && timeoutCounter==0){
                                     setTimeout(function(){
                                          sendMunchkinURLparams(currentURL,referrerURL);
                                          timeOutComplete=true;
                                          timeoutCounter++;
                                          console.log('Min session time complete on coming back.');
                                     },31000);
                                }
                           }
                      });
                 }
            });
            function sendMunchkinURLparams(webURL,refURL){
                 Munchkin.munchkinFunction('visitWebPage',{
                      'url':webURL,
                      'params':'sessionduration=minrequired&referrer='+refURL
                 });
            }
            function getHiddenProp(){
                 var prefixes = ['webkit','moz','ms','o'];
                 if ('hidden' in document) return 'hidden'; //if 'hidden' is natively supported just return it
                 // otherwise loop over all the known prefixes until we find one
                 for (var i = 0; i < prefixes.length; i++){
                      if ((prefixes[i] + 'Hidden') in document)
                           return prefixes[i] + 'Hidden';
                 }
                 return null; //otherwise it's not supported
            }
            function isHidden() {
                 var prop = getHiddenProp();
                 if (!prop) return false;
                 return document[prop];
            }