javascript - Google Analytics 转换代码无效

标签 javascript google-analytics

我在 Google Analytics 中启用了转化跟踪。我的分析代码包含在每个页面上,如下所示:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-104000000-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];

  function gtag() {
    dataLayer.push(arguments);
  }
  gtag('js', new Date());
  gtag('config', 'UA-104000000-1');
</script>

尽管它显示了跟踪代码管理器代码,但实际上我是通过 Analytics 获得的,并且有意不使用 Google 代码设置。

我还在页面上添加了 JS 代码,用户在完成交易后将结束该页面。我的代码添加在页面底部,如下所示:

<script>
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
    'id': '%invoice.public_id%',
    'revenue': ' %payment.amount%'
  });
  ga('ecommerce:addItem', {
    'id': '%invoice.public_id%',
    'name': '%item.item_title%',
    'price': '%item.first_total%',
    'quantity': '%item.qty%'
  });
  ga('ecommerce:send');
</script>

所有占位符值都正确填充,但我没有将转化数据滴入分析。其余分析似乎运行良好。

更新:还尝试将我的代码更改为以下代码,但仍然只访问非电子商务。我确实检查过以确保增强型电子商务已启用。

<script>
    gtag('event', 'purchase', {
        'transaction_id': '%invoice.public_id%',
        'value': '%payment.amount%',
        'items': [
            {
                'id': '%item.item_id%',
                'name': '%item.item_title%',
                'quantity': 1,
                'price': '%item.first_total%'
            }
        ]
    });
</script>

我错过了什么?

最佳答案

您将 gtag 代码与 Universal Analytics (ga) 代码混合在一起。 您可以在此处找到有关使用 gtag 增强电子商务的文档:

https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce

另一种解决方案可能是使用 Universal Analytics 代码代替 gtag:

<!-- 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-104000000-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

因此,对于电子商务跟踪,您可以使用您在帖子中编写的代码段:

<script>
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
    'id': '%invoice.public_id%',
    'revenue': ' %payment.amount%'
  });
  ga('ecommerce:addItem', {
    'id': '%invoice.public_id%',
    'name': '%item.item_title%',
    'price': '%item.first_total%',
    'quantity': '%item.qty%'
  });
  ga('ecommerce:send');
</script>

关于javascript - Google Analytics 转换代码无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59345960/

相关文章:

javascript - Marionette Collection View,获取集合不会触发事件

javascript - 将多个值映射到一个属性的最佳方法

android - 为什么范围级别为 "user"的自定义维度没有显示在我的报告中?

google-analytics - Google Analytics Experiments 总是选择原始变体?

javascript - jQuery OR 运算符?

javascript - jquery 使用 for 循环和随机 eq 触发多次点击事件

javascript - 动态移除最后一个 div

api - Google Analytics(分析)API和内部搜索问题

backbone.js - 用于 Backbone 应用程序的 Google Analytics 事件跟踪的在线资源

java - Android 谷歌分析 v3