Funky world of Juha

Bizarre and funky world of Juha

How to track user behavior August 17, 2010

Filed under: jQuery — juhq @ 7:19 am

User behavior tracking is quite simple with little help from our friend jQuery.

$("a").live("click", function(event) {
      var $this = $(this);
      var url = $this.attr("href");
      var text = $this.text();
      var color = $this.css("color");
      $.ajax({
           url: "saveClick.php",
           data: {url: url, color: color, text: text},
           success: function() {
                window.location = url;
           }
      });
      event.preventDefault();
});

What does this do?
It will do an ajax call everytime when user clicks some link on your page, so you’ll be able to track which links are performing well and which are not.

Add this snippet to your javascript and then just make some backend script that will save all the clicks for you.
This should help you find what links with what color and text gets the most hits, so you’ll be able to optimize your links.

It’s really easy to track whatever from your links, I used text and color in this example. The url will be the unique separator in the backend.

If you are wondering what $this is and why am I using it in this example, you should read this article by Sean Koole

Edit: The codesnippet should be working properly now, thanks Sean for the fix!

Whenever you click a link on a website, which redirects out of that page, all ajax-queries will stop. So in order to save click with ajax, we need first to do the ajax-call and redirect after this is done.

This will make some of the links littlebit slower, as it needs to do the ajax call before redirecting to a new page.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s