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
Does this not work with 2.0.4? I mean, it seems to work. On my computer, the link says (97 hits). I tried it on every other computer in the house, but they all still say "no click." Why?
Thanks for your time :)
It works with every WP version from 0.71 to 13.99 (due in 2035)
[…] Click Counter Plugin For WordPress « planetOzh Plugin para WordPress que mantém um contador de cada link de saÃÂda da sua página (tags: wordpress) […]
[…] Click Counter – este é só divertido, conta quantos clicks por link dos que são colocados no texto dos posts […]
[…] Click Counter Aggiunge un contatore di click a fianco di ogni link […]
Hi Ozh
I've been using Click Counter for months to track downloads of my plugins. Very happily. But…! I wanted to wrap the download into a graphic so use the construct:
At which point it stopped tracking the hits. Wondered if you have any ideas or pointers?
[…] I have changed my mind. I have completely, no entirely removed the click counter plug-in from my blog because of better connectivity for external blogs. So enjoy to post here, execpt comment spam… […]
It is too hard for a blogger to install a plugin like this.Can the installation be easier?
Perfecto. Yo agregue una funcion al principio del plugin para crear la tabla en la base de datos.
'
function install_contador() {
global $wpdb;
$wpdb->query("CREATE TABLE IF NOT EXISTS `wp_linkclicks` (
`link_id` int(11) NOT NULL auto_increment,
`link_url` text NOT NULL,
`link_clicks` int(11) NOT NULL default '0',
`link_date` datetime NOT NULL default '0000-00-00 00:00:00',
`link_title` text NOT NULL,
UNIQUE KEY `link_id` (`link_id`)
);");
}
install_contador();
'
Olvidaba decir Gracias y los espero por mi sitio que esta casi nuevo
jejeje
Hey I wonder if you get some time, how about a way to exclude certain always present links from being counted (e.g., in conjunction with the popular post module).
First, let me say what a wonderful plugin this is. I installed it a few weeks ago and it worked great. However, I recently moved my entire WordPress installation on my server (out of a folder called /wp/ and into my root directory, so that no longer did my url look like http://www.heyitsfree.net/wp/post-title)
But it seems that when I moved WordPress, this plugin stopped working. Do I need to edit the MySQL table in any way? Any idea why a move would cause this to stop working? Thanks in advance!
Goob
[…] Click Counter: Cuenta los clics que se hacen sobre los enlaces. […]
[…] Habe gerade das Click Counter Plugin in wordpress eingebaut, so I get an idea about how many outgoing clicks mein Blog verursacht. Schliesslich ist Aufmerksamkeit die Währung der Informationsgesellschaft, und sollte ich zahlreiche Aufrufe einer Site verursachen, wäre das schon ein Grund zumindestens eine Rückverlinkung zu bekommen.. […]
[…] Click Counter Plugin For WordPress « planetOzh – […]
[…] æ‰“ç®—å¥½å¥½ç ”ç©¶ä¸€ä¸‹. 感觉挺å¤æ‚çš„, å› ä¸ºæ˜¨å¤©åªæ˜¯ç”¨äº†ä¸‹Ozhçš„ClickCounteræ’件就罢工了(之å‰åœ¨åˆ«çš„主题上没有问题). æ示在theloop.php的第76行有错误, […]
[…] Click Counter is telling you how many people click on your links. It adds a click counter to links in posts. And you can track number of outgoing links, or you can use this plugin as a download counter to see how popular are your files. […]
Thanks a lot!!!
[…] Visit […]
[…] Click Counter […]
hello,
thanks so much for the lovely plugin.
i was wondering: could i possibly have the link redirect at the bottom of the page disabled, so as for visitors to see only the click counter and not the url?
thanks.
M I
hello,
thanks for your lovely plugin.
do you think it's possible when going mouse-on on a link, to have it display only the number of clicks and not it's url? (to surprise the visitors)
thanks
MI
Hi,
it is possible to show the number of clicks somewhere else, at the end of the line e.g.?
thx for help :)
This is a great plugin. This type of is priceless for the new kid on the block.
Thanks
Vic
[…] added Planet Ozh's Click Counter for WordPress plugin to my website. It's a way of judging popularity of the various links I put in my […]
I try to use it, but error. I dont know why?
Thank you! But I miss a somethink like a reload closes :-(
the plugin wont display the right valvues.
if i click a link the plugin will increase the link_clicks but wont show the valvue on page. allways i see the "no hits" label instead of the true valvues…
is the a easy way to fix this?
greatings fib
Fibric » it does show the correct values. But obviously not instantly if, say, you're using WP-Cache (as the page content is pulled from cache)
thanks Ozh, no i dont use webcache. my wp-config.php is clean, no cache entry inside. no cache directory in wp-content. i dont realy know whats going wrong with my wp. any idear?
greatings
Fib » ho, got it. Old bug that's fixed in the upcoming update: it handles badly URLs with a & inside and the only URL you tried is like this :)
okay for know it works because i installed a clean new copy of wordpress.i will update to the next version if you will release then.
keep up good workd man! :)
greatings
i just installed this plugin on my blog and i love it! thank you!
Hi, ive been using your plug in for quite a while now, some weeks ago I upgraded to WP2.2.2 and I deactivated it cuz I was having problem with wp-cache. A some days back, like a week or so I reactivated yout pluging, and today all of the sudden I realized that every link in every post is linking to the right place but actually just gets the visitor to the home page, like and error exception or something.
Does anyone else has this problem? thanks
[…] 原作者網站:http://planetozh.com/blog/2004/09/click-counter-plugin-for-wordpress/ (No click) å•Ÿç”¨ä»¥å¾Œç™¼ç¾ sidebar çš„éˆçµå¤±æ•ˆï¼ŒéŒ¯èª¤è¨Šæ¯ä¸é¡•ç¤ºæ˜¯è³‡æ–™åº«çš„å•é¡Œï¼Œä¸æƒ³å†èŠ±æ™‚間了,ç¾åœ¨æ”¹ç”¨æ¨¡çµ„çš„æ–¹å¼å¯ä»¥æ£å¸¸å°±å¥½ï¼Œå…ˆæ¹Šåˆè‘—用,è¬ä¸€å‡ºç¾åš´é‡å•é¡Œå†èªªå•¦ï¼ […]
[…] Click Count […]
hi i installed your plugin in my site on my server shows clicks but not in my site links, please explain
I like this plugin, it is very usefull! Thank you.
I made this functions that creates (and drops) the table automatically…
Cau Guanabara » Nice. I have version 2.0 of this plugin ready, it contains amongst other features a proper install function :) Will release it some day in a near future.
Hi,
I like your plugin very much. I have an idea for one more feature to add. It would be nice if the table contained column 'IP' which would store the ip address of the user who clicked the link. Thanks :)
Giorgi Dalakishvili » I know some would like this kind of feature, but this is analytics territory and it's not the purpose of this plugin. Adding such a table would imply my adding also some functions to analyze data stored, an interface to display results with the ability to filter and dig data etc… This is not something I want to go in.
[…] Click Counter Plugin For WordPress « planetOzh Adds a click counter to links in your posts. (tags: wordpress plugin counter NumberOfClicks) […]
北京æ¬å®¶
[…] nicht neugierig wie oft ein Link im Blog angeklickt wurde? Das folgende PLugin schafft Abhilfe. Das Click Counter Plugin For WordPress von Ozh versieht Links in Beiträgen mit einem Counter. Der Counter kann direkt angezeigt […]
Siip OKe Good Jobs For You Are Thank You Very Much .. Indonesia And The WOrld :)
Okay, I'm trying to install this. However, every time I copy and paste the mysql table that you say to copy and paste into the box it doesn't wanna run? Honestly, I'm a total newbie to this and I hardly understand what I'm doing. Any help is appreciated though!
Okay! I fixed it! Thanks so much for creating this script!
Bonjour,
J'utilise votre magnifique plug-in sur mon site de livres audio gratuits et je me pose une question : est-il possible de forcer le téléchargement au clic ? Est-ce difficile à mettre en place ?
D'avance merci pour votre aide. (beaucoup de personnes non expérimentées ont du mal à télécharger nos livres à cause de ce problème ; en cliquant-gauche elles ne parviennent qu'à l'écouter – et Firefox pose des problèmes pour le clic-droit)
Augustin
ps. Je précise que j'ai tenté de rajouter les lignes :
header("Content-type: application/force-download");
header("Content-Disposition: attachment);
dans le fichier go.php mais sans succès.
Is there a way to set up the plugin where it only shows the download count if the user is logged in? or even if just the Admin is logged in?
Augustin » je ne comprends pas le problème à vrai dire. En quoi FF pose-t-il probleme avec le clic droit? Sinon, bonne idée les 2 headers, mais si cela ne corrige pas le pb, alors je ne sais pas comment faire…