You have a blog. You have stats telling you how many people read it. You post links. But do you have something telling you how many people click on your links ?
Here is it : I'm releasing my latest plugin, Click Counter. Basically, it adds a click counter to links in your posts, as in these examples :
I love WordPress, WordPress and WordPress.
Click on one link and reload the page (hit counter displayed on mouseover link title, in plain text, or both)
Less basically, it comes with a real bunch of options you'll probably never need to tweak, but I wanted to do something as flexible and usable as I could.
Current version : 1.02
Note : you may find sometimes this plugin disabled here on my own server, when I'm tweaking a few things. The plugin is fully functionnal anyway, download it :-)
Note: The plugin is rather well commented, advanced users can probably directly download, edit and run. I'd suggest reading the "How it works" and "Tips & Tricks" sections anyway.
Set up the MySQL table
You need a new table in your WordPress database, named wp_linkclicks (you can modify the name, be sure then to edit the beginning of the plugin properly) Create this table with the following query :
- CREATE TABLE `wp_linkclicks` (
- `link_id` INT NOT NULL AUTO_INCREMENT ,
- `link_url` TEXT NOT NULL ,
- `link_clicks` INT NOT NULL ,
- `link_date` DATETIME NOT NULL ,
- `link_title` TEXT NOT NULL ,
- UNIQUE (
- `link_id`
- )
- );
Newbie tips : first, install PHPMyAdmin on your website. This is a great interface for anything you can do with MySQL databases. Once it's done, look for the "SQL" link in the menu : click, and paste in the textbox the above code. It will set up the required table for you.
Install
ozh-click-counter.zip
Extract and upload to your blog, preserving directory structure if any.
Note: download counter here and stats on wordpress.org may differ and reflect the number of downloads before this plugin was hosted on the plugin directory
The archive contains the tracker file, go.php : put this file in your blog root. If for some reason you want to modify the tracker file name, be sure to properly edit the beginning of the plugin script as well.
Use the plugin
In its default configuration, the plugin needs nothing more. Get back to your blog and post as usual. It will silently add trackers to links in your posts and links in commenters' posts.
A small warning : adding a counter to a link "costs" one SQL query. If you are hosted on a slow server, or if you are teh ultimate linkblogger with 200 external links posted in your frontpage à la Metafilter, you might slightly overload your SQL server :) If this is your case, read by the end of this article the Tips & Tricks, I'll explain how to add a "silent tracker with no SQL query".
Another small warning : the plugin works with proper xhtml syntax, that is href="url" or class="myclass" (quotes), not href=url or class=myclass (no quotes)
How it works
Don't change anything to your posting habits, the plugin is the "install & forget" type.
When your WordPress will print a post with a link, the plugin replaces every link target and title. For example :
- <a href="http://www.link.com/page.php">link</a>
becomes
- <a href="yourblog/go.php?http://www.link.com/page.php"
- title="XX clicks">link</a>
The number of clicks in the title value will be added to an existing title if specified : title="this is a cool link (XX clicks)"
When someone clicks on this modified link, it will add or update the following values to the table you've created earlier :
- link_id: the link id, which is more or less useless
- link_url : the url, "http://www.this-is-a-link.com/page.php" in the above example
- link_clicks : how many people clicked on it
- link_date : the date of the first click on it (not the date you posted it)
- link_title : the title of the remote page, if exists, as defined by its title html tag
Unless stated otherwise, only external links will have a counter added, because it's pointless to track how many people click on your pages. There are stats plugins for this :)
So, if you want to add a counter to an internal file, put a absolute path in the link html : href="http://site/blog/file" instead of href="file"
Template Tags
I've written two "template tags" you can add to your pages
- wp_ozh_click_topclicks($limit, $trim , $pattern) : displays your most clicked links
- wp_ozh_click_comment_author_link() : a replacer for comment_author_link() in wp-comments.php, if you want to add a counter to your commenters' website when specified.
As you can see, the first one, wp_ozh_click_topclicks, has 3 optionnal arguments :
- $limit : how many links you want to display. Default value is 5, and can be modified in the plugin.
- $trim : maximum length for displayed links title. Setting this for example at 5 will convert "WordPress Home" to "Wordp…". Default value is 15, and can be modified in the plugin.
- $pattern : html code used to display the list. See right below.
The pattern parameter is an html string with template tags. The default value, as defined in the plugin, is :
- <li>
- <a href="%%link_url%%" title="%%link_title%%">%%link_title_trim%%</a>: %%link_clicks%%
- </li>
Tags are enclosed in between "%%" and can be :
- link_id: the link id number in the sql database, I don't see why you would use this one :)
- link_url : the link url, obviously.
- link_clicks : the number of clicks the link has received so far
- link_date : date of first click (more or less useless too, I admit)
- link_title : remote page title
- link_title_trim : shortened value of link_title, trimmed to the value of $trim as defined previously.
If the target page has no title, and is html content, the title will be replaced by the url itself. In this case, %%link_title%% will output the url, shortened by its trailing slash and leading http://www. if applicable.
It the page has no title because it's not an html document (http://site.com/somefile.zip for example), it will be replaced by the document name (somefile.zip)
Optionnal configuration tweaking
Now, I'll go through all the variables and their default values you can modify and edit at the beginning of the script. For the interested only, most users don't need so much tweaking and are probably bored of reading this page :)
Note : as usual, I've named the plugin file, functions and vars with a beginning "wp_ozh_click" to prevent any nuclear world war.
Core variables :
$wp_ozh_click['table'] = 'wp_linkclicks'
Name of the table where stats will be stored
$wp_ozh_click['file'] = get_settings('siteurl') . "/go.php"
Name and location of the tracker file
Basic features :
$wp_ozh_click['track_all_links'] = 1
Value can be 1 or 0
If set to 1, every link will have a tracker. Just post as usual.
If set to 0, you will have to specify when you want to add a tracker, with the following code :
- <a href="http://site.com/" count="1">link</a>
The value countwill be removed at processing time, so output will be valid html.
The value of count can be : 1, title, inline, or a mix.
- count = "1" : number of hits will be added to link according to the default behaviour as defined in the script (see below)
- count = "title" : number of hits added in link title : <a href="http://site.com/" title="69 hits">link</a>
- count = "inline" : number of hits added in plain text, after the link : <a href="http://site.com/" >link</a> (69 hits)
- count="title inline" : both : <a href="http://site.com/" title="69 hits">link</a> (69 hits)
$wp_ozh_click['in_title'] = 1
Value can be 1 or 0
Default behaviour for displaying counter (according to $wp_ozh_click['track_all_links'] or to count value in the link html). If set to one, display hit number in link title.
$wp_ozh_click['in_plain'] = 0
Value can be 1 or 0
Default behaviour for displaying counter (according to $wp_ozh_click['track_all_links'] or to count value in the link html). If set to one, display hit number in plain text after the link.
$wp_ozh_click['0click'] = 'No click'
Default text for zero click
$wp_ozh_click['1click'] = 'One hit'
Default text for one click
$wp_ozh_click['clicks'] = '%% hits'
Default text for several clicks, where %% will be replaced by a number
$wp_ozh_click['method'] = 2
Value can be 1, 2 or 3.
There are 3 ways to modify the link html to add the tracker. Each has advantages and drawbacks, but all validate any Doctype up to XHTML 1.1. If you want things to remain simple, just skip this setting and leave its default value
Depending on the chosen method, <a href="http://site.com"> will become :
- Method 1:
<a href="http://site.com" onclick="window.location='/go.php?http://site.com'; return false">
Cool : status bar shows real link without further trick.
Less cool : doesnt work with "open link in new window" - Method 2:
<a href="/yourblog/go.php?http://site.com">
Cool : works with "open in new window" and doesn't require Javascript enabled
Less cool : shows ugly link "/blog/go.php?http://site.com" in status bar - Method 3:
like method 2 but also modifies status bar to hide the ugly and sometimes annoying "yoursite.com/blog/go.php?" part with onmouseover="javascript:window.status='http://site.com'; return false"
Cool : status bar shows real link.
Less cool : adds a few bytes of html - I'd suggest you use preferably method 2, or at least method 3. Method 1 is really less accurate since it doesn't keep track of links opened in a new window
$wp_ozh_click['do_posts'] = 1
Value can be 1 or 0 :
Do you want to add a counter to links in your posts ?
$wp_ozh_click['do_comments'] = 1
Value can be 1 or 0 :
Do you want to add a counter to links in your commenters comments ?
Now onto the link title features :
$wp_ozh_click['get_title'] = 0
Value can be 1 or 0.
Get remote page title the first time a user clicks a link to store it along with hits in the table. This will slow down the first clicker, by 1 or 2 seconds maximum, time for your website to retrieve the distant page and parse it.
Note : mostly untested feature. It uses fopen(), check your host has enabled this (if not, the plugin will work anyway, and title will simply be empty)
$wp_ozh_click['get_title_forcerefresh'] = 50
Value is an integer.
Refresh remote page title every XX clicks. Set to 0 if you don't want to use this feature. If you chose to use it, the higher traffic – then clicks – you get, the higher you should set this. Examples : 50 for Joe's blog, 3000 for Slashdottish blog.
To be honest this is really a gadget – almost totally useless :)
$wp_ozh_click['extensions'] = array ( lots of extensions )
Most common non html file extensions. These are files that have no title html tag, so their link title will be the file name (somefile.zip for example)
Then, top link features :
$wp_ozh_click['top_limit'] = 5
Default number of top links to be displayed by wp_ozh_click_topclicks() (see above, Template Tags)
$wp_ozh_click['top_pattern'] = '<li><a href="%%link_url%%" title="%%link_title%%">%%link_title_trim%%</a>: %%link_clicks%%</li>'
Default pattern used to display top links (see above, Template Tags)
$wp_ozh_click['trim'] = 15
Default maximum length of link titles (0 not to trim)
Tips and Tricks
Here are listed a few tips or thoughts I've had during the development and test of the plugin.
» As a file download counter
You can use this plugin as a download counter to see how popular are your files :-P
To do so, you can either specify count="1″ in the download link html tag, or link them with an absolute file (http://site/blog/download/file.zip)
» Use with Matt's Asides
If you are using Matt's asides or something similar, you'll have to modify a bit your index.php
Replace
- echo wptexturize($post->post_content)
with
- echo wp_ozh_click_modifyhrefs(wptexturize($post->post_content))
» Use with blogrolls
Similarily, if you want to use the feature on a blogroll link list for example, modify the php code to add wp_ozh_click_modifyhrefs().
Remember one thing : each link counter display (in title on in plain text) will cost 1 SQL query. Know what your server can handle before doing such things :)
» Invisible tracker for weak SQL servers
You can use the plugin to track clicks at no SQL cost : set $wp_ozh_click['track_all_links'], $wp_ozh_click['in_title'] and $wp_ozh_click['in_plain'] to zero. It will still modify your link html tags to add the tracker file to them, but it won't display hit counters along with links.
Now, check your link clicks from phpmyadmin directly, or using the template tag wp_ozh_click_topclicks() (1 SQL query)
History
1.0 :
- Initial release
1.01 :
- improved : only one query if same url repeated
- fixed : no javascript in links for rss outputs (so the feed validates)
1.02 :
- fixed potential SQL injection
Wow
It took me almost longer to write this doc than to write the plugin itself :)
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/2a
Hi,
I have written a function "my_the_content" which replaces the original function "the_content" and prints the post content on my own way. How can my function work with your code? I assume your code should run after all other functions, so there should be no difference. However, it does not work for me when I switch to my function instead of "the_content".
Sorry for spamming you with al the questions earlier this week. I was just getting so excited by the plugin, I wanted to tweak it to my own needs as soon as possible. I've got it all working now, but did notice I'm using a huge amount of bandwidth since I installed the plugin.
Looking at the apache logs showed some of the hits on go.php just generate enourmous traffic.
For example a "GET /blog/go.php?http://www.centraalboekhuis.nl/" this morning consumed some 610megabytes. Most of the hits to go.php result in a status 302 and do not have any bytes set in the Apache logs. However, sometimes the HTTP status is set to 200 and there is a very large number of bytes sent. This looks like there is actual data being sent back and not a 302-Moved header.
I'll continue to investigate as I'm now burning through my monthly available bandwidth in a couple of days. Does this problem sound familiair to you?
Afshin » it's your function, can't help you with something I didn't make. Make sure this function is not *echoing* (printing, outputting, whatever you call it) anything, but passing it to wp_ozh_click… as a parameter.
Wilfred » looks like a server dependant issue, that a few seem to have (read comments here). You could modify the go.php to output a 302 header all the time, don't know, I've never encountered this problem myself.
Ozh,
I solved the problem by looking at the code of function "the_content". Calling the filters should have been done after all other things I did. That's all.
However, I still have another problem. Hit numbers are shown in Firefox but not in Internet Explorer. Do I need to change any setting?
Thanks.
Hi, Problem with the go.php are solved. I removed the IIS part and that did the trick.
I just noticed that you add "…" to the link_title_trim. When using this it leads to a warning on XHTML validation since it is not closed by a ;
Perhaps something to fix in a future version.
One other enhancement I made to the wp_ozh_click_topclicks is to not sort by the absolute number of clicks but by the average number of clicks per day. I had one entry that was linked from a high traffic website. The links in that entry had a very high number of clicks and were never going to disappear from the 'Popular Links' category. By taking the average number of clicks per day, they will eventually disappear and will be replaced by more recent links. Perhaps something to consider for a future version (switchable by a parameter to wp_ozh_click_topclicks)
[…] Well, thanks to the WordPressPlugins page, I was able to find a nifty little that will tell me how many times someone has clicked on a link. If you are a WordPress user, you can check out this plug at its Homepage. […]
I'm in the same boat as some, in terms of trying to get my blogroll to utilize your plugin. I've gone into the links.php file and started tinkering around. It is my understand that the wp_ozh_click_modifyhrefs(string) function takes in the entire a href tag, from opening anchor to closing anchor, then adds the necessary information.
With that in mind I went to edit the get_links() function. Here is a snippet of my code….
$output.= "';
if (($row->link_image != null) && $show_images) {
if (strstr($row->link_image, 'http'))
$output.= "link_image' $alt $title />";
else // If it's a relative path
$output.= "link_image' $alt $title />";
} else {
$output.= $name;
}
$output.= '';
$output = wp_ozh_click_modifyhrefs($output);
For some reason I get back (a )LINK NAME(/a), replace the () with . Any thoughts? Am I being supremely stupid and missing something?
Btw, I apologize for my poor HTML coding….
Sorry to have semi spammed you, but I managed to figure out my problem. Turns out that in the get_links() function, the url is not properly formatted. Instead of being href="url" it was href='url'. When I modified the code to have the true qoutes it took care of everything, and I'm now quite satisfied with the plugin. Great job, as I am abhorent with regular expressions. Thanks again!
[…] Click Counter adds a click counter to links in your posts. […]
Hello Ozh,
I’m using WP in Blogsome. I activated the Click Counter plugin, but I have not been able to make it work.
I suppose I need to add a line of code somewhere but I have not been able to figure out what or where.
I tried adding the following line of code in my Index.file, with and without the {literal} smarty tags, but it is not working.
{literal} %%link_title_trim%%: %%link_clicks%% {/literal}
Maybe you or somebody else reading this can help me. I would appreciate it.
Thanks.
Great plugin, many thanks.
One note: it doesn't work with the acronym replacer plugin, if you use an acronym as the link. I mean: I used "CNN" as a link to a CNN news article, and I have the acronym "CNN" defined as Cable News Network.
When hovered, it only showed the "Cable News Network" It does work though, I changed the link to "CNN link" and the counter showed up fine on the " link" half.
Just an FYI, probably not worth even trying to troubleshoot.
Thanks for the plugin. It's nice to be able to see what people are clicking on
Hopefully third time's a charm since your pain in the ass spam filter won't let me comment.
I'd like to suggest that the script ignore search engines as well as if "bloginfo('url')" is in the URL to be redirected to.
i.e. my blog is at http://www.viper007bond.com/ and if that string is in the URL, don't process it.
ViperBond » sorry about that, I kill every comment with email adresses containing 3 or more digits, this saves me a few hundreds spams each week :)
Ah, gotcha. I just use Bad Behavior to 403 the bots myself. :)
Hey great pluging, but doesn't parse href's in single quotes eg:
foobar
i guess i need to change the preg call but don't know my regexps :-)
I have modified the WordPress plugin wp_ozh_clickcounter v1.01 so it:
-can be used to track clicks on links/blogroll
-can ignore bots
-can ignore admin clicks
-ignores links to local absolute URLs.
-$wp_ozh_click[’do_comments’] = 1
See http://froginmythroat.no-ip.com/wp/2005/08/wp_ozh_clickcounter-v101fimt/
[…] Other added feature about wordpress I liked was the plug-in Click Counter that help keep count on number of people who actually clicked on the links on a blog. –> […]
[…] Plugin Name: Click Counter Plugin URI: frenchfragfactory.net Description: Adds a click counter to links in your posts […]
[…] Click Counter Plugin For WordPress « planetOzh Site Navigation […]
[…] I recently added a couple of plug-ins: Comment Count, WP-Scrobbler, Event Calendar, Click Counter, and a few others I haven't installed yet. I'm literally going ape shit with adding them. […]
[…] Ab sofort werden alle Klicks auf Links in Beiträgen gezählt.. das mach ich mit dem "Click Counter Plugin for WordPress" – ausserdem lass ich die Liste der verwendeten Plugins in der FAQ ab sofort dynamisch darstellen (dank des "Plugins Used" Plugins) .. […]
[…] Click Counter This plugin keeps track of all external links that are clicked. It also enables the list of popular external links in the sidebar. […]
[…] Click Counter […]
Whoever. . . any one have any idea why I would see the following?
I'm running WP 1.5 – Strayhorn. The plugin is installed correctly, activated, and being called correctly. It's counting, but it will send me to a blank page when using Firefox, and in Internet Explorer, a “Page not found†will be returned to me. The redirection doesn't appear to be working correctly. I searched all the above comments and didn't see any definitive work-around.
Any help would be much appreciated!
Thanks,
Dave
Hi
Great plugin! But if i dont want the (No Click) to show if no one clicked it yet, where can i change that?
(And i still want to have $wp_ozh_click[’track_all_links’] = 1 ; )
You can't.
(without modifying the plugin yourself, that is)
Ok
If i do like this:
I delete No Click but where do i find the () so it doesnt write out that?
[…]   Click Counter Plugin For WordPress,点击统计æ’件。 […]
is there any way to use this as a category click count?
I wanna keep track of how many hits does my categories receive each….
I have installed this on my new website and i like it, is there a way to get this counter to work with ed2k links as used on my website ?
have anyone use it to count the on posts "read more" hits?
[…] que os hablo, y en la sección El Blog he implementado un Top con los enlaces más populares). No hay Comentarios –> […]
I'm having an issue with your plugin, but it's most likely I'm using it in a way other than intended. I'm trying to track clicks on the title of the post (which is working properly) but I'm actually trying to display the click counter in a different location on the post. I've tried calling
wp_ozh_click_getclicks(the_permalink())
where I want it, but all that gets returned is the printed permalink. Is what I'm trying to do possible with the plugin as is?
Never mind, I solved my own issue. Bloody get_permalink()…
At first, thanks for the great plugin!
It works great but I am wondering if this is possible:
I've modded my template a bit so that it can read a custom field to put a link in the title of the post.
What can I do to track (and show the click number) like the other links in posts? Thanks in advance.
Alan » don't know, it depends obviously on the code you modified. You should be able to use my function wp_ozh_click_modifyhrefs() with the link passed as an argument.
Watch fedora4.com/fedora-downloads/, how i use this script whit other script.
Watch also in the sidebar the top 10 downloads.
I also modify this line: $result->link_title = preg_replace("/((ht)*f*tp:\/\/)*(www\.)*(fedora4.com\/downloads\/)*/", "", $result->link_url); , so now appear only the name of the file
It's a great plugin, but it causes a very high load on my server. Serverload went up from 0.9 to 12.4…
Maybe not very handy on a site with 20K + pageviews :-)
too many to do, I think I need some time to learn how to install it in my wordpress blog.
Have a question – can it be somehow used for tracking Add to Favorites link? I have tried it, but no result – I guess this is due to java… Any ideas?
add to favorites
I installed this plugin on one of my blogs, and I love it.
I'm having a problem sort-of similar to the one others were having. Switching from link modificaiton method 2 to 3 makes no difference in Firefox; the status bar still shows the ugly link. IE6 and Netscape 7.2 have the same problem.
Is there anything I can do to fix this?
[…] Better Feed Click Counter IP 2 Nation Random Words […]
Great plugin thanks. Am a newbie @ wordpress and got it working fairly easily. Was just wondering whether there is anyway this plugin can be amended to just track downloads of rar/zip files and not links to other web pages?
Thanks again.
[…] Click Counter Plugin: Explicación para Blogsome del contador de clicks en enlaces. […]
[…] ä¸è¿‡ä¹Ÿæœ‰ç‘•ç–µï¼Œä¸åŒºåˆ†å¤§å°å†™ï¼ŒæŠŠä»»ä¸€å—æ¯å¤§å†™åŽå°±è¾¨è®¤ä¸å‡ºæ¥äº†ï¼Œå†è€…与simpletagså’ŒClick Counter相冲çªï¼Œæœ€åŽä¸å¾—ä¸æ”¾å¼ƒè¿™ä¸€æ’件。 […]
Nice extension, will be useful for some downloads I plan to place on my site.
I had an interesting problem – I don't know the cause really, but amending the link worked. Just thought I'd tell you in case its useful:
Gives an xml error page with – at the bottom:
http://blog.sciam.com/index.php?title=threw_the_book_at_em&more=1&c=1&tb=1&pb=1
Works:
http://blog.sciam.com/index.php?title=threw_the_book_at_em
Gavin » heh, indeed, that's a funny one. I just cannot understand why it's behaving like this :)
Thanks for the scoop.