javascript - Durandal Google Analtyics Tracking - Stack Overflow

I'm using Durandal 1.2 and the Durandal Router Plugin and wanting to track page views in the SPA t

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745386480a4625461.html

相关推荐

  • javascript - Durandal Google Analtyics Tracking - Stack Overflow

    I'm using Durandal 1.2 and the Durandal Router Plugin and wanting to track page views in the SPA t

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信