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

cod*_*bia 5 angularjs angular-ui-router

我正试图让州的路径发送到谷歌分析.有一些问题.我正在使用抽象状态,所以使用像toState.url这样的东西不会工作,因为它不会抓住整个网址.

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

click about: sends nothing
click contact: sends about url
click services: sends contact url
Run Code Online (Sandbox Code Playgroud)

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

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

cod*_*bia 4

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

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