Gurkenkönig
Gurkenkönig

Reputation: 828

ngx-google-analytics with cookie consent in angular

I have ngx-google-analytics built into my website and it works very well so far. Problem: I live in the EU and now I need a cookie consent banner that should be able to turn tracking with ngx-google-analytics on or off. Can you somehow load modules dynamically? Or is there another way to activate the plugin only after consent?

Upvotes: 0

Views: 1042

Answers (1)

Jimmy
Jimmy

Reputation: 1429

The library is using APP_INITIALIZER token, which loads at start up time, before anything else in your Angular application. You can check the source code here. Scroll down near the bottom, you will see it appends the google-tag-script into the DOM.

And here is where it uses gtag to send data to the server

I don't know much about GTM, but as in your description, I can make an assumption that the solution should be either do not let the library load at all or to stop it from sending data to server.

  • Because of APP_INITIALIZER token, it cannot wait until users click OK in the cookie consent banner
  • Because there's no if/else, meaning no condition you can put to stop it from sending data to server once it loaded.
  • You said it yourself, this option window['ga-disable-UA-XXXXX-Y'] = true; won't work.

Conclusion: unfortunately, with this current library, you cannot achieve what you want.

Upvotes: 1

Related Questions