在Woocommerce 3中没有在线支付交易的订单的Google分析集成 [英] Google analytics integration for orders without online payment transaction in Woocommerce 3

查看:73
本文介绍了在Woocommerce 3中没有在线支付交易的订单的Google分析集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Analytics中添加有关订单的信息。但统计数据并未显示收到有关订单的信息。该网站不使用在线支付(也许原因与此有关)。我使用



脚本中已识别的冲突。页脚消失。

解决方案

首先检查虚拟数据是否正常工作。

  add_action('wp_head','add_gscript_trans'); 

function add_gscript_trans(){?>

<! - Google Analytics - >
< script>
(函数(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)
})(窗口,文档,'脚本' ,的 'https://www.google-analytics.com/analytics.js','ga');

ga('create','UA-130000602-1','auto');
/ *要加载电子商务插件,请使用以下命令:* /
ga('require','ecommerce');
/ *您使用电子商务:addTransaction命令将交易数据添加到购物车:* /

ga('ecommerce:addTransaction',{
'id':'1234 ',//交易ID。必填。
'affiliation':'Acme Clothing',// Affiliation或商店名称。
'收入':'11 .99',//总计。
'shipping':'5',// Shipping。
'tax':'1.29'// Tax。
});

/ *接下来,要将商品添加到购物车,请使用电子商务:addItem命令:* /

ga('ecommerce:addItem',{
'id':'1234',//交易ID。必填。
'名称':'蓬松的粉红色兔子',//产品名称。必填。
'sku':'DD23444',/ / SKU /代码。
'类别':'派对玩具',//类别或变体。
'价格':'11 .99',//单价。
'数量':' 1'//数量。
});
/ *最后,一旦您在购物车中配置了所有电子商务数据,就可以使用电子商务:发送命令将数据发送到Google Analytics:* /

ga('电子商务:发送');

< / script>
<! - 结束Google Analytics - >
<?php}

有关详细信息,请参阅此链接:电子商务跟踪


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>

My account Google analytics settings:

Identified conflicts in the script. Footer disappears.

解决方案

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 : E-commerce Track

这篇关于在Woocommerce 3中没有在线支付交易的订单的Google分析集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆