google-analytics - 使用 AngularJS 跟踪 Google Analytics 页面浏览量

标签 google-analytics angularjs

我正在使用 AngularJS 作为前端来设置一个新应用程序。客户端上的一切都是通过 HTML5 Pushstate 完成的,我希望能够在 Google Analytics 中跟踪我的页面浏览量。

最佳答案

如果您使用ng-view在 Angular 应用程序中,您可以收听 $viewContentLoaded事件并将跟踪事件推送到 Google Analytics。

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

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

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

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

关于google-analytics - 使用 AngularJS 跟踪 Google Analytics 页面浏览量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10713708/

相关文章:

javascript - 无法在 Google Analytics 上设置不同的标题

javascript - Google Analytics API for Apps 脚本中的有机搜索段

google-analytics - 如何从 Google Click ID (gclid) 中提取信息?

javascript - 在 ng-repeat 上更改一次迭代

google-analytics - 谷歌分析 : Show User ID view report day by day

Android Google Analytics 配置生成不起作用

javascript - 没有标签的 AngularJS 动态路由

html - 如何使用 bootstrap 和 angularjs 显示和隐藏按钮?

javascript - 无法通过 AngularJs 加载 Google map

javascript - 在哪里放置资源特定逻辑