Title: Javascript Error in JS-Code for preventing duplicate tracking
Last modified: February 29, 2024

---

# Javascript Error in JS-Code for preventing duplicate tracking

 *  [wirtshausfranz998](https://wordpress.org/support/users/wirtshausfranz998/)
 * (@wirtshausfranz998)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/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”:
 *     ```wp-block-code
       // 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:
 *     ```wp-block-code
       <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)

 *  [igyhere](https://wordpress.org/support/users/igyhere/)
 * (@igyhere)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/javascript-error-in-js-code-for-preventing-duplicate-tracking/#post-17463061)
 * Same issue here, we have also custom ids. Please fix asap
 *  [secureit](https://wordpress.org/support/users/secureit/)
 * (@secureit)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/javascript-error-in-js-code-for-preventing-duplicate-tracking/#post-17463626)
 * Same issue.
 * Comment in code:
 *     ```wp-block-code
       "// Store order ID to prevent tracking this purchase again."
       ```
   
 * So while WooCommerce Order IDs are indeed numeric (order id = post id), your 
   code is not using the Order ID. Your plugin is using the Order Number, which 
   cannot be assumed to be numeric.
 *  [secureit](https://wordpress.org/support/users/secureit/)
 * (@secureit)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/javascript-error-in-js-code-for-preventing-duplicate-tracking/#post-17463993)
 * Here is a band-aid until the author fixes the issue. Goes in functions.php. Test
   before you deploy. I am not responsible if it doesn’t work in your environment.
 *     ```wp-block-code
       add_filter('js_escape', 'fix_gtm4wp_js_bug', 10, 2);
       function fix_gtm4wp_js_bug ($safe_text, $text) {
       	global $wp;
   
       	$order_id = filter_var( wp_unslash( isset( $_GET['order'] ) ? $_GET['order'] : '' ), FILTER_VALIDATE_INT );
   
       	if ( ! $order_id & isset( $wp->query_vars['order-received'] ) ) {
       		$order_id = $wp->query_vars['order-received'];
       	}
   
       	$order_id = absint( $order_id );
   
       	if ( $order_id > 0 ) {
       		$order = wc_get_order( $order_id );
   
       		if($order->get_order_number() == $text) {
       			$safe_text = str_pad($safe_text, strlen($safe_text) + 2, "'", STR_PAD_BOTH);
       		}
   
       	}
   
       	return $safe_text;
   
       }
       ```
   
    -  This reply was modified 2 years, 1 month ago by [secureit](https://wordpress.org/support/users/secureit/).

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.

 * ![](https://ps.w.org/duracelltomi-google-tag-manager/assets/icon-256x256.png?
   rev=1708451)
 * [GTM4WP - A Google Tag Manager (GTM) plugin for WordPress](https://wordpress.org/plugins/duracelltomi-google-tag-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/duracelltomi-google-tag-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/duracelltomi-google-tag-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/duracelltomi-google-tag-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/duracelltomi-google-tag-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/duracelltomi-google-tag-manager/reviews/)

## Tags

 * [GTM4WP](https://wordpress.org/support/topic-tag/gtm4wp/)
 * [purchase](https://wordpress.org/support/topic-tag/purchase/)
 * [ReferenceError](https://wordpress.org/support/topic-tag/referenceerror/)
 * [thankyou page](https://wordpress.org/support/topic-tag/thankyou-page/)
 * [tracking](https://wordpress.org/support/topic-tag/tracking/)

 * 3 replies
 * 3 participants
 * Last reply from: [secureit](https://wordpress.org/support/users/secureit/)
 * Last activity: [2 years, 1 month ago](https://wordpress.org/support/topic/javascript-error-in-js-code-for-preventing-duplicate-tracking/#post-17463993)
 * Status: not resolved