最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Durandal Google Analtyics Tracking - Stack Overflow

programmeradmin2浏览0评论

I'm using Durandal 1.2 and the Durandal Router Plugin and wanting to track page views in the SPA through Google Analytics: window._gaq.push(['_trackPageview', location.pathname + location.search + location.hash]

I know I could listen for the hashchange event or even hook into via Sammy. I'd rather not do that given Durandal is currently being rewritten to remove the dependency on Sammy.

My question is, is there a way to set this up using the Durandal Router Plugin?

I'm using Durandal 1.2 and the Durandal Router Plugin and wanting to track page views in the SPA through Google Analytics: window._gaq.push(['_trackPageview', location.pathname + location.search + location.hash]

I know I could listen for the hashchange event or even hook into via Sammy. I'd rather not do that given Durandal is currently being rewritten to remove the dependency on Sammy.

My question is, is there a way to set this up using the Durandal Router Plugin?

Share Improve this question edited Oct 19, 2015 at 16:03 GrandMasterFlush 6,40919 gold badges86 silver badges109 bronze badges asked Jun 5, 2013 at 23:15 Drew FreylingDrew Freyling 1,25813 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Have a look at onNavigationComplete in the router. You could override that to do your analytics:

// 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]
};

Obviously you'll need to do this somewhere very early in your application's lifecycle, such as in your main.js.

You could also attach to the navigation plete event, so that you don't have to store the router's original function.

//Update anaytics whenever the router navigates
router.on('router:navigation:plete', function(instance, instruction) {
    window._gaq.push(['_trackPageview', instruction.fragment]);
});
发布评论

评论列表(0)

  1. 暂无评论