我正在使用Durandal1.2和Durandal Router Plugin,并希望通过Google Analytics跟踪SPA中的页面浏览量:window._gaq.push(['_trackPageview', location.pathname + location.search + location.hash]
我知道我可以监听hashchange事件,甚至可以通过Sammy连接。我不想这么做,因为Durandal目前正在使用rewritten来消除对Sammy的依赖。
我的问题是,有没有一种方法可以使用Durandal路由器插件来设置?
发布于 2013-06-06 07:18:31
查看路由器中的onNavigationComplete。你可以覆盖它来做你的分析:
// Store reference to router's onNavigationComplete so you can call it
var onNavigationComplete = router.onNavigationComplete;
router.onNavigationComplete = function (routeInfo, params, module) {
// Run the default onNavigationComplete
onNavigationComplete.call(this, routeInfo, params, module);
// Analytics!
window._gaq.push(['_trackPageview', location.pathname + location.search + location.hash]
};显然,您需要在应用程序生命周期的早期执行此操作,例如在main.js中。
发布于 2013-08-30 23:24:43
您还可以附加到navigation complete事件,这样就不必存储路由器的原始功能。
//Update anaytics whenever the router navigates
router.on('router:navigation:complete', function(instance, instruction) {
window._gaq.push(['_trackPageview', instruction.fragment]);
});https://stackoverflow.com/questions/16951388
复制相似问题