73460

Google analytics integration for orders without online payment transaction in Woocommerce 3

Question:

I'm trying to add information about orders in the Google analytics. But the statistics do not show the receipt of information about orders. The site does not use online payments (perhaps the reason is related to this). I used the <a href="https://stackoverflow.com/questions/52174961/google-analytics-integration-for-a-custom-order-received-page-in-woocommerce-3/52190053?noredirect=1#_=_" rel="nofollow">answer</a>

I added code to functions.php in my theme directory.

add_action( 'woocommerce_thankyou', 'google_analytics_integration', 20 ); function google_analytics_integration(){ ?> <script> ga('require', 'ecommerce'); <?php // GET the WC_Order object instance from, the Order ID $order = wc_get_order( $order_id ); $order_key = $order->get_order_key(); $transaction_id = $order->get_transaction_id(); // Doesn't always exist $transaction_id = $order_id; // (Or the order key or the transaction ID if it exist) ?> ga('ecommerce:addTransaction', { 'id': '<?php echo $transaction_id; // To be checked ?>', 'affiliation': '<?php echo 'UA-130000602-1'; // replace by yours ?>', 'revenue': '<?php echo $order->get_total(); ?>', 'shipping': '<?php echo $order->get_shipping_total(); ?>', 'tax': '<?php echo $order->get_total_tax(); ?>', 'currency': '<?php echo get_woocommerce_currency(); // Optional ?>' }); <?php // LOOP START: Iterate through order items foreach( $order->get_items() as $item_id => $item ) : // Get an instance of the WC_Product object $product = $item->get_product(); // Get the product categories for the product $categories = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) ); $category = reset($categories); // Keep only the first product category ?> ga('ecommerce:addItem', { 'id': '<?php echo $transaction_id; ?>', 'name': '<?php echo $item->get_name(); ?>', 'sku': '<?php echo $product->get_sku(); ?>', 'category': '<?php echo $category; ?>', 'price': '<?php echo wc_get_price_excluding_tax($product); // OR wc_get_price_including_tax($product) ?>', 'quantity': '<?php echo $item->get_quantity(); ?>', 'currency': '<?php echo get_woocommerce_currency(); // Optional ?>' }); <?php endforeach; // LOOP END ?> ga('ecommerce:send'); </script> <?php }

I added the counter code to header.php

<!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-130000602-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-130000602-1'); </script>

My account Google analytics settings: <a href="https://i.stack.imgur.com/5xXlH.jpg" rel="nofollow"><img alt="Google analytics settings" class="b-lazy" data-src="https://i.stack.imgur.com/5xXlH.jpg" data-original="https://i.stack.imgur.com/5xXlH.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Identified conflicts in the script. Footer disappears. <a href="https://i.stack.imgur.com/9I4LW.jpg" rel="nofollow"><img alt="Identified conflicts" class="b-lazy" data-src="https://i.stack.imgur.com/9I4LW.jpg" data-original="https://i.stack.imgur.com/9I4LW.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> <a href="https://i.stack.imgur.com/xdomX.jpg" rel="nofollow"><img alt="Identified conflicts" class="b-lazy" data-src="https://i.stack.imgur.com/xdomX.jpg" data-original="https://i.stack.imgur.com/xdomX.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

First check with the dummy data is its working fine or not.

add_action('wp_head','add_gscript_trans'); function add_gscript_trans(){?> <!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-130000602-1', 'auto'); /* To load the ecommerce plugin, use the following command: */ ga('require', 'ecommerce'); /* You add transaction data to the shopping cart using the ecommerce:addTransaction command:*/ ga('ecommerce:addTransaction', { 'id': '1234', // Transaction ID. Required. 'affiliation': 'Acme Clothing', // Affiliation or store name. 'revenue': '11.99', // Grand Total. 'shipping': '5', // Shipping. 'tax': '1.29' // Tax. }); /* Next, to add items to the shopping cart, you use the ecommerce:addItem command: */ ga('ecommerce:addItem', { 'id': '1234', // Transaction ID. Required. 'name': 'Fluffy Pink Bunnies', // Product name. Required. 'sku': 'DD23444', // SKU/code. 'category': 'Party Toys', // Category or variation. 'price': '11.99', // Unit price. 'quantity': '1' // Quantity. }); /* Finally, once you have configured all your ecommerce data in the shopping cart, you send the data to Google Analytics using the ecommerce:send command: */ ga('ecommerce:send'); </script> <!-- End Google Analytics --> <?php }

For more details see this link : <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce" rel="nofollow">E-commerce Track</a>

Answer2:

Perhaps this page will help:

<a href="https://stackoverflow.com/a/54111464/7118098" rel="nofollow">https://stackoverflow.com/a/54111464/7118098</a>

Also, can't see the previous line where owl is defined, but it looks like it's defined from a jQuery $() from an element ID. Perhaps check that it exists and that owlCarousel isn't deferred and is indeed attached to the element you're referencing before .owlCarousel() is called on it.

Answer3:

Why don't you try <a href="https://wordpress.org/plugins/woocommerce-google-analytics-integration/" rel="nofollow">WooCommerce Google Analytics Integration</a> By WooCommerce or <a href="https://wordpress.org/plugins/duracelltomi-google-tag-manager/" rel="nofollow">DuracellTomi's Google Tag Manager for WordPress</a> By Thomas Geiger

Recommend

  • C# WPF Child Windows inside Main Window
  • got “libatk-1.0.so.0: undefined symbol” when I tried ipython --pylab in Ubuntu 13.10
  • Getting started with Sync Framework and SQlite
  • How to add fog to texture in shader (THREE.JS R76)
  • How to store confidential PDF documents (file system vs. SQL) if we only use forms authentication
  • mysql setting a negative integer value to a primary auto increment field which is int
  • python: Using ncurses when underlying library logs to stdout
  • Bash script to bring up and down an interface on loop
  • Multiple custom authentication with spring security
  • Wildcard in MapPageRoute
  • Set selected using jQuery
  • ffmpeg: How to concat audio files and add background music in a single command?
  • Display a custom text if user has already bought the current product In Woocommerce
  • forever does not start on server reboot [duplicate]
  • getting the values of checkboxes in a checkboxlist control
  • Using django-multiupload within a ModelForm
  • integrity constraint violation: NOT NULL check constraint
  • Network communication options in Java (Client/Server)
  • Is there a .NET method equivalent to NETWORKDAYS in Excel?
  • Reader monad - how does it conform to Monad interface?
  • Example of using Service Exists MSBuild task in Microsoft.Sdc.Tasks?
  • Facebook friend list in Facebook Android SDK 3.14
  • Update all WooCommerce product prices to 2 decimals in database
  • Allowing audio files in Spring MVC 3.0?
  • Stop an element moving with padding on hover
  • select an input by value?
  • Runtime complexity of getting the length of a string in different representations
  • C++ STL stack pop operation giving segmentation fault
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • Drag and drop unicode TText in DelphiXe4
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Firebase: How to read from external DB?
  • JavaScript RegExp Replace
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • XSLT Transformation to validate rules in XML document