为了让Alexa在基于Angular的应用程序上工作,我下载了Alexa代码片段并对其进行了分析.
似乎Alexa依赖于这个功能:
fire: function (e) {
this.user_cookie_v = this.muc();
this.map(e, function (e, t) {
this.opts[e] = t
});
// this.fired() check if "window._atrk_fired" is truthy
if (this.fired()) {
return
}
window._atrk_fired = true;
var t = new Image(1, 1);
t.alt = "alexametrics";
t.src = this.gen_url();
this.cloudfront.fire()
}
因为,在Angular应用程序中,页面没有被重新加载,事件fire()只在“window._atrk_fired”保持真实之后被触发一次.
我必须添加此代码才能模拟页面重新加载:
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
console.log('setting alexa fire value to false');
window._atrk_fired = false;
})
我知道这个hack不太可靠,因为Alexa可能会更改pageView跟踪策略.但我的目标是看看Alexa是否可以跟踪Angular应用程序我想知道您对此事的看法?
最佳答案 当你将“window._atrk_fired”设置为false时,你必须通过调用atrk()重新运行fire方法,这样你的路由更改事件应该是这样的
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
console.log('setting alexa fire value to false');
window._atrk_fired = false;
atrk();
})