Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda