user1008650
user1008650

Reputation: 41

Tracking page views and display daily, weekly, monthly results

Platform: PHP5, MySQL

I have a web site that displays articles that are sorted into categories. I would like to track the number of views for each article. Then, in the side bar, display the top 5 articles viewed articles for the current day, within the past meek, and within the last month. What do you think would be the best way to do that? One row in database for each view (article_id, timestamp)? What would be the least amount of work for the server?

Thanks, joe

Upvotes: 4

Views: 1441

Answers (2)

Nick Clark
Nick Clark

Reputation: 4467

This can become a tricky problem. If you just store raw hits, your table will grow rapidly and crunching the numbers becomes more time consuming. So, one way to deal with this is to create aggregate tables and crunch the numbers using a cron job.

For example, you could have the following tables

  • hit_count: article_id, timestamp
  • hit_count_daily: day, year, article_id, hit_count
  • hit_count_weekly: week, year, article_id, hit_count
  • hit_count_monthly: month, year, article_id, hit_count
  • hit_count_yearly: year, article_id, hit_count

You then process the data in the hit_count table, add it to the aggregate tables, and then remove the data from the hit_count table.

You also need to think about what happens if someone refreshes the page or if Google crawls the article. Do you want to count those as hits?

To keep crawlers from triggering hits, you could use some Javascript on the page to communicate back to your server and register the hit. This way, a normal browser will trigger the hit but a crawler will not.

You could also offload this task to another service, like Chartbeat or Clicky

Upvotes: 2

Tim
Tim

Reputation: 1899

How about:

Add this to php.ini

auto_append_file = /server_root/footer.php

footer.php contains a silent routine to write the SQL data.

Upvotes: 0

Related Questions