2

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.

0

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 () {
  • The question is about Drupal Commerce, not Ubercart – David Thomas Aug 25 '15 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.