Javascript Error in JS-Code for preventing duplicate tracking
-
Hey!
On our wordpress-installation, we are using a custom orderId (with prefix) that breaks the custom javascript code located in “duracelltomi-google-tag-manager/integration/woocommerce.php” starting from line 678.
That has been added to functions.php custom code to change orderid from (int) type “12345” to a mixed datatype “OnlineShop-12345”:
// WOOCOMMERCE: ORDER NUMBER PRE- AND SUFFIX add_filter('woocommerce_order_number', 'change_woocommerce_order_number', 1, 2); function change_woocommerce_order_number($order_id, $order) { $prefix = 'Onlineshop-'; $suffix = ''; return $prefix . $order->get_id() . $suffix; }
Browser console is showing “Uncaught ReferenceError: Onlineshop is not defined” … Here is the code rendered in frontend from GTM4WP:
<script type="text/javascript" id="gtm4wp-additional-datalayer-pushes-js-after"> /* <![CDATA[ */ // Check whether this order has been already tracked in this browser. // Read order id already tracked from cookies or local storage. let gtm4wp_orderid_tracked = ""; if ( !window.localStorage ) { let gtm4wp_cookie = "; " + document.cookie; let gtm4wp_cookie_parts = gtm4wp_cookie.split( "; gtm4wp_orderid_tracked=" ); if ( gtm4wp_cookie_parts.length == 2 ) { gtm4wp_orderid_tracked = gtm4wp_cookie_parts.pop().split(";").shift(); } } else { gtm4wp_orderid_tracked = window.localStorage.getItem( "gtm4wp_orderid_tracked" ); } // Check whether this order has been already tracked before in this browser. let gtm4wp_order_already_tracked = false; if ( gtm4wp_orderid_tracked && ( Onlineshop-214355 == gtm4wp_orderid_tracked ) ) { gtm4wp_order_already_tracked = true; } // only push purchase action if not tracked already. if ( !gtm4wp_order_already_tracked ) { dataLayer.push({"event":"purchase","ecommerce":{"currency":"EUR","transaction_id":"Onlineshop-214355","affiliation":"","value":140.42,"tax":22.42,"shipping":0,"coupon":"","items":[{"item_id":"U3818","item_name":"Plom 113","sku":"113s","price":118,"stocklevel":null,"stockstatus":"instock","google_business_vertical":"retail","item_category":"Zubehoer","id":"U3818","item_brand":"Unbekannt","quantity":1}]}}); } // Store order ID to prevent tracking this purchase again. if ( !window.localStorage ) { var gtm4wp_orderid_cookie_expire = new Date(); gtm4wp_orderid_cookie_expire.setTime( gtm4wp_orderid_cookie_expire.getTime() + (365*24*60*60*1000) ); var gtm4wp_orderid_cookie_expires_part = "expires=" + gtm4wp_orderid_cookie_expire.toUTCString(); document.cookie = "gtm4wp_orderid_tracked=" + Onlineshop-214355 + ";" + gtm4wp_orderid_cookie_expires_part + ";path=/"; } else { window.localStorage.setItem( "gtm4wp_orderid_tracked", Onlineshop-214355 ); } /* ]]> */ </script>
Fix would be, adding the orderid to quotes?
Thanks for helping with this issue!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Javascript Error in JS-Code for preventing duplicate tracking’ is closed to new replies.