如何将 GA4 电子商务数据层添加到 WooCommerce?

问题描述 投票:0回答:3

我正忙于在我的 WooCommerce 网站中实施 GA4 电子商务

dataLayer
。有一些插件可以实现 Universal Analytics 的
dataLayer
(例如 这个,每年 99 美元),但我找不到 GA4 的任何插件。

这是

dataLayer
事件的
purchase
来源):

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
3个回答
0
投票

我想已经找到了:

    <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>

0
投票

以防其他人需要可以将 WooCommerce 与 GA4 集成的 WordPress 插件。有一个解决方案可以使用 Google 跟踪代码管理器来做到这一点:

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

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


0
投票

您将此代码放置在 Woocommerce 网站的什么位置?

© www.soinside.com 2019 - 2024. All rights reserved.