Skip to content

Commit 405dbb7

Browse files
committed
Address PR comments
1 parent ca367fd commit 405dbb7

File tree

13 files changed

+115
-109
lines changed

13 files changed

+115
-109
lines changed

common/api-review/analytics-exp.api.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ import { DynamicConfig } from '@firebase/analytics-types-exp';
1111
import { EventNameString } from '@firebase/analytics-types-exp';
1212
import { EventParams } from '@firebase/analytics-types-exp';
1313
import { FirebaseApp } from '@firebase/app-types-exp';
14-
import { FirebaseInstallations } from '@firebase/installations-types-exp';
14+
import { _FirebaseInstallationsInternal } from '@firebase/installations-types-exp';
1515
import { _FirebaseService } from '@firebase/app-types-exp';
1616
import { MinimalDynamicConfig } from '@firebase/analytics-types-exp';
1717
import { SettingsOptions } from '@firebase/analytics-types-exp';
1818

19+
// @public
20+
export function analyticsSettings(options: SettingsOptions): void;
21+
1922
// Warning: (ae-forgotten-export) The symbol "AnalyticsService" needs to be exported by the entry point index.d.ts
2023
// Warning: (ae-internal-missing-underscore) The name "factory" should be prefixed with an underscore because the declaration is marked as @internal
2124
//
2225
// @internal
23-
export function factory(app: FirebaseApp, installations: FirebaseInstallations): AnalyticsService;
26+
export function factory(app: FirebaseApp, installations: _FirebaseInstallationsInternal): AnalyticsService;
2427

2528
// @public
2629
export function getAnalytics(app: FirebaseApp): Analytics;
@@ -233,9 +236,6 @@ export function setAnalyticsCollectionEnabled(analyticsInstance: Analytics, enab
233236
// @public
234237
export function setCurrentScreen(analyticsInstance: Analytics, screenName: string, options?: AnalyticsCallOptions): void;
235238

236-
// @public
237-
export function settings(options: SettingsOptions): void;
238-
239239
// @public
240240
export function setUserId(analyticsInstance: Analytics, id: string, options?: AnalyticsCallOptions): void;
241241

packages-exp/analytics-exp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"bugs": {
5656
"url": "https://github.com/firebase/firebase-js-sdk/issues"
5757
},
58-
"typings": "dist/index.d.ts",
58+
"typings": "dist/analytics-exp-public.d.ts",
5959
"nyc": {
6060
"extension": [
6161
".ts"

packages-exp/analytics-exp/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
setAnalyticsCollectionEnabled as internalSetAnalyticsCollectionEnabled
4949
} from './functions';
5050

51-
export { settings } from './factory';
51+
export { analyticsSettings } from './factory';
5252

5353
declare module '@firebase/component' {
5454
interface NameServiceMapping {

packages-exp/analytics-exp/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export enum GtagCommand {
4141
* Officially recommended event names for gtag.js
4242
* Any other string is also allowed.
4343
*/
44-
export enum EventName {
44+
export const enum EventName {
4545
ADD_SHIPPING_INFO = 'add_shipping_info',
4646
ADD_PAYMENT_INFO = 'add_payment_info',
4747
ADD_TO_CART = 'add_to_cart',

packages-exp/analytics-exp/src/factory.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import {
2929
findGtagScriptOnPage
3030
} from './helpers';
3131
import { AnalyticsError, ERROR_FACTORY } from './errors';
32-
import { FirebaseInstallations } from '@firebase/installations-types-exp';
32+
import { _FirebaseInstallationsInternal } from '@firebase/installations-types-exp';
3333
import { areCookiesEnabled, isBrowserExtension } from '@firebase/util';
34-
import { initializeIds } from './initialize-ids';
34+
import { initializeAnalytics } from './initialize-ids';
3535
import { logger } from './logger';
3636
import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp';
3737

@@ -137,14 +137,14 @@ export function getGlobalVars(): {
137137
* Intended to be used if `gtag.js` script has been installed on
138138
* this page independently of Firebase Analytics, and is using non-default
139139
* names for either the `gtag` function or for `dataLayer`.
140-
* Must be called before calling `firebase.analytics()` or it won't
140+
* Must be called before calling `getAnalytics()` or it won't
141141
* have any effect.
142142
*
143143
* @public
144144
*
145145
* @param options - Custom gtag and dataLayer names.
146146
*/
147-
export function settings(options: SettingsOptions): void {
147+
export function analyticsSettings(options: SettingsOptions): void {
148148
if (globalInitDone) {
149149
throw ERROR_FACTORY.create(AnalyticsError.ALREADY_INITIALIZED);
150150
}
@@ -186,7 +186,7 @@ function warnOnBrowserContextMismatch(): void {
186186
*/
187187
export function factory(
188188
app: FirebaseApp,
189-
installations: FirebaseInstallations
189+
installations: _FirebaseInstallationsInternal
190190
): AnalyticsService {
191191
warnOnBrowserContextMismatch();
192192
const appId = app.options.appId;
@@ -234,7 +234,7 @@ export function factory(
234234
}
235235
// Async but non-blocking.
236236
// This map reflects the completion state of all promises for each appId.
237-
initializationPromisesMap[appId] = initializeIds(
237+
initializationPromisesMap[appId] = initializeAnalytics(
238238
app,
239239
dynamicConfigPromisesList,
240240
measurementIdToAppId,

packages-exp/analytics-exp/src/helpers.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
getOrCreateDataLayer,
2424
insertScriptTag,
2525
wrapOrCreateGtag,
26-
findGtagScriptOnPage
26+
findGtagScriptOnPage,
27+
promiseAllSettled
2728
} from './helpers';
2829
import { GtagCommand } from './constants';
2930
import { Deferred } from '@firebase/util';
@@ -99,7 +100,7 @@ describe('Gtag wrapping functions', () => {
99100

100101
initPromise2.resolve('other-measurement-id'); // Resolves second initialization promise.
101102
await Promise.all([initPromise1, initPromise2]); // Wait for resolution of Promise.all()
102-
await Promise.all(fakeDynamicConfigPromises);
103+
await promiseAllSettled(fakeDynamicConfigPromises);
103104

104105
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
105106
});
@@ -132,7 +133,7 @@ describe('Gtag wrapping functions', () => {
132133

133134
initPromise2.resolve(); // Resolves second initialization promise.
134135
await Promise.all([initPromise1, initPromise2]); // Wait for resolution of Promise.all()
135-
await Promise.all(fakeDynamicConfigPromises);
136+
await promiseAllSettled(fakeDynamicConfigPromises);
136137

137138
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
138139
}
@@ -194,7 +195,7 @@ describe('Gtag wrapping functions', () => {
194195
expect((window['dataLayer'] as DataLayer).length).to.equal(0);
195196

196197
initPromise1.resolve(); // Resolves first initialization promise.
197-
await Promise.all(fakeDynamicConfigPromises);
198+
await promiseAllSettled(fakeDynamicConfigPromises);
198199
await Promise.all([initPromise1]); // Wait for resolution of Promise.all()
199200

200201
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
@@ -240,7 +241,7 @@ describe('Gtag wrapping functions', () => {
240241
expect((window['dataLayer'] as DataLayer).length).to.equal(0);
241242

242243
initPromise1.resolve(fakeMeasurementId);
243-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
244+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
244245
expect((window['dataLayer'] as DataLayer).length).to.equal(0);
245246

246247
await Promise.all([initPromise1]); // Wait for resolution of Promise.all()
@@ -254,7 +255,7 @@ describe('Gtag wrapping functions', () => {
254255
(window['gtag'] as Gtag)(GtagCommand.CONFIG, fakeMeasurementId, {
255256
'transaction_id': 'abcd123'
256257
});
257-
await Promise.all(fakeDynamicConfigPromises);
258+
await promiseAllSettled(fakeDynamicConfigPromises);
258259
await Promise.resolve(); // Config call is always chained onto initialization promise list, even if empty.
259260
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
260261
});
@@ -294,7 +295,7 @@ describe('Gtag wrapping functions', () => {
294295
expect(existingGtagStub).to.not.be.called;
295296

296297
initPromise2.resolve(); // Resolves second initialization promise.
297-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
298+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
298299
expect(existingGtagStub).to.not.be.called;
299300

300301
await Promise.all([initPromise1, initPromise2]); // Wait for resolution of Promise.all()
@@ -331,7 +332,7 @@ describe('Gtag wrapping functions', () => {
331332
expect(existingGtagStub).to.not.be.called;
332333

333334
initPromise2.resolve(); // Resolves second initialization promise.
334-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
335+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
335336
expect(existingGtagStub).to.not.be.called;
336337

337338
await Promise.all([initPromise1, initPromise2]); // Wait for resolution of Promise.all()
@@ -406,7 +407,7 @@ describe('Gtag wrapping functions', () => {
406407
expect(existingGtagStub).to.not.be.called;
407408

408409
initPromise1.resolve(); // Resolves first initialization promise.
409-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
410+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
410411
expect(existingGtagStub).to.not.be.called;
411412

412413
await Promise.all([initPromise1]); // Wait for resolution of Promise.all()
@@ -462,7 +463,7 @@ describe('Gtag wrapping functions', () => {
462463
expect(existingGtagStub).to.not.be.called;
463464

464465
initPromise1.resolve(fakeMeasurementId);
465-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
466+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
466467
expect(existingGtagStub).to.not.be.called;
467468

468469
await Promise.all([initPromise1]); // Wait for resolution of Promise.all()
@@ -482,7 +483,7 @@ describe('Gtag wrapping functions', () => {
482483
(window['gtag'] as Gtag)(GtagCommand.CONFIG, fakeMeasurementId, {
483484
'transaction_id': 'abcd123'
484485
});
485-
await Promise.all(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
486+
await promiseAllSettled(fakeDynamicConfigPromises); // Resolves dynamic config fetches.
486487
expect(existingGtagStub).to.not.be.called;
487488
await Promise.resolve(); // Config call is always chained onto initialization promise list, even if empty.
488489
expect(existingGtagStub).to.be.calledWith(

packages-exp/analytics-exp/src/helpers.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ import {
2727
import { GtagCommand, GTAG_URL } from './constants';
2828
import { logger } from './logger';
2929

30+
/**
31+
* Makeshift polyfill for Promise.allSettled()
32+
*
33+
* @param promises Array of promises to wait for.
34+
*/
35+
export function promiseAllSettled<T>(
36+
promises: Array<Promise<T>>
37+
): Promise<T[]> {
38+
return Promise.all(promises.map(promise => promise.catch(e => e)));
39+
}
40+
3041
/**
3142
* Inserts gtag script tag into the page to asynchronously download gtag.
3243
* @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
@@ -86,7 +97,9 @@ async function gtagOnConfig(
8697
// find the appId (if any) corresponding to this measurementId. If there is one, wait on
8798
// that appId's initialization promise. If there is none, promise resolves and gtag
8899
// call goes through.
89-
const dynamicConfigResults = await Promise.all(dynamicConfigPromisesList);
100+
const dynamicConfigResults = await promiseAllSettled(
101+
dynamicConfigPromisesList
102+
);
90103
const foundConfig = dynamicConfigResults.find(
91104
config => config.measurementId === measurementId
92105
);
@@ -131,7 +144,9 @@ async function gtagOnEvent(
131144
}
132145
// Checking 'send_to' fields requires having all measurement ID results back from
133146
// the dynamic config fetch.
134-
const dynamicConfigResults = await Promise.all(dynamicConfigPromisesList);
147+
const dynamicConfigResults = await promiseAllSettled(
148+
dynamicConfigPromisesList
149+
);
135150
for (const sendToId of gaSendToList) {
136151
// Any fetched dynamic measurement ID that matches this 'send_to' ID
137152
const foundConfig = dynamicConfigResults.find(

0 commit comments

Comments
 (0)