GA SPA integration

This commit is contained in:
SleepWalker 2016-11-20 14:01:57 +02:00
parent 251a1237b4
commit e0a1f0c845
2 changed files with 19 additions and 10 deletions

View File

@ -29,16 +29,12 @@
}
%>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '<%- htmlWebpackPlugin.options.ga.id %>', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '<%- htmlWebpackPlugin.options.ga.id %>', 'auto');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
<% } %>
</body>
</html>

View File

@ -87,6 +87,19 @@ function restoreScroll() {
}, 200);
}
browserHistory.listen(trackPageView);
function trackPageView(location) {
const ga = window.ga;
if (!ga) {
return;
}
ga('set', 'page', location.pathname + location.search);
ga('send', 'pageview');
}
/* global process: false */
if (process.env.NODE_ENV !== 'production') {
// some shortcuts for testing on localhost