Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to know how long a user has spent on a page?

Say I've a browser extension which runs JS pages the user visits.

Is there an "outLoad" event or something of the like to start counting and see how long the user has spent on a page?

like image 325
dsp_099 Avatar asked Jul 23 '13 07:07

dsp_099


People also ask

Can Google Analytics track time spent on page?

Displays the average amount of time that visitors spend on a page of your site during Google Analytics sessions that are attributed to clicks on search or social objects. Only available after you link your Search Ads 360 advertiser with a Google Analytics web property.

How much time do users spend per page?

One study shows that the average time that a site visitor spends on a webpage is 52 seconds across all industries. You can compare your metric to this standard to get a benchmark of your website's performance.


1 Answers

I am assuming that your user opens a tab, browses some webpage, then goes to another webpage, comes back to the first tab etc. You want to calculate exact time spent by the user. Also note that a user might open a webpage and keep it running but just go away. Come back an hour later and then once again access the page. You would not want to count the time that he is away from computer as time spent on the webpage. For this, following code does a docus check every 5 minutes. Thus, your actual time might be off by 5 minutes granularity but you can adjust the interval to check focus as per your needs. Also note that a user might just stare at a video for more than 5 minutes in which case the following code will not count that. You would have to run intelligent code that checks if there is a flash running or something.

Here is what I do in the content script (using jQuery):

$(window).on('unload', window_unfocused);
$(window).on("focus", window_focused);
$(window).on("blur", window_unfocused);
setInterval(focus_check, 300 * 1000);

var start_focus_time = undefined;
var last_user_interaction = undefined;

function focus_check() {
    if (start_focus_time != undefined) {
        var curr_time = new Date();
        //Lets just put it for 4.5 minutes                                                                                
    if((curr_time.getTime() - last_user_interaction.getTime()) > (270 * 1000)) {
            //No interaction in this tab for last 5 minutes. Probably idle.                                               
            window_unfocused();
        }
    }
}

function window_focused(eo) {
    last_user_interaction = new Date();
    if (start_focus_time == undefined) {
    start_focus_time = new Date();                                                               
    }
}

function window_unfocused(eo) {
    if (start_focus_time != undefined) {
    var stop_focus_time = new Date();
    var total_focus_time = stop_focus_time.getTime() - start_focus_time.getTime();
    start_focus_time = undefined;
    var message = {};
        message.type = "time_spent";
        message.domain = document.domain;
        message.time_spent = total_focus_time;
        chrome.extension.sendMessage("", message);
    }
}
like image 197
Methos Avatar answered Sep 22 '22 01:09

Methos