H2OSANI
H2OSANI

Reputation: 43

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?

Upvotes: 4

Views: 544

Answers (2)

Marko Marinkovic
Marko Marinkovic

Reputation: 376

I have this function inside my firebase file and call it when users click on "accept". Works great

export function setCookies() {
  if (app.name && typeof window !== 'undefined') {
    const analytics = getAnalytics(app)
  }
}

Upvotes: 0

Filip Rejmus
Filip Rejmus

Reputation: 50

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.

Upvotes: 1

Related Questions