firebase-analytics
firebase-analytics copied to clipboard
Web: Race condition with initializeFirebase
Describe the bug FirebaseAnaltyics behaves badly if don't wait for initializeFirebase to return, and call trackEvent.
To Reproduce
export async function initFirebase() {
return FirebaseAnalytics.initializeFirebase(firebaseConfig).then(() => {
FirebaseAnalytics.setCollectionEnabled({
enabled: true,
});
})
}
export const firebaseReady = initFirebase()
## if you call FirebaseAnalytics.trackEvent() here, without waiting for firebaseReady, you'll get a warning that firebase was initialized twice, and the setCollectionEnabled call will fail.
Expected behavior There are already some readyhandlers coded in FirebaseAnalyticsWeb. i expect them to take care of this situation automatically. Eg: If you call trackEvent before initializeFirebase, i expect them to error. if you call trackEvent after initializeFirebase, but before it's complete... it should wait on the promise internally and exectute.
At a minimum, it should give a more appropriate error message.
version info @capacitor-community/firebase-analytics: 4.0.0, @capacitor/core: 4.5.0,
Faced an identical issue. Here's how I tackled it. Hope it helps.
(async () => {
dayjs.locale(activeLocale);
if (Capacitor.getPlatform() === "web") {
console.log("Firebase Analytics Initializing...");
await FirebaseAnalytics.initializeFirebase({
apiKey: "AIzaSyCUUQE4ntyjztCMSjWSpmoO0TtXHSCWop8",
authDomain: "shredy-test.firebaseapp.com",
databaseURL: "https://shredy-test.firebaseio.com",
projectId: "shredy-test",
storageBucket: "shredy-test.appspot.com",
messagingSenderId: "799608757722",
appId: "1:799608757722:web:ec6ed28de552a4bfe574e3",
measurementId: "G-04CYHDSB4K",
});
await FirebaseAnalytics.setCollectionEnabled({
enabled: true,
});
console.log("Firebase Analytics Initialized");
}
ReactDOM.render(<App />, document.getElementById("root"));
// Call the element loader after the app has been rendered the first time
defineCustomElements(window);
})();