Recommended way to track unique pages view for each URL on my site that's behind a CDN
I have an ASP.NET MVC 3 site that is behind a CDN (combo of Azure CDN and CloudFlare). Each page view pulls data from SQL server (unless it's being cached by the CDN of course).
I'd like to be able to display a "N page views" on each page on my site (~10k pages) so when visitors view the pages, they know how popular (or unpopular) it is. Stackoverflow does this on each question page. I don't care WHO the users were, just the grand total per pag开发者_运维知识库e.
I'm wondering what the best way to do this is. I've thought of using client code, but this is easily messed with by a malicious user to inflate page view count. So it seems the best way is to implement this on the server. I did my best to search Stackoverflow for code samples and recommended approaches but couldn't find something that applied to what I"m asking.
If I were implementing this, I would probably just stand on top of my existing analytics package. I like using Google Analytics, and it tracks all kinds of awesome data, including page hit count:
http://www.google.com/analytics/
In a little piece of async javascript I would just use their API to get the total page views:
http://code.google.com/apis/analytics/docs/
I know it's lazy, but there are far fewer things I can screw up by using their code :-)
Here's some example code I've written to do just this:
https://github.com/TomGullen/C-Unique-Pageview-Tracker/blob/master/Tracker.cs
Advantages
- No reliance on database
- Customisable memory usage
Disadvantages
- Will double count unique views depending on cache expiry + max queue size
Example usage
var handler = new UniquePageviewHandler("BlogPost_" + blogPostID);
if(handler.ProcessPageView(visitorIPAddress)){
// This is a new page view, so process accordingly
// EG: Increment `unique page views` count in database by 1
}
You'll need to modify the code I've linked to slightly to get it working in your project but it should work without too much issues.
精彩评论