Improving handling of add_to_cart and other Woo events
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 | TaskUncaught 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.
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_cartlistener, return early if the$buttonargument is not an actual jQuery element instance
- [x] In the
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_cartand other WooCommerce events.
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.
@abdelmalekkkkk, did you forget to save your changes?
@eugene-manuilov Yep it seemed like it didn't go through. Should be good now.
IB ✔️