• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1750
  • Last Modified:

Click Tracking with Ajax

Howdy,
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.
0
headzoo
Asked:
headzoo
  • 3
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
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:

<script>
var newImg = new Image();
newImg.src = "http://your.server.com/your/cgi.php?with=some¶ms";
</script>

0
 
headzooAuthor Commented:
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.
0
 
headzooAuthor Commented:
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="http://www.somesite.com" onclick="mytrackingfunc();"&gt;something&lt;/a&gt;

The script
----------------
link_clicked = false;

function mytrackingfunc() {
      myajax = getAjaxObj();
      myajax.send();
      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. :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

- Sean
0
 
hbkmunroeCommented:
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){
    if(document.images){
        (new Image()).src = "/record_stat?id="+id;
    }
    return true;
}
0
 
ZvonkoSystems architectCommented:
Open a new question for your problem.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now