追踪谷歌分析页面访问量与Angular.js [英] Tracking Google Analytics Page Views with Angular.js
问题描述
我设置使用Angular.JS作为前端一个新的应用程序。在客户端上一切都与HTML5 pushState的做,我希望能够跟踪谷歌分析我的页面访问量。
I'm setting up a new app using Angular.JS 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.
推荐答案
如果你使用 NG-视图
在角应用程序,你可以听的 $ viewContentLoaded
事件,推动跟踪事件谷歌分析。
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.
假设你已经安装跟踪code在主index.html文件与 VAR _gaq
和的名字MyCtrl是您在定义什么 NG-控制器
指令。
Assuming you've setup 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
$window.ga('send', 'pageview', { page: $location.url() });
这篇关于追踪谷歌分析页面访问量与Angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!