Storing/Tracking user activity on a website Storing/Tracking user activity on a website database database

Storing/Tracking user activity on a website


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.


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 :)


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).