Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track user time on site

Tags:

I'm looking to track users average time on a website (in the same way that Google analytics does) for internal administration.

What's the easiest way to do this?

like image 272
Walker Avatar asked Aug 20 '10 21:08

Walker


People also ask

How do I track user activity on a website?

Ways to Track User Activity on a WebsiteTools like Google Analytics and Search Console. Click tracking (recording which elements on a page users click) Scroll tracking (recording where users scroll on a page) Viewing session recordings of users as they use their site.

Can I see how long I spent on a website?

There is no built in function to record IE usage, although there might be 3rd party software out on the web that might be able to help you out. Do a search for something like " Track Time spent online" and see what that comes up with.

How do I monitor visitors to my website?

Google Analytics is the most powerful website visitor tracking software on the market. It allows you to see how many visitors you are getting and what they do while visiting your website. First, you need to sign up for a Google Analytics account. You can use any Google or Gmail account to sign up.

How do you measure spent time?

The time spent can easily be computed by just summing the time spent on each event. Calculating this time spent is something that can quickly be done even in real-time applications.


2 Answers

You can get the time in next ways:

  1. Once user visit your site, save current time at cookie as "visited", and at next visit you can grab it, if it was set.
  2. And more expensive method: when the page loads, start js timer, and on page unload send to server time which user sent and save it to db.
  3. And if window.unload does not work at Opera, you can send time to server every 5 seconds, and stores it to DB.

If you need, I can write an example script.

UPDATE:

<!DOCTYPE html>
<html>
    <head>
        <title>Collect time</title>
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
        $(function()
        {
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            });
            $(window).unload(function(event) {
                var time = event.timeStamp - start;
                $.post('/collect-user-time/ajax-backend.php', {time: time});
            })
        });
        </script>
    </head>
    <body>

    </body>
</html>

And backend script:

<?php 
$time = intval($_POST['time']);
if (!file_exists('data.txt')) {
    file_put_contents('data.txt', $time . "\n");
} else {
    file_put_contents('data.txt', $time . "\n", FILE_APPEND);
}

But as I said it wouldn`t work at Opera browser

like image 91
Dmytro Krasun Avatar answered Sep 18 '22 13:09

Dmytro Krasun


Main way I can think of:

When the user first hits a page, you log, say, their IP address, the page loaded, and the time. Then, using some Javascript and AJAX, when they leave the page, you use the unload event to send to an AJAX handler that records the page and when they leave.

You would need to use some sort of ID, apart from a session, to store the page visit. Say I have 5 instances of the homepage open, you'd want to log each one individually. So, something like this:

  1. Access the page, generate a code (let's say page: index.php code: 2345)
  2. Store this in a database table with their IP, and visit time
  3. Unload event fire, call the AJAX, passing the page & code
  4. Look up in the DB for the IP, page, and code, and log the leave time

If they visit index.php again, you would generate another code, say, 36789. Use something that generates a random GUID is best, so you can (essentially) ignore any possibilities of collisions on the same IP/page/code combination.

like image 45
Tarka Avatar answered Sep 21 '22 13:09

Tarka