How do I add onclick Google Analtyics custom event "add to cart" to add to cart button in Drupal Commerce? 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' =&gt; 'submit',
    '#value' =&gt; t('Add to cart'),
    '#weight' =&gt; 50,
  );


to this:

else {
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add to cart'),
    '#onclick' =&gt; "_gaq.push(['_trackEvent', 'Button', 'Add to Cart']);"
    '#weight' =&gt; 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.
Sorry for updating, but you can easily add code before sending form! 

It's in sites/all/modules/uc_ajax_cart/js/uc_ajax_cart.js:70

Just put your request after

beforeSubmit : function () {