2

i need a script to count link clicks on my website and count report to a flatfile/excel.

1
  • 7
    You'll need server-side code (e.g. Python, PHP, C#) to do this. Commented Jun 25, 2010 at 1:23

1 Answer 1

1

The use of a Javascript framework like jQuery would be wise in this case.

Note that you cannot save data into a file on the client's computer. Instead, you can do an AJAX to the server, and save it via your SSI on the server into database/excel/file whatever data storage there is.

My demo will be using jQuery, jQuery Cookie Plugin and PHP:

countdetect.js

jQuery(function(){
  $("a").click(function{
    var cookiename = 'linkcounter';
    if($.cookie(cookiename) == null){
       $.cookie(cookiename, 0);
    }
    $.cookie(cookiename, $.cookie(cookiename)+1);
  });
});

index.php

<?php

session_start();

$counter_file = 'counter';
if(!file_exists($counter_file)){
  file_put_contents($counter_file, 0);
}

$counts = (int)file_get_contents($counter_file);
file_put_contents($counter_file, $counts++);
// you can use $counts if you want to display it on the page.

?><!DOCTYPE html>
<html>
  <head>
    <title>Link Click Counter Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.cookie.js"></script>
    <script type="text/javascript" src="countdetect.js"></script>
  </head>
  <body>
    <a href="http://www.google.com/"></a><br />
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>"></a><br />
    Link clicks: <?php echo $counts; ?>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.