How to track user time on site

前端 未结 5 1727
渐次进展
渐次进展 2021-01-30 14:49

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?

5条回答
  •  执笔经年
    2021-01-30 15:30

    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.

提交回复
热议问题