From 3dfa264366557118e570e31b8d8a4e68bbd92c29 Mon Sep 17 00:00:00 2001 From: "tom.vanhowe" Date: Thu, 4 Dec 2025 13:15:29 +0000 Subject: [PATCH 1/3] Handle error loading scripts in web by rejecting the promise --- src/web.ts | 78 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/web.ts b/src/web.ts index 724b0d7..f8fab91 100644 --- a/src/web.ts +++ b/src/web.ts @@ -6,7 +6,8 @@ declare var window: any; export class FirebaseAnalyticsWeb extends WebPlugin - implements FirebaseAnalyticsPlugin { + implements FirebaseAnalyticsPlugin +{ private not_supported_mssg = "This method is not supported"; private options_missing_mssg = "Firebase options are missing"; private duplicate_app_mssg = "Firebase app already exists"; @@ -15,6 +16,7 @@ export class FirebaseAnalyticsWeb public readonly ready: Promise; private readyResolver: Function; + private readyReject: Function; private analyticsRef: any; private scripts = [ @@ -30,7 +32,11 @@ export class FirebaseAnalyticsWeb constructor() { super(); - this.ready = new Promise((resolve) => (this.readyResolver = resolve)); + this.ready = new Promise( + (resolve, reject) => ( + (this.readyResolver = resolve), (this.readyReject = reject) + ), + ); this.configure(); } @@ -40,10 +46,9 @@ export class FirebaseAnalyticsWeb * @returns firebase analytics object reference * Platform: Web */ - initializeFirebase(options: FirebaseInitOptions): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async initializeFirebase(options: FirebaseInitOptions): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (this.hasFirebaseInitialized()) { reject(this.duplicate_app_mssg); return; @@ -65,10 +70,9 @@ export class FirebaseAnalyticsWeb * @param options - userId: unique identifier of the user to log * Platform: Web/Android/iOS */ - setUserId(options: { userId: string }): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async setUserId(options: { userId: string }): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -92,10 +96,12 @@ export class FirebaseAnalyticsWeb * value: The value of the user property. * Platform: Web/Android/iOS */ - setUserProperty(options: { name: string; value: string }): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async setUserProperty(options: { + name: string; + value: string; + }): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -156,10 +162,9 @@ export class FirebaseAnalyticsWeb * params: the map of event parameters. * Platform: Web/Android/iOS */ - logEvent(options: { name: string; params: object }): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async logEvent(options: { name: string; params: object }): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -185,10 +190,9 @@ export class FirebaseAnalyticsWeb * @param options - enabled: boolean true/false to enable/disable logging * Platform: Web/Android/iOS */ - setCollectionEnabled(options: { enabled: boolean }): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async setCollectionEnabled(options: { enabled: boolean }): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -218,10 +222,9 @@ export class FirebaseAnalyticsWeb return this.analyticsRef; } - enable(): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async enable(): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -232,10 +235,9 @@ export class FirebaseAnalyticsWeb }); } - disable(): Promise { - return new Promise(async (resolve, reject) => { - await this.ready; - + async disable(): Promise { + await this.ready; + return new Promise((resolve, reject) => { if (!this.analyticsRef) { reject(this.analytics_missing_mssg); return; @@ -261,7 +263,8 @@ export class FirebaseAnalyticsWeb this.analyticsRef = window.firebase.analytics(); } } catch (error) { - throw error; + this.readyReject(error); + return; } const interval = setInterval(() => { @@ -289,12 +292,15 @@ export class FirebaseAnalyticsWeb return resolve(null); } - await this.loadScript(firebaseAppScript.key, firebaseAppScript.src); - await this.loadScript( - firebaseAnalyticsScript.key, - firebaseAnalyticsScript.src + resolve( + Promise.all([ + this.loadScript(firebaseAppScript.key, firebaseAppScript.src), + this.loadScript( + firebaseAnalyticsScript.key, + firebaseAnalyticsScript.src, + ), + ]), ); - resolve(null); }); } From cdce92af848a9a0ee0dcb7d2d8eb25772858061e Mon Sep 17 00:00:00 2001 From: "tom.vanhowe" Date: Tue, 9 Dec 2025 15:38:03 +0000 Subject: [PATCH 2/3] Load firebase app script before analytics script on web --- src/web.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/web.ts b/src/web.ts index f8fab91..11ea8da 100644 --- a/src/web.ts +++ b/src/web.ts @@ -294,10 +294,12 @@ export class FirebaseAnalyticsWeb resolve( Promise.all([ - this.loadScript(firebaseAppScript.key, firebaseAppScript.src), - this.loadScript( - firebaseAnalyticsScript.key, - firebaseAnalyticsScript.src, + this.loadScript(firebaseAppScript.key, firebaseAppScript.src).then( + () => + this.loadScript( + firebaseAnalyticsScript.key, + firebaseAnalyticsScript.src, + ), ), ]), ); From 702f659d9aac1b14b34839505780ef55e0f379f9 Mon Sep 17 00:00:00 2001 From: "tom.vanhowe" Date: Tue, 9 Dec 2025 15:38:46 +0000 Subject: [PATCH 3/3] Remove promise.all call from loadScripts --- src/web.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/web.ts b/src/web.ts index 11ea8da..1d24879 100644 --- a/src/web.ts +++ b/src/web.ts @@ -293,15 +293,12 @@ export class FirebaseAnalyticsWeb } resolve( - Promise.all([ - this.loadScript(firebaseAppScript.key, firebaseAppScript.src).then( - () => - this.loadScript( - firebaseAnalyticsScript.key, - firebaseAnalyticsScript.src, - ), + this.loadScript(firebaseAppScript.key, firebaseAppScript.src).then(() => + this.loadScript( + firebaseAnalyticsScript.key, + firebaseAnalyticsScript.src, ), - ]), + ), ); }); }