Saving / tracking user activity on the website

I was wondering what would be the best way to store / track user actions on a website. I think about the user's activities on the points that the user visited, how many times she visited when (for example, a time stamp) his / her images.

I am currently thinking of creating multiple tables in my database for each web page on my website, each of which will track aspects of user activity on my website. For example, 1 table containing a timestamp and user ID of each user who visited this web page, another table that stores its actions on the web page (for example, I liked something)

Would this be a good way to track user activity or are there other, better methods for doing this?

PS: I use PHP and MYSQL for the website, and I do it as a means of improving my coding, so I will not use Google analytics or anything like that.

+8
html database php mysql
source share
3 answers

Yes, save everything in the database. There is a table for user visits, one for user interactions, etc.

First, use the page_visits table that contains the user ID (this is your first problem to solve - how to uniquely identify the visitor. page_visits identify them using your IP address and user agent? Create a cookie that refers to a unique identifier? There are various methods, depending from your goal - none of them are 100% flawless), as well as the page ID or URL they visited, along with a timestamp.

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

How you decide to implement is entirely up to you, but it will probably be the path to the database.

+5
source share

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

On top of my head, I might think to save my IP address

get information about GEO, and you can use this IP address to get and store the country in which they are located.

You can save the web page that they were on before coming to your site.

How many times they came to your site and the pages they 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 for getting your country can be obtained on many websites ... Usually you need to pay for it if you want to update information ... some people do not have free older information, which is still very useful.

Here is a good provider of GEO information you pay for, but it's very cheap.

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

to make the number of visits, just grab their IP address, when they arrive, search the database for that IP address, and if it exists, increase the number of visits by 1. If you do not create a new visitor. Do the same increment on every single page.

use this to get the url they were at before coming to your site.

 $_SERVER['HTTP_REFERER'] 

So a table like this:

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

Assuming you don't have thousands of pages, this might work on a dozen pages.

There is a ton of other things that you can store as the average time on the site, etc., but this is the main material.

give it a try and when you run into problems along the way ask more questions and people will help :)

+1
source share

you can use the flag for each web page and save the value of the incremented flag if it clicks on the web page in the corresponding user login database to keep track of which pages (a) it clicks (this is only for web pages that have user database).

0
source share

All Articles