I have multiple angular applications that have been tracking page views with logic similar to the one described in this blog.
I will also add the relevant code pieces here to avoid third party reference.
Adding gtag dependency in the head of every page (this is also described in Google analytics docs):
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
</script>
React to router events so that you tell GA to use the new page title, although Angular apps are SPAa:
export class AppRoutingModule {
constructor(public router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
gtag('config', 'GA_MEASUREMENT_ID',
{'anonymize_ip': true},
{
'page_path':
event.urlAfterRedirects
}
);
}
});
}
}
Obviously, I replace GA_MEASUREMENT_ID
with my real tracking id. So this has been working previously in all cases in which I had a 'UA-xxxxxxx' (universal analytics) tracking id. Now, for the first time I got Google Analytics 4 tracking id (G-xxxxx) and things just don't want to work. No page views are tracked.
What I have tried:
- I am debugging using the real time tracking view, which is also changed for the new analytics. Still, I hoped that what I want will be tracked in the page view section. Still, only the hostname is visible there for me.
- I noticed that these analytics have different configuration, e.g. there is data stream configuration, which I am not sure the purpose of, but I assumed I need to add my host there. I added it and still my page views are not visible.
- This tracking id is created through google tag manager and I initially tried configuring the dependency using the Google Tag manager instructions, but as this did not work out, I switched back to the analytics tracking configuration described in my post.
- Now I am writing a SO question with the hope that someone else already got this working and he/she will help me.