Reputation: 43
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
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
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