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

View File

@ -87,6 +87,19 @@ function restoreScroll() {
}, 200); }, 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 */ /* global process: false */
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
// some shortcuts for testing on localhost // some shortcuts for testing on localhost