Reputation: 67
There are plenty of answers online for 'how to combine two onclick events' but nothing seems to work in my situation. Google Website Optimizer gives a tutorial on tracking an event, such as a click. First, I add this to my Analytics code:
function doGoal(that) {
try {
And so on. Then, I should add the following to my event:
onclick="doGoal(this);return false;"
But my event is a button that already contains an onclick event:
<input type="image" src="image.png" onclick="productAddToCartForm.submit(this);return false;">
How do I combine the two? Anything I've tried has either registered nothing with Google, or will forward me to a page called '/undefined/'
Upvotes: 2
Views: 672
Reputation: 14435
I don't know exactly what you are trying to track so I will use an event and pageview as an example:
function doGoal() {
var productNum = document.productAddToCartForm.product.value;
var productQty = document.productAddToCartForm.qty.value;
//event tracking
_gaq.push(['_trackEvent', 'Cart', 'Add', productNum, productQty]);
//virtual page tracking for goal
_gaq.push(['_trackPageview', '/cart/add/']);
setTimeout("document.productAddToCartForm.submit()", 100);
}
Image button:
<input type="image" src="image.png" onclick="doGoal();return false;">
These are examples. Put your own values in the tracking that you would like.
The setTimeout
function allows the trackers time to be sent before the form processes.
Simple fiddle example without the analytics tracking: http://jsfiddle.net/4vvKv/2/
Upvotes: 1
Reputation: 6755
When you want to bind the same event to multiple things you should use addEventListener. Bear in mind that in IE8 and anything before you need to use attachEvent. (also mentioned in above documentation)
Alternatively
onclick="productAddToCartForm.submit(this);doGoal(this);return false;"
should work.
Upvotes: 1