AngularJS与谷歌分析 [英] AngularJS with Google Analytics

查看:135
本文介绍了AngularJS与谷歌分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add Google Analytics code to my AngularJS app.

我将Ruby On Rails用作后端框架。 Rails的AngularJS application.html.erb 文件只在第一次请求时加载。

I am using Ruby On Rails as a backend framework. The AngularJS application.html.erb file of Rails loads only on the first request.

Google分析代码在 viewcontentLoad 事件中。

So, I am putting google analytics code inside viewcontentLoad event.

 $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代码的有效方式。

I don't know if this is a valid way of inserting Google Analytics code or not.

推荐答案

这个答案在跟踪Google Analytics页面Angular.js的意见可能会有帮助。

This answer on Tracking Google Analytics Page Views with Angular.js may be helpful.

其中一条评论表明这是一个很好的方法:

One of the comments suggests that this is a good way to do it:

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

这篇关于AngularJS与谷歌分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆