使用AngularJS跟踪Google Analytics页面浏览量 [英] Tracking Google Analytics Page Views with AngularJS

查看:149
本文介绍了使用AngularJS跟踪Google Analytics页面浏览量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS作为前端设置了一个新应用程序。客户端的所有内容都是通过HTML5 pushstate完成的,我希望能够在Google Analytics中跟踪我的网页浏览量。 解决方案

如果您在Angular应用程序中使用 ng-view ,您可以监听 $ viewContentLoaded 事件并推送Google Analytics(分析)的跟踪事件。

假设您已经在名为 var _gaq的主index.html文件中设置了跟踪代码并且MyCtrl是您在 ng-controller 指令中定义的。

函数MyCtrl($ scope,$ location,$ window){
$ scope。$ on('$ viewContentLoaded',function(event){
$ window._gaq .push(['_trackPageView',$ location.url()]);
});

更新:的谷歌分析使用这一个

 函数MyCtrl($ scope,$ location,$ window){
$ scope 。$ on('$ viewContentLoaded',function(event){
$ window.ga('send','pageview',{page:$ location.url()});
});
}


I'm setting up a new app using AngularJS as the frontend. Everything on the client side is done with HTML5 pushstate and I'd like to be able to track my page views in Google Analytics.

解决方案

If you're using ng-view in your Angular app you can listen for the $viewContentLoaded event and push a tracking event to Google Analytics.

Assuming you've set up your tracking code in your main index.html file with a name of var _gaq and MyCtrl is what you've defined in the ng-controller directive.

function MyCtrl($scope, $location, $window) {
  $scope.$on('$viewContentLoaded', function(event) {
    $window._gaq.push(['_trackPageView', $location.url()]);
  });
}

UPDATE: for new version of google-analytics use this one

function MyCtrl($scope, $location, $window) {
  $scope.$on('$viewContentLoaded', function(event) {
    $window.ga('send', 'pageview', { page: $location.url() });
  });
}

这篇关于使用AngularJS跟踪Google Analytics页面浏览量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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