【问题标题】:AngularJS with Google AnalyticsAngularJS 与谷歌分析
【发布时间】:2014-08-14 12:22:05
【问题描述】:

我想将 Google Analytics 代码添加到我的 AngularJS 应用程序中。

我使用 Ruby On Rails 作为后端框架。 Rails 的 AngularJS application.html.erb 文件仅在第一个请求时加载。

所以,我将谷歌分析代码放入 viewcontentLoad 事件中。

 $rootScope.$on('$viewContentLoaded', function () {

        (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','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-XXXX-Y', 'auto');
//        ga('send', 'pageview');
        ga('send', 'pageview', {'page': $location.path()});
});

我不知道这是否是插入 Google Analytics 代码的有效方式。

【问题讨论】:

    标签: ruby-on-rails angularjs google-analytics


    【解决方案1】:

    Tracking Google Analytics Page Views with Angular.js 上的这个答案可能会有所帮助。

    其中一个 cmets 建议这是一个很好的方法:

    $rootScope.$on('$routeChangeSuccess', ...)
    

    【讨论】:

    • 我会检查一下并告诉您..感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2016-05-11
    相关资源
    最近更新 更多