woocommerce / woocommerce-google-analytics-integration

WordPress plugin: Provides the integration between WooCommerce and Google Analytics.
http://wordpress.org/plugins/woocommerce-google-analytics-integration/
170 stars 69 forks source link

Add to cart event not sent when "Redirect to the cart page after successful addition" option is enabled. #427

Open jorgemd24 opened 3 months ago

jorgemd24 commented 3 months ago

Describe the bug:

The cart event is not sent when "Redirect to the cart page after successful addition" option is enabled.

image

Steps to reproduce:

  1. Go to WC -> Settings -> Products and enable "Redirect to the cart page after successful addition".
  2. Open the assistant tag manager
  3. Open an incognito window and buy a product.
  4. See the the Add to Cart event is not sent.
  5. Disable "Redirect to the cart page after successful addition" and buy a product again. See that the "Add to Cart" event is sent.

Expected behavior:

Add to Cart event should be sent regardless "Redirect to the cart page after successful addition" option

Actual behavior:

Add to Cart event is not sent when "Redirect to the cart page after successful addition" is enabled.

Additional details:

puntope commented 3 months ago

Just for when we address this issue. Important to check in all the scenarios and add the test steps in https://github.com/woocommerce/woocommerce-google-analytics-integration/wiki/General-Testing or add E2E

kdingsa8c commented 3 months ago

7952298-zen

jorgemd24 commented 2 months ago

I tested this issue again, but the results were not clear. In some cases, the redirection happens too quickly, not allowing enough time to send the event however today I got the correct event (add_to_cart) 95% of the time, making it difficult to test.

We could use the woocommerce_add_to_cart_redirect filter, but this might duplicate the add_to_cart event if the event is sent correctly on the shop page.

https://github.com/woocommerce/woocommerce/blob/f0b8d13ab4dafebdaa9d94a341eb42024d8220ef/plugins/woocommerce/includes/class-wc-form-handler.php#L819

Another alternative is to use transport_type: 'beacon' as mentioned in this doc, which could help prevent the issue. For now, we'll need to wait and see if this issue affects more merchants.

fetenlakhal commented 1 month ago

@jorgemd24 Any news about this one? We have a merchant who is really upset in ticket 7952298-zen

jorgemd24 commented 1 month ago

Hi @fetenlakhal, my results weren't clear enough to propose a solution. I'll tag @tomalec, who is the porter this week, to have a second look at this. Thanks for bringing it up.

tomalec commented 1 month ago

I was able to reproduce it 100% times with classic [products] shortcode and Storefront theme. In such case WooCommerce core simply redirect and abort firing the event at all

https://github.com/woocommerce/woocommerce/blob/74ee55d515558a05b7773712a7abe4a58a3c37b2/plugins/woocommerce/client/legacy/js/frontend/add-to-cart.js#L109-L116

Another alternative is to use transport_type: 'beacon' as mentioned in this doc, which could help prevent the issue. For now, we'll need to wait and see if this issue affects more merchants.

The docs are no longer there, and from what I found on StackOverflow, the latest analytics use the beacon by default. And given the WC Core behavior described above, I doubt it would help.

I don't see an easy and 100% reliable solution here :/

I think we could either:

  1. Listen to adding_to_cart event, then check for wc_add_to_cart_params.cart_redirect_after_add is set, if so assume the product will be eventually added - not 100% reliable, as it may actually fail to add the product.
  2. Listen to navigation API after adding_to_cart was fired - not reliable, as the API is experimental and not supported in Firefox.
  3. Try identifying added_to_cart event from the cart page after the redirect - not easy, if possible. The only trace I found was in wc_add_to_cart_message but using its hooks to get what we need feels like a really unstable workaround
  4. Change WC Core to fire a native (non-jQuery) Custom, synchronous event (like added_to_cart_about_to_redirect) just before redirecting https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/wc-cart-functions.php#L98:L98 - not fast to ship.

I think the 4. would be the cleanest and most reliable. However, It could take months to release and upgrade our merchants. I imagine someone from WC Core Team could oppose having a blocking event before such a redirect.

//cc @martynmjones

vbcda commented 4 weeks ago

@tomalec thank you for explaining the issue and offering solutions.

Could you please help with the implementation of the # 4 solution that you proposed? Does it involve modifying the core files? Either way I wouldn't mind doing that I as need a solution for this I am not able to track the add_to_cart event on google ads and really need a solution.