ui-router 获取谷歌分析状态变化的当前路径 [英] ui-router getting current path on state change for google analytics
问题描述
我正在尝试获取要发送到谷歌分析的状态路径.有一些问题.我使用的是抽象状态,所以使用像 toState.url 这样的东西是行不通的,因为它不会抓取整个 url.
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.
我想过在 $stateChangeSuccess 上使用 $window.location.pathname,但结果是在浏览器中更新 url 之前成功触发.这意味着网页浏览量太晚了 1 次网页浏览量.即.
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
基本上最终的结果应该是这样的,但是使用 toState 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);
});
推荐答案
看起来您需要使用 $location 来获取新路径:
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-router 获取谷歌分析状态变化的当前路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!