SOLVED

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

Go to solution
Anonymous
Not applicable

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Anonymous
Not applicable

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

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

}

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

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

Search the Community for "munchkin time on page".

Anonymous
Not applicable

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

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

}

bkirkpatrick
Level 1

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

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?

Brad Kirkpatrick
Digital Marketing Manager
Apex Benefits
SanfordWhiteman
Level 10 - Community Moderator

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

They'll be coming in as Visit Web Page activities, so you can filter on the URL/query string as with any other web activity.

bkirkpatrick
Level 1

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

Do you mind showing me an example of this particular URL/query string? Thank you.

Brad Kirkpatrick
Digital Marketing Manager
Apex Benefits
SanfordWhiteman
Level 10 - Community Moderator

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

If you were to use the code above, the Visit Web Page would look like:

https://www.example.com/?sessionduration=minrequired&referrer=https://offsite.example

 

Note the code is broken though, it should be URI-encoding the referrer value before appending it.