Forums » Help & Troubleshooting



Javascript onclick goal for a button

Hey,

So I'm trying to track an onclick method on a button on one of our pages that is used as a link. I started by creating a function to do the Clicky and Google analytics goal tracking, and then redirect the user to the new location:

function trackButtonClick() {

// Google Analytics Tracking
_gaq.push(['_trackEvent', 'xxxxx', 'xxxxx', 'xxxxxx']);

// Clicky Tracking
clicky.goal(849);
clicky.pause(500);

// Link to the destination page
location.href = 'http://test.mysite.com/destination.html';

}


Down below I have added the trackButtonClick() call to the onclick method of the button like this:

input type="submit" value="Add to cart now!" title="Add to cart now!" name="" onclick="addToCart()"


The issue I'm running into here is that it will not actually track the goal, only the page visit and actions. It seems to be associated with the javascript "redirect" or "link" happening. Here's what I've tried:

1. Comment out the location.href line. This made it track the goal.
2. Up the clicky.pause to 3 full seconds. No worky.
3. Comment out the location.href line, and put the input submit button inside a form with the action set to the destination page. Still no worky.

Posted Sun Oct 30 2011 12:08a by adidal***


Yeah setting the location.href is probably overriding any kind of manual pause.

Here's what you should try instead:

1) remove the clicky.pause() call
2) put the location.href in a timeout function:

setTimeout( function(){ location.href='...'; }, 500 );

That will wait 500 milliseconds before setting the href, which will work similar to the clicky.pause(), but delay the actual setting of that variable as well.

Posted Mon Oct 31 2011 7:20p by Your Friendly Clicky Admin


Thanks! Worked great!

Posted Wed Nov 2 2011 3:54p by adidal***


Great! :D

Posted Thu Nov 3 2011 7:23a by Your Friendly Clicky Admin


You must be logged in to your account to post!