User tracking table design and method - php

I have some questions about database design for user tracking (analytics)
My website will have 3 classes of user:
non user
user with incomplete regstration progress
registered user
For each of these classes I have different events I want to track. Ie: for a non-user I want to track if they reach registration. For registered I want to track if they make it to a certain page etc...
At the start of each visit I will record their landing page and referrer, then I want to track each of these funnels. So for the table design I am thinking:
A table with their session id (stored in a cookie) and user id if they ever log in=one row per user + one row per guest
A table for visit details- referrer, landing page, time etc... so If a user from table 1 visits 10 times they will have 10 entries in this table. Maybe have another table with a list of browsers?
3 tables for each of the user types with tracking flags. Each row will link to the visit in table 2. So there is a 1:1 relation between these 3 tables and table 2
Is this a logical design for tracking users? Does anyone have any links with good articles on user tracking?
Also, how reliable is the http-referrer header for tracking where users came from? Is there any other method I can use to see where people came from?
Also also, how scalable will this table design be?
Thanks, M

Not having done that before, I find your approach not bad - propably improvable, but ok. Just wanted to add that - depending on your requirements - you could be very lazy and let Google Analytics do that for you!
With different tracking rules, you can set up Google Analytics to track differently depending on user cookies, which you can set in the registration/login-process (or not for the non-user).
Downside of this idea is that you'd rely on javascript and you won't have seperate data for each user, just a total of the user-class.
Here's the GA Resource how to set up exceptions (see the lower description). Then create three different GA tracking codes and on each only accept user with either no-cookie, logged-in cookie or registration-process cookie.
Hope I could help, anyway good luck!

Related

Page Watch counter

I'm developing a website which have many products. For every product I want to show a counter with the number of different users whom watched it.
I'm facing with the problem that there will be users logged in and others not, and that fact to avoiding to increment a 'watch' more than one time from the same user.
So, what's the best way to tackle this? How do I store the info?
This simply cannot be done for non-registered users. The closest thing you can do and in fact what web analytics services do in practice is to create a tracking cookie.
In its simplest form it looks something like this:
<?php
if (isset($_COOKIE["trackingId"])) {
$uid = $_COOKIE["trackingId"];
} else {
$uid = uniqid();
setcookie("trackingId",$uid,2147483647);
}
Note that analytics sites will attempt to use information which may uniquely identify a user such as hardware capabilities which they receive, screen resolution etc.
Create a table:
Watched by users
productId, userId, trackingId
You can use this table to keep track of who's watching your product. Normal registered users will be stored with a userId. Non-registered users will be stored with their tracking id.
The advantage of this approach is that you can keep track of the same user using the same browser across sessions even if they change IP. The disadvantage is that as soon as the user clears their cookies you've lost this information.
You can refine your tracking mechanism by doing something like canvas fingerprinting instead of using uniqid but the thing to realise here is that there's no method which can uniquely identify a user forever and rightly so because a user should have the right to not be tracked.
For the simple solution, you can:
To separate "view" users, you can count on user's IP.
"View" info can be stored in a simple table product_view(id, product_id, view_ip, user_id)

Storing/Tracking user activity on a website [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I was wondering what would be the best way to go about storing/tracking user activity on a website. I am thinking of user activity along the lines of which webpage a user visited,how many times has she visited,when(i.e timestamp) of his/her vists.
I am currently thinking of creating multiple tables in my database for each webpage on my website, each of which would track an aspect of user activity on my site. E.g 1 table containing timestamp and userid of each user who visited that webpage, another table storing their actions done on the webpage(e.g liked something)
Would this be a good way of tracking user activity or are there other, better methods of doing so?
PS: Im using PHP and MYSQL for the website, and am doing this as a means of improving my coding so i won't be using google analytics or anything like it.
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).
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.

select users who currently view same page

I find several partial solutions in answers on this question, but common answer seems to be absent.
So, I have a table users with columns user_id, user_name. On each static page of website I want to display all user names of users who currently view this page.
Should I have a table views with columns user_id, webpage_link?
If yes, when I shall update data in column webpage_link? How to connect code from following answer with mysql database Is there a way to detect if a browser window is not currently active? ? (If it is ok for this purpose.)
To make updates very often is not very good. So, the user can view several pages (for example, in 2 or more tabs). What type of webpage_link column shall be in this case?
With every http request, you get a $path variable. if you also have a logged in user, you can store which page this user requested last (e.g. in a table like you described, but only storing the relative path).
You update this information on a per-request-basis in some sort of front-controller. (just make sure you put it where it is called for every authenticated page). When the users session times out, you remove the row of that user from the table.
this case is a little more difficult. you could store the last n pages/paths the user has requested and leave the rest as above. You don't have to change the table structure for that, just allow for multiple rows per user. (the combination user_id+path should be unique, though)
Hope that helps to get you started

Gathering active visitors; assign each visitor an unique user row in database

I'm currently looking for the BEST and SMOOTHEST way to get the active users on my website using a particular page. (purpose: think game lobby).
I was thinking about storing each visitor in a table, with their IP-adress as primary key in the database, together with a lastAction field - which stores a timestamp of their last action.
Gathering the active users would then simply just be to gather all rows where the last action was within the correct amount of time.
The problem: people on the same network (same IP) would be identified as the same user - it'd be hard to tell if they actually were a real person or just a page refresh with an old session. You get the idea.
I'm looking for smoother ways to separate users, or preferably, get away from the whole IP-adress part. Any ideas or suggestions would be appreciated.
Why not use a session based algorithm to determine the count of 'active users'? If you setup your session handler to write to a database (instead of flatfile), you can run queries against rows within the table to determine how many visitors and which page they are currently viewing.
You can read about how to switch your session handler here, but it does appear to be outdated. I suspect more googling will result in newer implementations.

how to handle online status. guest, users etc

Whats the best way to keep track of how many users and guests are online? Im making a forum for fun and learning
Right Now I have a 2 fields in the users table called is_online and last_access_time.
If current time is 5 minutes or more than last_access_time i set is_online it to zero. And if the signed in user refreshes browser i set it to 1.
But what about guests? I wanna keep track on how many guests are on also
Another thing that would be really cool is to show what page the user is viewing. and on the page, forum thread for example, 5 guests, Homer and Shomer are viewing this page. But how should i structure this? Hmm maybe i should make another question for that.
i dont know what i should do
What do you suggest?
I'd use cookies for this. Set a cookie when the user enters (checking first to make sure one doesnt exist). Easy way to generate a unique id for that user is to hash their IP plus the current time.
$id = md5($_SERVER['REMOTE_ADDR'] . time());
Store that id in your database and use that to reference
You can check what page they are viewing by grabbing either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] near the top of your php source. Store that in the table. I'd take a look at php.net's explanation of whats stored in the _SERVER global, as it should help out quite a bit if you find that you need more then just the document they are on (ex index.php). Found here.
You may need to pull apart of the query string that was used to access that page, parse out the variables to determine the page they are requesting. Either way, this could all be done through cookies, or just use a single cookie to store the unique id and use your table for storing everything else.
You cannot know for certain which page a user is viewing, but you can keep track of which page they last viewed. Every time you deliver a page to a user, record that page's path in a database row associated with them. Voila.
To keep the number of guests, I suggest tracking the number of distinct unauthenticated IP/HTTP-User-Agent combinations seen on a certain page in the last X minutes.
I found this article on Web Monkey that might help you.
http://www.webmonkey.com/2010/02/how_many_users_are_on_your_site_right_now/

Categories