Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Firebase Analytics after Opt-In

I am trying to use Firebase´s Google Analytics for my Nextjs App.

Problem is i live in Germany and i am only allowed to use Analytics after user Opt-in.

My Config looks like that:

import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";

const firebaseConfig = {
  //my Config
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

I have a Cookie Consent Component where the user can Accept or Decline the use of Cookies. On Accept i want to initialize Firebase analytics. Is it possible to export the analytics and initialize it in the Cookie Consent Component after the user Input?

like image 766
H2OSANI Avatar asked Nov 28 '25 17:11

H2OSANI


1 Answers

I am kind of getting swamped by this topic too. Perhaps you could initialize with const analytics = getAnalytics(app).setAnalyticsCollectionEnabled(false); and then set it true on accept.

like image 166
Filip Rejmus Avatar answered Dec 01 '25 08:12

Filip Rejmus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!