I created a click-able image block and posted it on the left sidebar. A click made on the image leads to an external website. How do I count the number of clicks made on that image. The Google analytics don't seem to give immediate results. Is there any module for this. Any help is much appreciated.

| improve this question | | | | |
  • It depends how reliable you want it to be and what kind of count forgery and errors you are willing to accept the most. – Mołot Aug 20 '13 at 8:55

Look on how Advertisement module is doing it.

Simplified outline is:

  1. Each image is linked to a page named like /count/someuniqueid
  2. Module implements that page to increase counter and then sends 302 Found HTTP header to user's browser.
  3. Browser is redirected to a desired page and user don't really see what happened at all.

Actual workflow is, due to performance issues, way more complicated and involves php files that does not bootstrap Drupal, caching and all that.

| improve this answer | | | | |

----inside abc.js---------

Drupal.behaviors.abc = {
 attach: function (context, settings) {
var count = 1;
if (typeof(Drupal.settings.orgs.count) != 'undefined') {
    var count = Drupal.settings.orgs.count;
$('#imageid').mousedown(function() {
 $.post(Drupal.settings.basePath + 'custom-ajax-callback',{count: count});

----inside module abc.module------------

    function custom_ajax_callback() {
    if(isset($_POST['count'] )) {
    $count = $_POST['count'];
    else {
    $count = 1;
    $orgs['count'] = $count;
    drupal_add_js(array('orgs' => $orgs), array('type' => setting));
    drupal_add_js(drupal_get_path('module','abc.module').'/abc.js', array('org' => $org['count']);

By using js function mousedown() you can count the number of clicks on the image

| improve this answer | | | | |
  • Don't forget you will not count clicks from people with JS disabled or adblocking software installed. – Mołot Aug 20 '13 at 8:54
  • 2
    What can be the possible solution when js is disabled ? – harshal Aug 20 '13 at 9:02
  • Thanks for all. @Mołot I used to apply some code when I was used to use ordinary HTML pages (non-drupal). Can you please explain in more detail on how to implement this a Drupal 6 environment. – Sarmad Aug 20 '13 at 11:42
  • @Sarmad I don't get what code you were using. Check out Advertisement (link in my answer), it has D6 version and I hacked it for e-warsaw.pl to get (among other) image click counting. – Mołot Aug 20 '13 at 11:45
  • @user13198 How to use this code, I am not yet comfortable with creating modules myself. Is there any further advise please. – Sarmad Aug 20 '13 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.