English 中文(简体)
How to track page views
原标题:
  • 时间:2010-10-19 04:07:10
  •  标签:
  • pageviews

What is the best way to track page views? For instance: SO has how many views a Question has, yet hitting refresh doesn t up the view count.

I ve read that using cookies is a pretty good way to do so, but I m at a loss on how this doesn t get out of hand.

I ve searched all over and can t find a good answer for this.

EDIT:

I also see that another option (once again I could be horribly wrong) is to use Google Analytics API to get page views. Is this even an viable option? How does Stackoverflow, youtube, and others track their views?

最佳回答

You can track them in a database if you re rolling your own. Every time a page loads, you call a method that will decide whether or not to up the page views. You can add whatever criteria you like.

IF IP is unique
OR IP hasn t visited in 20 minutes based on a session variable
ETC
THEN add a page view record

| ID | IPAddress | ViewDateTime |
| 1  | 1.2.3.4   | Oct 18 ...   |

However, session variables can get pretty load intensive on sites with as many visitors as SO. You might have to get a little more creative.

Now, if you don t want to code it, the I would suggest looking into SmarterStats as it reads your server logs and is more robust than analytics.

note: i m not sure about a similar Apache software

问题回答

I set a session variable with the address I m checking, then toggle it if it s been hit by that browser. In my page template I then check the var for that page and handle as appropriate.

A simple, hacky method:

Create local MySQL table for tracking:

CREATE TABLE pageviews ( pageview_count int(9) default NULL )

Then, on the index.php page, or wherever the user is going to be landing, you can run an update query on that field.

<php
$link = mysql_connect( localhost , root ,  ); 
if (!$link) {
    die( could not connect   .mysql_error());
}
$mysql = mysql_select_db($database_name,$link);
$query = mysql_query( UPDATE pageviews set pageview_count = pageview_count + 1; );
mysql_close();




相关问题
Track page views

I am looking for a C# code which tracks number of times a page is visited by unique users (as like tracking IP address) per day and display it to my site. Apart from using Google Analytics, is there ...

How to track page views

What is the best way to track page views? For instance: SO has how many views a Question has, yet hitting refresh doesn t up the view count. I ve read that using cookies is a pretty good way to do so,...

"pageview" term in web world

I m building a web app and need to implement "how many times the item is being viewed". What is the term of "being viewed"? is it every time someone goes to that page or being counted once for the ...

Basic site analytics doesn t tally with Google data

After being stumped by an earlier quesiton: SO google-analytics-domain-data-without-filtering I ve been experimenting with a very basic analytics system of my own. MySQL table: hit_id, subsite_id, ...

热门标签