React无法将电子商务数据发送到Google Analytics

3

我在使用React向Google Analytics发送增强型电子商务跟踪数据时遇到了麻烦。

我仅为测试将以下代码放入我的index.js中:

ReactGA.initialize('UA-MY-ID-7', {
    debug: true,
 });
ReactGA.plugin.require('ec');


ReactGA.plugin.execute('ec', 'setAction', 'detail', {
  step: 1,
});

ReactGA.plugin.execute('ec', 'setAction', {
  id: '1',
  affiliation: 'Allocab',
  revenue: 30, // Grand Total.
  shipping: '0', // Shipping.
  tax: 32, // Tax.
  currency: 'EUR',
  coupon: 'POMOCODE',
});

ReactGA.event({
  category: 'Booker-ecommerce',
  action: 'New transaction',
});

ReactGA.plugin.execute('ec', 'addPromo', 'promo_click', {
  name: 'PROMOCODE',
});

ReactGA.event({
  category: 'Booker-ecommerce',
  action: 'New discount code',
});

ReactGA.plugin.execute('ec', 'addProduct', 'add', {
  name: 'PRODUCT', // Product name. Required.
  price: 30, // Unit price.
  quantity: 1, // Quantity.
  currency: 'EUR',
  category: 'CATEGORY',
});

ReactGA.event({
  category: 'Booker-ecommerce',
  action: 'Add product',
});
ReactGA.plugin.execute('ec', 'setAction', 'add', {
  step: 2,
});

ReactGA.plugin.execute('ec', 'addProduct', 'checkout', {
  name: 'PRODUCT', // Product name. Required.
  price: 30, // Unit price.
  quantity: 1, // Quantity.
  currency: 'EUR',
  category: 'CATEGORY',
});

ReactGA.event({
  category: 'Booker-ecommerce',
  action: 'Start checkout',
});

ReactGA.plugin.execute('ec', 'setAction', 'checkout', {
  step: 3,
  option: 'Visa',
});

ReactGA.plugin.execute('ec', 'addProduct', 'purchase', {
  name: 'PRODUCT', // Product name. Required.
  price: 30, // Unit price.
  quantity: 1, // Quantity.
  currency: 'EUR',
  category: 'CATEGORY',
  coupon: 'PROMOCODE',
});

ReactGA.event({
  category: 'Booker-ecommerce',
  action: 'Purchase product',
});

ReactGA.plugin.execute('ec', 'setAction', 'purchase', {
  step: 4,
  option: 'Visa',
});

ReactGA.pageview();

我正在使用Google调试工具,一切似乎都很正常。但是在我的Google Analytics中没有收到任何信息。当我使用旧的电子商务插件时,它可以正常工作,但是使用增强型电子商务却无法正常工作。我尝试了使用js原生代码而不是ReactGA库,但结果相同。

您有什么想法,这可能是我的问题吗?


你找到解决方案了吗?如果是的话,能否在这里分享一下? - Muhammad Usama Rabani
1个回答

0

你可以试试这个,对我来说很有效 同时也可以使用文档

 ReactGA.plugin.require('ec');


 for (let i = 0; i < data.orderItems.length; i++) {
          ReactGA.plugin.execute('ec', 'addProduct', {
            id: data.orderItems[i].id,
            name: data.orderItems[i].product.name,
            brand: data.orderItems[i].product.brand,
            category: data.orderItems[i].product.categories,
            variant: data.orderItems[i].product.product_type_name,
            sku: data.orderItems[i].product_id,
            price: data.orderItems[i].cost,
            quantity: data.orderItems[i].quantity,
            currency: "USD",
          });
        }

        ReactGA.plugin.execute('ec', 'setAction', 'purchase', {
          id: data.order_id,
          affiliation: storeName,
          revenue: data.total_cost,
          shipping: data.delivery_cost,
          currency: "USD",
          tax: data.tax
        });
        ReactGA.pageview('/orderDetails');
        ReactGA.plugin.execute('ec', 'clear');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接