How to track visitor's time on page, so that it can be used as filter in smart campaign? | Community
Skip to main content
Jan 23, 2018 7:25 PM UTC
Solved

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

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Accepted solution
Mar 16, 2018 9:38 AM UTC

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];

}

6 replies

SanfordWhiteman
Level 10
Jan 23, 2018 7:33 PM UTC

Search the Community for "munchkin time on page".

Translate
Accepted solution
Mar 16, 2018 9:38 AM UTC

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];

}

Translate
bkirkpatrick
Level 2
Jul 21, 2020 7:13 PM UTC

How do I display this in a smart campaign within Marketo once this is live on my website? Do I need to create a custom field?

Translate
Brad KirkpatrickDigital Marketing ManagerApex Benefits