UI的路由器上的谷歌分析状态变化获取当前路径 [英] ui-router getting current path on state change for google analytics

查看:110
本文介绍了UI的路由器上的谷歌分析状态变化获取当前路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得国家的路径发送到Google Analytics。有一些问题。我使用的抽象状态,所以使用类似toState.url不会工作,因为它不会抢了整个网址。

我想用在$ stateChangeSuccess $ window.location.pathname的,但事实证明,成功火灾网址在浏览器更新前。这意味着,浏览量被发送1页视图为时已晚。即

 单击一下:什么都不发
点击联系人:发送有关网址
单击服务:发送URL接触

基本上,最终的结果应该是这个样子,但与使用toState URL或路径:

  $ rootScope。在$($ stateChangeStart功能(事件,toState,toParams,fromState,fromParams){
  VAR路径= ???;
  GA(发送,浏览量,路径);
});


解决方案

看起来你需要使用$位置,而不是获得新的路径:

  $ rootScope。在$($ stateChangeSuccess功能(事件,toState,toParams,fromState,fromParams){
  $ window.ga(发送,浏览量,$ location.path());
});

I am trying to get the path of the state to send to google analytics. There are some issues. I am using abstract states, so using something like toState.url wont work as it wont grab the entire url.

I thought of using $window.location.pathname on $stateChangeSuccess, but it turns out success fires before the url is updated in the browser. This means that pageviews get sent 1 page view too late. ie.

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

Basically the end result should look something like this, but with the toState url or pathname:

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

解决方案

looks like you need to use $location instead to get the new path:

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

这篇关于UI的路由器上的谷歌分析状态变化获取当前路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆