eberswine
eberswine

Reputation: 1224

How to count page Views using a simple caching system

I am using a really simple FAST caching technique. I just implemented this techniuqe and it is great.

I have a row on my database that counts page views. This is also a very simple technique.

$query_rs_update = sprintf("UPDATE table SET views = views+1 WHERE id = %s", GetSQLValueString($colname_DetailRS1, "int"));

I can not find a way to use this technique and COUNT the VIEWS at the same time. Since the Page is being Cached .. ?

Anyone have a better technique? Can you turn a Query to Javascript so it will work using Caching ?

Thanks!

Upvotes: 0

Views: 1025

Answers (3)

Squiva
Squiva

Reputation: 21

You can use Javascript! Just download Node.js, type the following into your terminal: npm install --save express and npm install --save socket.io. Then, follow this tutorial: https://gist.github.com/Squiva/0eabc2ca5080fd2e4de5

Upvotes: 0

pp19dd
pp19dd

Reputation: 3633

There might be specific reasons to do it way you're doing, but it's generally considered bad practice to track pageviews or activity via MySQL. Your two problems now are that pages are cached, and the tracking ability will be severely underpowered. For example, you'll be missing an ability to separate organic vs robot traffic.

It's best to allow an independent, trusted and capable metrics vendor to do that tracking as they do it for a living - and they can provide a rich dataset to explore.

Warning: check your host's TOS

Note that some hosting companies explicitly forbid forms of 'performance tracking' for managed SQL uses in their terms of service (count increment might apply here, since it can alter index information.) Unless you're using InnoDB storage engine, the performance will always be bad because the table is locked for all reads until the update is finished.

Use Google Analytics

There are limitations to GA, but the positives are that:

  • CACHING: it bypasses any local caching schemas
  • EFFICIENCY: offloads tracking overhead resources onto the client
  • PRICE: it's free to use
  • FILTER: robot traffic is removed from human activity
  • BROWSE: reports can be neatly organized, shared and explored
  • WRITE: has virtual page tracking (so your unique Ajax-dynamic page interaction can still be tracked)
  • READ: there is an API you can use to retrieve the data; ex: most viewed (see http://www.electrictoolbox.com/google-analytics-api-php-class-most-popular-pages/)

Upvotes: 1

Eric Petroelje
Eric Petroelje

Reputation: 60498

You could have some javascript on your page that makes an AJAX request to a separate page that records the page view. This is similar to how most site statistic engines (i.e. Google Analytics) work.

Upvotes: 1

Related Questions