I added googleanalytcis js (analytics.js) to index.html of my angular app.
<!-- 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');
</script>
<!-- End Google Analytics -->
I initiate google analytics (ga) in my custom angular service with or without cookies depending on wether the user has (already) agreed to all cookies. This happens when the app is loaded (and the user has not yet agreed to all cookies). If the users has allowed cookies the decision is stored in a cookie as well (cookieConsent).
if(cookieConsent){
this.ga('create', gaId, "auto");
}else{
this.ga('create', gaId, {"storage": "none"}); //block ga cookies (e.g if user has not (yet) allowed cookies)
}
Later, if the user has agreed to all cookies I want to reinitiate ga in order to load the ga cookies (_ga, _gid). My current attempt is as follows:
public onCookieConsentChanged(event){
if(event.allowed){
this.ga('create', gaId, "auto");
}
}
Unfortunately the ga cookies do not appear after onCookieConsentChanged is called (except after full page reload). How can I properly reinitiate ga in order that the cookies load?