After implementing Google Analytics (GA) in my Angular single page application and placing the tracking code at the bottom of the main index.html, I encountered an issue regarding incorrect pageview results.
<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","//www.google-analytics.com/analytics.js","ga");
ga("create", "UA-56056824-2", "auto");
ga("send", "pageview");
</script>
To ensure that a new pageview is triggered every time a user navigates to a new view, I added a listener for $viewContentLoaded
which then sends a pageview to GA.
app.run(["$rootScope", "$location", "$window", function($rootScope, $location, $window){
$rootScope.$on("$viewContentLoaded", function(event){
$window.ga("send", "pageview", { page: $location.url() });
});
}]);
Despite this setup working well, I noticed an unusually low bounce rate of only 3%. Upon further investigation, it became clear that the double pageview was causing this discrepancy. The first pageview occurs when the user visits the homepage triggering the GA script, followed by a second pageview upon the view loading via the JavaScript code above.
It's evident that this approach leads to inaccurate pageview metrics and bounce rates. How can I correct this setup for more precise analytics?