ui-router获取谷歌分析的状态变化的当前路径

时间:2014-12-14 22:21:05

标签: angularjs angular-ui-router

我正在尝试将状态路径发送到Google Analytics。有一些问题。我正在使用抽象状态,所以使用像toState.url这样的东西不会工作,因为它不会抓住整个网址。

我想在$ stateChangeSuccess上使用$ window.location.pathname,但是在浏览器中更新url之前,它会成功触发。这意味着网页浏览过晚发送1页面视图。即

click about: sends nothing
click contact: sends about url
click services: sends contact url

基本上,最终结果看起来应该是这样的,但使用toState url或pathname:

$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
  var path = ???; 
  ga('send', 'pageview', path);
});

1 个答案:

答案 0 :(得分:4)

看起来您需要使用$ location来获取新路径:

$rootScope.$on("$stateChangeSuccess", function(event, toState, toParams, fromState, fromParams) {
  $window.ga('send', 'pageview', $location.path());
});