Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Angular: (re)activate google analytics (cookies after user consented to all cookies

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!