Duplicate transactions from mobile devices
-
Hello, my client was noticing duplicate e-commerce transactions specifically from mobile devices. When I looked at the plugin code, I saw that this should not happen when a page is reloaded from the server. (in woocommerce.php for example).
However, I noticed that on some mobile browsers, when a user returns to a browser tab after some time, JavaScript on the page is executed again, even though the page is not re-fetched from the server. This is what I believe was causing the duplicate transactions. I added code in frontend.php to set and check a transaction-specific cookie, and if it exists, to not resend the transaction to GA.
$transId = $gtm4wp_datalayer_data["transactionId"]; $_gtm_header_content .= ' if(Cookies && !Cookies.get("ga-transaction-' . $transId . '-tracked") ){ ' . $gtm4wp_datalayer_name . '.push(' . str_replace( array( '"-~-', '-~-"' ), array( "", "" ), str_replace( "?", "-", $dl_json_data ) ) . '); }';
if($gtm4wp_datalayer_data["transactionId"]){ $transId = $gtm4wp_datalayer_data["transactionId"]; echo '<script>if(Cookies) { Cookies.set("ga-transaction-' . $transId . '-tracked", true, { expires: 14 }) }</script>'; }
Would you consider adding this, or something like it, to the plugin? Essentially, check both server-side AND client-side if the transaction has already been sent to GA.
- The topic ‘Duplicate transactions from mobile devices’ is closed to new replies.