How to set up Google Analytics for React-Router?

后端 未结 15 685
名媛妹妹
名媛妹妹 2020-12-12 12:25

I\'m trying set up Google Analytics on my react site, and have come across a few packages, but none of which has the kind of set up that I have in terms of examples. Was ho

15条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 13:04

    If you use hash or browser history you can do:

    import trackingHit from 'tracking';
    
    import { Router, browserHistory } from 'react-router';
    browserHistory.listen(trackingHit);
    // OR
    import { Router, hashHistory } from 'react-router';
    hashHistory.listen(trackingHit);
    

    where ./tracking.es6

    export default function(location) {
        console.log('New page hit', location.pathname);
        // Do your shizzle here
    }
    

提交回复
热议问题