site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Improving handling of add_to_cart and other Woo events

Open jamesozzie opened this issue 3 months ago • 4 comments

Bug Description

A couple of users reported possible conflicts or issues with our WooCommerce add_to_cart events. These are highlighted below:

  • Uncaught TypeError: Cannot read properties of undefined (reading ‘data’) | SH info | Topic | Task
  • Uncaught TypeError: Cannot read properties of null (reading ‘data’) | Awaiting SH info | Topic | Task

One user provided very valuable insights into their findings, found here.

After the first report, we discussed scope for improvement with the data property for add_to_cart events. This GitHub issue is to review and provide such improvements to reduce the liklihood of errors reported by users.

Image

Additional Context

  • Reported since Site Kit 1.159.0

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The reported JavaScript error should not occur while using Site Kit's plugin conversion tracking with WooCommerce at any point.
  • When using Site Kit's plugin conversion tracking, add to cart events in WooCommerce should be correctly tracked for all types of products (e.g. simple, variable, etc).
  • If, for any reason, the event cannot be tracked (e.g. when WooCommerce is customised by the theme or third-party integrations), no JavaScript error should be raised.

Implementation Brief

  • [x] Update the WooCommerce event provider in assets/js/event-providers/woocommerce.js
    • [x] In the added_to_cart listener, return early if the $button argument is not an actual jQuery element instance

Test Coverage

QA Brief

Test base functionality:

  • Navigate to a WooCommerce shop page with simple products
  • Open browser developer console (F12 → Console tab)
  • Click "Add to Cart" on a simple product
  • Verify no JavaScript errors appear in the console
  • Verify the add_to_cart event is tracked (check Network tab for gtag requests or use Google Analytics DebugView)

Test again on the sites/setups described in the issue description. Confirm no JavaScript errors occur.

Changelog entry

  • Fix handling of add_to_cart and other WooCommerce events.

jamesozzie avatar Sep 01 '25 11:09 jamesozzie

I've downgraded this to a P1 as this doesn't appear to happen with a default WooCommerce setup, but it can happen when the DOM is customised by the theme or third-party WooCommerce integrations.

nfmohit avatar Nov 06 '25 06:11 nfmohit

@abdelmalekkkkk, did you forget to save your changes?

eugene-manuilov avatar Nov 18 '25 20:11 eugene-manuilov

@eugene-manuilov Yep it seemed like it didn't go through. Should be good now.

abdelmalekkkkk avatar Nov 18 '25 21:11 abdelmalekkkkk

IB ✔️

eugene-manuilov avatar Nov 19 '25 08:11 eugene-manuilov