【问题标题】:How do I add the GA4 ecommerce dataLayer to WooCommerce?如何将 GA4 电子商务数据层添加到 WooCommerce?
【发布时间】:2020-12-01 20:09:24
【问题描述】:

我正忙于在我的 WooCommerce 网站中实施 GA4 电子商务 dataLayer。有一些插件可以为 Universal Analytics 实现 dataLayer(例如 this one, for $99 per year),但我找不到任何适用于 GA4 的插件。

这是purchase 事件的dataLayer (source):

dataLayer.push({
  'event': 'purchase',
  'ecommerce': {
    'purchase': {
      'transaction_id': 'T12345',
      'affiliation': 'Online Store',
      'value': '35.43',
      'tax': '4.90',
      'shipping': '5.99',
      'currency': 'EUR',
      'coupon': 'SUMMER_SALE',
      'items': [{
        'item_name': 'Triblend Android T-Shirt',
        'item_id': '12345',
        'item_price': '15.25',
        'item_brand': 'Google',
        'item_category': 'Apparel',
        'item_variant': 'Gray',
        'quantity': 1,
        'item_coupon': ''
      }, {
        'item_name': 'Donut Friday Scented T-Shirt',
        'item_id': '67890',
        'item_price': '33.75',
        'item_brand': 'Google',
        'item_category': 'Apparel',
        'item_variant': 'Black',
        'quantity': 1
      }]
    }
  } 
});

谁能帮我把变量放进去吗?

【问题讨论】:

    标签: e-commerce hook-woocommerce google-datalayer google-analytics-4


    【解决方案1】:

    我猜已经找到了:

        <script type='text/javascript'>
    window.dataLayer = window.dataLayer || [];
    dataLayer.push({
    'event': 'purchase',
    'ecommerce': {
    'purchase': {
      'transaction_id': '<?php echo $order->get_order_number(); ?>',
      'affiliation': '<?php echo get_option("blogname"); ?>',
      'value': '<?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>',
      'tax': '<?php echo number_format($order->get_total_tax(), 2 ,".", ""); ?>',
      'shipping': '<?php echo number_format($order->calculate_shipping(), 2 , ".", ""); ?>',
      'currency': 'EUR',
       <?php if($order->get_used_coupons()) : ?>
      'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>',
      <?php endif; ?>
      'items': [
      <?php
                                     foreach ( $order->get_items() as $key => $item ) :
                                        $product = $order->get_product_from_item($item);
                                        $variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : '';
                                    ?>
                                    {
        'item_name': '<?php echo $item['name']; ?>',
        'item_id': '<?php echo $item['product_id']; ?>',
        'item_price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
        'item_brand': '',
        'item_category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
        'item_variant': '<?php echo ($variant_name) ? implode("-" , $variant_name->get_variation_attributes()) : ''; ?>',
        'quantity': <?php echo $item['qty']; ?>,
        'item_coupon': ''
      },
      <?php endforeach; ?>
      ]
    }
    });
    </script>
    

    【讨论】:

      【解决方案2】:

      以防万一其他人需要一个可以将 WooCommerce 与 GA4 集成的 WordPress 插件。有一个解决方案可以使用 Google Tag Manager 做到这一点:

      https://wordpress.org/plugins/gtm-ecommerce-woo/

      它涵盖了 purchase 和 add_to_cart 事件,并负责填充这些值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-02
        • 2021-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多