-
Notifications
You must be signed in to change notification settings - Fork 927
Add initializeAnalytics()
to analytics-exp
#4575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Size Analysis ReportAffected Products
|
// Async but non-blocking. | ||
// This map reflects the completion state of all promises for each appId. | ||
initializationPromisesMap[appId] = initializeAnalytics( | ||
// Factory will have thrown if options has no appId. | ||
initializationPromisesMap[app.options.appId!] = _initializeAnalytics( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After giving it some thought, I realize we can't do initialization outside the factory, otherwise FCM, which depends on Analytics, might try to use an uninitialized Analytics service, as shown in the below example:
import { initializeApp } from 'firebase/app';
import { getMessaging, onMessage } from 'firebase/messaging';
import { getAnalytics } from 'firebase/analytics';
const app = initializeApp({...});
/**
* Messaging calls `const analytics = await messaging.firebaseDependencies.analyticsProvider.get();`
* which will create and return an uninitialized Analytics service.
* And it will probably throw when calling `analytics.logEvent(...)`
*/
const msg = getMessaging(app);
onMessage(msg, () => {
// process messages
});
// getAnalytics async after a timeout
setTimeout(() => getAnalytics(), 1000);
We should let analytics initialize with the default options in this situation.
To support this, some changes to the component framework need to be made, e.g. allow it to take an option and pass it to the factory.
I need to think more about it and let's chat offline. This PR will need to be put on hold for now, Sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4595 is merged. We can use Provider.initialize()
to initialize Auth in the factory function.
ac5ba47
to
ae97224
Compare
@@ -121,12 +123,13 @@ export async function initializeAnalytics( | |||
// We keep it together with other initialization logic for better code structure. | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
(gtagCore as any)('js', new Date()); | |||
// User config added first. We don't want users to accidentally overwrite | |||
// base Firebase config properties. | |||
const configProperties: Record<string, unknown> = options?.config ?? {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! a lot of ?s
resource.name.includes('en=login') | ||
); | ||
if (callsWithEvent.length === 0) { | ||
return checkForEventCalls(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a limit on the number of retries, otherwise it may run forever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the karma timeout will catch it but I added a timeout and extracted the function, which helped me realize the second test was not correct (performance entries were not cleared from the browser between tests), so I fixed it.
ae97224
to
aa563ba
Compare
Create an
initializeAnalytics()
method that can only be called once, which can take anAnalyticsOptions
object with an initial config. Allows users to set useful custom gtag config parameters such assend_page_view
on initialization of analytics.Updated some documentation comments.
Design doc (internal link):
go/firebase-analytics-initial-config