As the number of page views rose I noticed a small deficiency in the ViewCount figures – it shows the number of times a page has been viewed, but this is not the same as the number of people who have viewed the page, and this is really what I wanted
[note: this plugin is an extension to stattraq - install that first]
I made a small modification to the sql so that it counts the distinct session id’s and therefore now shows the number of unique sessions which have viewed the page – so rather than telling you that a page has been viewed x times, it will tell you that it’s been viewed by y people – where y is any number less than or equal to x (i.e. where y=x each person has viewed the page just once).
Then Irelaised that it might be nice to have both capabilities (a) for informational purposes and (b) so as not to break the behaviour of the API (heh, look at me, one method and Im calling it an api).
Also, to ensure it didn’t grindon the server, i added an index on the session_id as follows: [sql]alter table wp_stattraq add index rjb (session_id);[/sql] – I’m going to assume and users can handle doing this using myphpadmin or mycc.
in case you’ve arrived here ready to cut ‘n’ paste without reading the rest… this plugin is an extension to stattraq – install stattraq first
Installation
Download this file- Rename it viewcount.php and copy it to your
/wp-content/pluginsfolder. - using wp-admin, enable the plugin
Usage
If you’re interested in finding the number of views for a particular post, then in the loop of your index.php file you might add the line:
<?php rjb_get_view_count( $post->ID, "", " views
" ); ?>
If you want to know how many distinct viewers the same post has had, you might use:
<?php rjb_get_viewer_count( $post->ID, "", " views
" ); ?>
Only problem: this doesn’t work if you have the permalinks activated, in which case the article_id is always 0 or Feed in the stattraq table… too bad!
Rich comments: Yeah, shame that – I’ll ping a note to Randy (stattraq’s daddy) and see if it’s possible to get around this between the pair of us – since I’m bending the database beyond it’s initial intended use we may be able to come up with a better way of doing things. Thanks for the heads-up.
Pingback: Metacosm
What you can do to make it work for people with rewrite rules is to pass your functions the title of the post, and then do a lookup in the posts table to find it’s id. That should work with all rewrite setups, but you have two queries instead of just one…
Hi, How could I turn this into a popular post listing? You have the call references already made, so how would I go about creating a list of popular posts? Thanks!
Response:I wondered exactly the same, but my SQL is not quite up to it these days. Any readers with more up to date knowledge are welcome to suggest the required query and I’ll add it.
I’m not 100% positive, but I imagine in order to get ‘popular’ posts, you’d need something like the SQL below. Bear in mind I work mostly in MS SQL, and am not sure if the TOP keyword works in MySQL:
SELECT TOP 10
article_id, COUNT( DISTINCT (session_id) ) as cnt
FROM $tablestattraq
GROUP BY article_id
ORDER BY COUNT( DISTINCT (session_id) ) DESC
Again, no idea how accurate this is syntrax-wise, but it should provide a decent starting point if anyone wants to hack on it.
yeah, it would be great to have it working from the same query as the poular posts to save load time…
looking forward to an upgrade to this great plugin! keep it up….
mat
i get this: 0 views by 0 views users. on all my pages… any ideas?
response: is URL rewriting turned on?
We don’t have TOP in mysql, but we do have LIMIT, so this may work in mysql
SELECT article_id, COUNT( DISTINCT (session_id) ) as cntFROM $tablestattraq
GROUP BY article_id
ORDER BY COUNT( DISTINCT (session_id) ) DESC limit 0,10
response: See the MostWanted plugin.
Any chance you could get this to work on WP installs that don’t have the index.php file in the WP directory? Man, I never would have set up my index file in my root if I knew how many things don’t work with that configuration…
rich: erm… AFAIK it with index.php in my root; or at least it does on this website.
I’ve been trying to get this to work..to no avail. I got stattraq working and it’s recording hits. I also ran the needed database query. I created the ViewCount plugin, and activated it.
From here I am not sure what to do. I wasn’t able to find any sort of installation guide so I am lost at this point. Hopefully you can help.
More input needed for proper diagnosis, but… if you found the usage guidelines (approximately like 29 of the php) and you’re getting zeroes then it’s probably a mod-rewrite issue, which is easy to fix, let me know if this is the case.
If, however, you didn’t find the usage guidelines in the PHP; in the loop of your index.php file you might add the line:
<?php rjb_get_view_count( $post->ID, ”, ‘ views<br />’ ); ?>
to see how many times a post has been viewed, or if you want to know how many unique people (viewers) there have been, use:
<?php rjb_get_viewer_count( $post->ID, ”, ‘ views<br />’ ); ?>
Pingback: Wordpress - Plugins 2 Checkout at the Dogberry Patch
Hi Richard
i added view count to my blog. But i discovered a problem, my site uses frames and when i open the wordpress section view count doesn’t work. While he works if i access the single frame at http://tonyz.altervista.org/wordpress/?p=12 for example.
How is that with frames it doesn’t work? Is there a logical explanation and a way to solve this?
I like this plugin very much. Thanks!
Hello All – I have a pbm:
I can see the view counts only if i’m logged in as admin.
I believe it might be related to a pbm with my stattraq install because I’ve ticked the option to disable login but still every time i access stattraq i’m being asked to login.
Any idea what’s wrong?
FYI: http://80.153.160.104/blog
Thanks
Hi David, the probloid is probably that you need to specify a lower user level in the method call. The default level is 9 so that view count only shows up for admins – this is to minimise SQL load.
try
<?php rjb_get_view_count($article_id, ‘’, ’ views’, 0 ); ?>
where 0 is the user level, so all users see it.
HTH!
You were right Rich – thanks.
viewcount works fine now – just need to figure out how to disable login in stattraq now! ;0)
I don’t know how you guys do it, but the [php] tag in this post didn’t work and the code is all messed up for me. Can you please post it as a .zip file so we can download it? It sounds very useful
Hi Poromenos: I’ve updated the installation steps above so it’s a download/rename process.
Ah, great, thanks. By the way, “download this file” is a bit misleading, I “saved link as” the other day and got the HTML file (I thought it was a regular text file which the server didn’t parse because of the .phps extension). Better instructions would be “open this and copy it to a file called viewcount.php”. Thanks again.
Incompatible with SnapShots?
Not sure if anybody is reading this now, but thought I’d ask. Would love some guidance.
Here’s what I get after installing.
—————
Snap Shots Options [Make this Shot larger] [Close] Options Disable Search Get Snap Shots Free Close Options
Warning: Cannot modify header information – headers already sent by (output started at /home/virtuall/public_html/wordpress/wp-content/plugins/viewcount.php:11) in /home/virtuall/public_html/wordpress/wp-includes/pluggable-functions.php on line 272
————-