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.
Related
I am building a link directory style web application. For simplicity all of the following are examples. On my website I have 10 categories. Each category has it's own page and each page has 100 links in a table format. Each link has many columns like name, id, url, etc but the focus of this question deals with the "time last viewed" column. It will display a default text if the user/visitor has never clicked the link however if the link has been clicked by the user prior to the visit it will display the time/date the user last visited that link.
The way I have it set up is when the user clicks the link they are sent to another page/script (using GET method. link 1 is appended with ?rid=1) I use a switch contruct. (Case value is 1 from $_GET execute code block) this code block is where i need the user statistics caputuring to happen. Once the function runs and both captures and stores the visit statistics info the user is sent to the requested resource via header location. So the next time to user sees the list of links on the category page the link they visited will now display the time they visited it.
On my production site i have up to 1000 links. If they clicked each link it would say next to each link the last time they clicked it. Important to include users will be logged in when clicking each link.
How would you go about doing this? Store the info in a cookie or in the database? As there are 1000 links there could be 1000 different values. Thanks in advance.
It isn't a lot of data so you can do both, store in the database as well as store in a cookie. Ideally for performance, you should retrieve from the cookie first and then retrieve from the database if the cookie doesn't contain any user information pertaining to that link. Depending on your performance requirements and the amount of traffic you anticipate, you can use database storage, in-memory storage and asynchronous updates.
database updates are instant but can impact overall performance and page load times
in-memory caching such as apc gives best performance but data needs to be synchronised to the database
asynchronous updates are great for balancing out performance hits because you can register a view from the client side using JavaScript after the page has loaded, rather than during php execution on server side.
Personally I would use all 3 if possible because it gives a good platform for future development.
I currently maintain a DB table of users, when after logging in I update the table with their ID and login_time. This works to a point but currently I can't tell if the user has been active since the login or for how long.
Is there a better way to get a complete list of users that have been active in the past X minutes?
The best way to get what you need would be a "Last Activity" column in the users table. You would just update it whenever a user access a page. Depending on what information you need it could replace the login_time column or it could be a new column.
You'll have to keep track of when the user made their last request in your database as a separate table or column. You can then formulate a query to select, e.g. all users that have made a request in the last 5 minutes.
PHP itself does not store - or care for - that kind of information. Unless you happen to have your own session management module which does store this kind of information, then you could use data from that.
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.
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!
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/