Click Tracking with Ajax

Posted on 2006-05-10
Last Modified: 2008-07-10
I'm trying to write an outbound link tracking script using Ajax.  The problem I'm having is the links are only being recorded by the backend PHP script half the time.  I think the problem is the page moves onto the link's location before the Ajax script has had a chance to send the information to the backend scripts.  I've had the same problem using other types of JavaScript tracking, and I've inserted a short pause before the browser moves onto the link location, to give the script time to do it's thing.

Is there a way to keep the browser from moving onto the link's location until the Ajax script get a response from the backend?  I tried creating a function for the Ajax script to call when it gets a response, and then it uses window.location to send the browser to the link's location, but for various reasons that's not satisfactory.
Question by:headzoo
    LVL 63

    Accepted Solution

    The shorter the instance creation call the greater the chance not to get killed by page unload.
    So the shortest call I can imagine is this:

    var newImg = new Image();
    newImg.src = "";


    Author Comment

    Yeah, I've done that, and it still isn't reliable if the backend is running like molasses for whatever reason.  This is why I decided to give Ajax a go, since I can at least get a response from the backend when it's done.

    Author Comment

    Well, you gave me an idea when you mentioned the unload event.  Here is what I'm doing:

    Add onClick to a link
    &lt;a href="" onclick="mytrackingfunc();"&gt;something&lt;/a&gt;

    The script
    link_clicked = false;

    function mytrackingfunc() {
          myajax = getAjaxObj();
          link_clicked = true;

    window.onunload = function() {
          if (link_clicked == true) {
                while (myajax.statuscode != 4) {

    In a nutshell, when the link is clicked, the ajax function sends it's stuff to the backend, and sets the global variable link_clicked to true.  The unload event for the window checks if link_clicked == true, and if it does, goes into a while {} loop until the ajax object returns a status code of 4, meaning it's done.  Then the window finishes unloading.

    It *seems* to be working. :)

    Author Comment

    The Ajax method has turned out to be more trouble than it's worth.  I'll stick with the above old fashioned method. :)

    - Sean

    Expert Comment

    I've tried to implement this myself. It works fine on my localhost but when I put it live it doesn't work. Any idea what the problem would be?

    //record stats
    function clk(id){
            (new Image()).src = "/record_stat?id="+id;
        return true;
    LVL 63

    Expert Comment

    Open a new question for your problem.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now