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

问题描述

我正在使用 AngularJS 作为前端设置一个新应用.客户端的所有操作都是通过 HTML5 pushstate 完成的,我希望能够在 Google Analytics(分析)中跟踪我的页面浏览量.

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.

推荐答案

如果您在 Angular 应用中使用 ng-view,您可以监听 $viewContentLoaded事件并将跟踪事件推送到 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.

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

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()]);
  });
}

更新:对于新版本的 google-analytics 使用这个

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 页面浏览量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 09:41
查看更多