Bug: remove-from-cart tracking broken
-
Hi Pixel Manager team,
First of all, thank you for a great plugin! It has saved us an enormous amount of time setting up conversion tracking. We have, however, stumbled upon a bug we want to report. I believe this bug is not specific to my environment but is a bug in your plugin.
Environment: Pixel Manager for WooCommerce (free, v1.67.0), WooCommerce (10.9.4), Flatsome theme with child theme.
The bug is, in short, that remove-from-cart tracking is broken due to a mismatch in how
cart_item_keysare handled, which produces the “Wasn’t able to retrieve a productId” error message. Let me describe this in detail.When a customer removes a product from the cart (mini-cart, cart and checkout), the Js-console in Chrome show the following error:
Pixel Manager: error: Error: Wasn't able to retrieve a productId
at e.removeProductFromCart (functions.js:171)
at HTMLAnchorElement.<anonymous> (event_listeners.js:159)This results in the
pmw:remove-from-cartevent nevr being sent.Steps to reproduce: Add any product to the cart, then load any page with the product in the cart, finally click a remove-from-cart button/link.
Root cause: I had a long debug session where I managed to trace where things go wrong. A data-format mismatch inside the plugin between the backend cart sync and the remove-link resolver:
1. ajax_pmw_get_cart_items() returns cart item keys as
cart_item_keys[key] = { id, is_variation, parent_id }// class-pixel-manager.php
$data['cart'][$product->get_id()]['variant'] = (string) implode( ' | ', $variant_text_array );
$data['cart_item_keys'][$cart_item]['parent_id'] = (string) $product->get_parent_id();
$data['cart_item_keys'][$cart_item]['is_variation'] = true;2. pmw.getCartItemsFromBackend() and pmw.reconcileCartFromBackend() in
src/js-src/public/pmw/functions.jsmerge that response intopmwDataLayer.cart_item_keysviaObject.assign, overwriting the{ product_id, variation_id }entries that the inline cart-fragment scrips printed on page load byincludes/pixels/class-pixel-manager.php.// includes/pixels/class-pixel-manager.php
<script>
(window.pmwDataLayer = window.pmwDataLayer || {}).cart_item_keys = window.pmwDataLayer.cart_item_keys || {};
window.pmwDataLayer.cart_item_keys['<?php
echo esc_js( $cart_item_key );
?>'] = <?php
echo wp_json_encode( $data, $json_encode_options );
?>;
</script>
// src/js-src/public/pmw/functions.js</span>
if (pmwDataLayer.cart_item_keys[cartItemKey]["variation_id"] === 0) {
productId = pmwDataLayer.cart_item_keys[cartItemKey]["product_id"];
} else {
productId = pmwDataLayer.cart_item_keys[cartItemKey]["variation_id"];
}3. pmw.getProductIdByCartItemKeyUrl() still reads the old fields, but since the “variation_id” key was overwritten, the productId will be undefined “variation_id” does not exist in the
cart_item_keys[cartItemKey]associative array.// product-detection.js
if (pmwDataLayer.cart_item_keys[cartItemKey]["variation_id"] === 0) {
productId = pmwDataLayer.cart_item_keys[cartItemKey]["product_id"];
} else {
productId = pmwDataLayer.cart_item_keys[cartItemKey]["variation_id"];
}This explains why the “Wasn’t able to retrieve a productId” error was thrown.
Happy to provide debug logs or more details if useful. Thanks again for a fantastic plugin!
You must be logged in to reply to this topic.