Storing/Tracking user activity on a website

try creating tables in your database to store the necessary information.

Off the top of my head I could think to store their IP address

get hold of some GEO information and you can use that IP address to obtain and store the country they are in.

You can store the webpage they were on before coming to your website.

How many times they have came to your website and the pages they have visited and how many times on each page.

IP address can be obtained using

$_SERVER['REMOTE_ADDR']

and

$_SERVER['HTTP_X_FORWARDED_FOR']

geo information to obtain their country can be obtained from many websites...Usually you have to pay for it if you want up to date info...some people off free older info which is still quite useful.

Here is a good provider of GEO information that you pay for but it is very cheap

http://dev.maxmind.com/geoip/geoip2/web-services/

to do the amount of visits just grab their IP address when they arrive, search your database for that IP and if it exists then increment the number of visits by 1. If not create a new visitor. Do the same incrementation on each individual page visit too.

use this to obtain the URL they were on before coming to your site

$_SERVER['HTTP_REFERER']

So a table like this:

userId | userIP | userCountry | visits | webpage1Visits | webpage2Visits | webpage3Visits

Assuming you dont have thousands of pages this could work for a dozen or so pages.

There a tonne of other stuff you can store like average time on site etc etc but this is the basic stuff.

attempt it and when you come across problems along the way ask more questions and people will help :)


Yep, store everything in a database. Have a table for user visits, one for user interactions etc.

To start with have a table page_visits that contains an ID for the user (This is your first problem to solve - how to uniquely identify a visitor. Identify them using their IP and or User agent? Create a cookie that refers to a unique ID? There are different methods depending on your goal - none are 100% fool-proof) and the page ID or URL they visited along with a timestamp.

Add a table to track interactions. How you capture the event is up to you, do you fire an AJAX call using Javascript or jQuery to log an event like a click? This table could contain fields such as user_id, timestamp, action_performed, source_page_id.

The way you decide to implement is entirely up to you but a database would probably be the way to go.


Without going into the whole GDPR debate, although you may want to take that into consideration before going further, here's how I'd proceed.

I'd have a separate table for storing user activity, although I may want to look into using something else to store this data in. Perhaps mongoDB and have all the details for each session stored into a json file. but for the purpose of this excersise you could just use MySQL.

I'd suggest adding entries based on sessions so the user activity table would look something along the lines of:

 1. usserId
 2. location or ip
 3. referrer
 4. timestamp
 5. sessionToken 

The second thing I'd do is have another table for just the session table and have all the details stored there with timestamps.

 1. sessionToken
 2. url
 3. timestamp

Now you can query the last x users that visited your site from the user activity table and when you select one of them in the interface you can see all the details of his visit. This is probably the less taxing method of viewing this information as far as your MySQL queries go.

This is a juvenile look at a very complex process that over the years A LOT of companies started using and it's called Real User Monitoring.


you can use flag for each webpage and save the incremented flag value if he clicks on the webpage to the respective users login database to track which pages (s)he is clicking (this is only for the webpages who has the user database).