The docs say to use the addEcommerceItem for every item in the cart prior to calling the trackEcommerceCartUpdate method. However only the trackEcommerceOrder method clears the internal eCommerceItems variable. This causes problems when an item is completely removed from the cart - even when using addEcommerceItem with qty 0, the item still gets sent to the server and the server tracks the deleted item as still being in the cart with qty 1.
A much cleaner mechanism would seem to be to just clear the internal eCommerceItems object after every CartUpdate in addition to track order. This of course necessitates using addEcommerceItem for every item in the cart on every change to it, but the docs already say to do that. Unless I'm missing a specific reason why CartUpdate doesnt clear eCommerceItems on purpose? I'm happy to submit a pull request for this one line change but wanted to see if there was a specific reason it was written this way.
I think the idea is that it's meant to match the behavior of a user, eg:
trackEcommerceCartUpdatew/ the one item in the cart
trackEcommerceCartUpdate, which has two items in the internal variable, just as there are two items in the user's cart
I think the problem is that there's no way to remove an item or clear the cart in the JS tracker if an item were to be removed from the user's cart or if the user's cart were cleared.
Maybe we should add two new methods,
clearEcommerceCart. What do you think @matomo-org/core-team ? Would be a quick addition to 3.6.0.
for me it makes sense to add such methods
Oh yes. please add such methods.
Yes those methods be great. FYI: In PHP Tracker we also clear the items after calling
trackEcommerceCartUpdate() but I presume we cannot do this as easily here re BC.