简体   繁体   English

如何使用 Barba.js 实现谷歌分析

[英]How to implement google analytics with Barba.js

I am trying to set up google analytics and Barba.js.我正在尝试设置谷歌分析和 Barba.js。 I have this analytics code in the footer;我在页脚中有这个分析代码; see below.见下文。 I am exporting the code with 'import { analytics } from "./analytics";'我正在使用“./analytics”中的“import { analytics};”导出代码using this code below.使用下面的代码。

I am thenusing 'barba.hooks.enter'.我正在使用'barba.hooks.enter'。 I can see each page in the console log as I click through each click, but google analytics is not recording the page links.当我单击每次单击时,我可以在控制台日志中看到每个页面,但谷歌分析没有记录页面链接。 Could some please point out what the issue is with the code?有人可以指出代码的问题吗?

Thank you谢谢

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

importing 'import { analytics } from "./analytics";'从“./analytics”导入“导入{分析};” in app.js.在 app.js 中。 I am using Webpack!我正在使用 Webpack!

const analytics = (container) => {

    (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');

        var path = (window.location.href).replace(window.location.origin, '').toLowerCase();
    
        window.dataLayer = window.dataLayer || [];
        function gtag() { dataLayer.push(arguments); }

        gtag('js', new Date());
        gtag('config', 'UA-xxxxxxxxx-xxx', {'page_path': path}); 

        console.log('view sent', path);
}

export default analytics

in app.js在 app.js 中

barba.hooks.enter((data) => {
    analytics();
  });

You are mixing code of analytics.js library and gtag.js library.您正在混合analytics.js库和gtag.js库的代码。

If you want to use analytics.js you have to use your first code.如果你想使用analytics.js ,你必须使用你的第一个代码。 And you can send custom page in this way:您可以通过这种方式发送自定义页面:

<!-- 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-xxxxxxxxx-xxx', 'auto');
ga('set', 'page', '/about.html');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference

Otherwise the gtag.js code, for Universal Analytics, is the following:否则,用于 Universal Analytics 的gtag.js代码如下:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-XX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXX-XX', {'page_path': '/about.html'});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未在Google Analytics(分析)中注册的AJAX虚拟综合浏览量(全局网站代码和Barba.js) - AJAX virtual pageviews not registered in Analytics (Global Site Tag and Barba.js) 如何在黑莓中实现Google Analytics(分析)? - how to implement google analytics in blackberry? 如何在混合移动应用程序中实施 Google Analytics? - How to implement Google Analytics in hybrid mobile apps? 如何在Rails中实施Google Analytics(分析)内容实验 - How to implement Google Analytics content experiment in Rails 如何在Angular2中实施Google Analytics? - How to implement Google Analytics in Angular2? 如何实施新的Google Play服务(分析)? - How to implement the new Google Play Services (Analytics)? 如何在Google Analytics中实施唯一自定义指标 - How to implement Unique Custom Metric in Google Analytics 如何实施Google Analytics(分析)进行交叉销售? - How to implement Google analytics for cross selling? 如何实施OAUTH 2.0访问Google Analytics(分析) - How to implement OAUTH 2.0 to access Google Analytics 如何在 Google Tag Manager for Google Analytics 中测试和实现数据层 - How to test and implement data layer in Google Tag Manager for Google Analytics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM