I'm looking for a way to track the number of visits to a page, without counting duplicates (like someone hitting refresh several times) and I'd like to figure out the best way to do it without keeping track of every single IP address to view the page. Perhaps a cookie? Any other suggestions?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You mentioned in one of the other comments that you are already using Google Analytics, so you should be able to do the following:
Create a custom report with "Page" as a Dimension and "Unique Visitors" as a metric. This will tell you how many unique Visitors there were for all of your pages for whatever time frame.
A cookie will fit. Just at each page view you check if it is set. If so, don't count it. Otherwise, count it and set the cookie.
You will have problems with people not having cookies enabled or cleaning them out after each visit. You should pair this approach with PHP sessions: instead of doing the process on each page view, do it only once, on session start. This will at least avoid counting visits within a single session.
All this is you want to store everything client-side. If you were server-side, you could use IP addresses, or pair them with the user agent string.
Or, just use google analytics...
Cookies are a good way to achieve this, however you have to keep in mind that users could have disabled cookies... When a user visits your page, just check if your cookie is already present, if not, count++ :)
Either use Webalizer to analyse your logfiles or use Google Analytics to track those metrics for you or - if you are looking for an extendable and self hosted solution, try Piwik:
In any case, there is no reason to reinvent the wheel here. Also, the software mentioned above is only a tiny fraction of web analysis tools out there.
Store/increment a key/value with
redis
ormemcache
, then run a cron job to store values for page/user every x-amount of minutes. Don't need to get complicated with things.