Last active
October 18, 2016 15:41
-
-
Save liesislukas/da29cc8e0e7f44d43251e77f1d0415e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
To enable google analytics with autotracker do these steps: | |
1. Add analytics script to head, i used <Helmet /> so one more entry to scripts array: | |
{async: true, src: 'https://www.google-analytics.com/analytics.js'}, | |
2. add code below to routes.js | |
P.s. to generate static files this line is added, because document obj. is missing at that moment. | |
if (typeof document !== 'undefined') { | |
} | |
*/ | |
if (typeof document !== 'undefined') { | |
require('autotrack'); // Google analytics | |
ga('create', 'UA-00000000-1', 'auto'); | |
ga('require', 'cleanUrlTracker', { | |
trailingSlash: 'add', | |
}); | |
ga('require', 'outboundLinkTracker'); | |
ga('require', 'urlChangeTracker', { | |
shouldTrackUrlChange: (newPath, oldPath) => { | |
return newPath && oldPath; | |
} | |
}); | |
ga('require', 'pageVisibilityTracker'); // session timeouts if it's left in some tab | |
ga('require', 'socialWidgetTracker'); | |
ga('require', 'eventTracker', { | |
events: ['click'], | |
attributePrefix: 'data-ga-', | |
}); | |
} | |
const _route_change_handler = () => { | |
if (typeof ga !== 'undefined') { | |
ga('send', 'pageview'); | |
} | |
}; | |
export default ( | |
<Route | |
... | |
onChange={(prevState, nextState, replace) => { | |
_route_change_handler(); | |
return true; | |
}} | |
onEnter={(nextState) => { | |
_route_change_handler(); | |
return true; | |
}} | |
... | |
> | |
... | |
</Route> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks awesome!