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 answer
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>
Perhaps this page will help:
https://stackoverflow.com/a/54111464/7118098
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.First check with the dummy data is its working fine or not.
For more details see this link : E-commerce Track
Why don't you try WooCommerce Google Analytics Integration By WooCommerce or DuracellTomi's Google Tag Manager for WordPress By Thomas Geiger