I want to fire a Google Analytics custom event called “add to cart” when someone adds a product to the cart.
I am using classic Google Analytics, not Universal.
I tried adding this to the commmerce_add_to_cart_confirmation.js
:
_gaq.push(['_trackEvent', 'Button', 'Add to Cart']);
Didn’t work.
Then I found the module that builds the add to cart form, commmerce_cart.module, and updated it to go from this:
else { $form['submit'] = array( '#type' => 'submit', '#value' => t('Add to cart'), '#weight' => 50, );
to this:
else { $form['submit'] = array( '#type' => 'submit', '#value' => t('Add to cart'), '#onclick' => "_gaq.push(['_trackEvent', 'Button', 'Add to Cart']);" '#weight' => 50, );
That didn’t work either. I would have preferred the former because the confirmation overlay only is presented when the add to cart is successful, whereas the latter would fire the custom event every time the add to cart button is clicked, even if there was an error.
Any help would be appreciated.