Skip to content

Commit 03d2b0f

Browse files
committed
Add initializeAnalytics()
1 parent ac9e469 commit 03d2b0f

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
AnalyticsCallOptions,
2525
CustomParams,
2626
EventNameString,
27-
EventParams
27+
EventParams,
28+
SettingsOptions
2829
} from '@firebase/analytics-types-exp';
2930
import { Provider } from '@firebase/component';
3031
import {
@@ -37,7 +38,8 @@ import { ANALYTICS_TYPE } from './constants';
3738
import {
3839
AnalyticsService,
3940
initializationPromisesMap,
40-
wrappedGtagFunction
41+
wrappedGtagFunction,
42+
settings
4143
} from './factory';
4244
import { logger } from './logger';
4345
import {
@@ -48,7 +50,6 @@ import {
4850
setAnalyticsCollectionEnabled as internalSetAnalyticsCollectionEnabled
4951
} from './functions';
5052

51-
export { analyticsSettings } from './factory';
5253
export { EventName } from './constants';
5354

5455
declare module '@firebase/component' {
@@ -57,15 +58,38 @@ declare module '@firebase/component' {
5758
}
5859
}
5960

61+
/**
62+
* Firebase Analytics initialization function that allows custom
63+
* settings. Can be called at most once. If no custom settings
64+
* are needed, `getAnalytics()` can be used to initialize instead.
65+
*
66+
* @public
67+
*
68+
* @param app - The FirebaseApp to use.
69+
* @param options - Custom gtag and dataLayer names.
70+
*/
71+
export function initializeAnalytics(
72+
app: FirebaseApp,
73+
options: SettingsOptions
74+
): Analytics {
75+
const analyticsProvider: Provider<'analytics-exp'> = _getProvider(
76+
app,
77+
ANALYTICS_TYPE
78+
);
79+
settings(options);
80+
const analyticsInstance = analyticsProvider.getImmediate();
81+
return analyticsInstance;
82+
}
83+
6084
/**
6185
* Returns a Firebase Analytics instance for the given app.
86+
* Initializes Firebase Analytics if not already initialized.
6287
*
6388
* @public
6489
*
6590
* @param app - The FirebaseApp to use.
6691
*/
6792
export function getAnalytics(app: FirebaseApp): Analytics {
68-
// Dependencies
6993
const analyticsProvider: Provider<'analytics-exp'> = _getProvider(
7094
app,
7195
ANALYTICS_TYPE

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function getGlobalVars(): {
144144
*
145145
* @param options - Custom gtag and dataLayer names.
146146
*/
147-
export function analyticsSettings(options: SettingsOptions): void {
147+
export function settings(options: SettingsOptions): void {
148148
if (globalInitDone) {
149149
throw ERROR_FACTORY.create(AnalyticsError.ALREADY_INITIALIZED);
150150
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { expect } from 'chai';
1919
import { SinonStub, stub, useFakeTimers } from 'sinon';
2020
import '../testing/setup';
2121
import {
22-
analyticsSettings,
2322
factory as analyticsFactory,
2423
resetGlobalVars,
2524
getGlobalVars
@@ -35,7 +34,7 @@ import { removeGtagScript } from '../testing/gtag-script-util';
3534
import { Deferred } from '@firebase/util';
3635
import { AnalyticsError } from './errors';
3736
import { logEvent } from './api';
38-
import { AnalyticsService } from './factory';
37+
import { AnalyticsService, settings } from './factory';
3938
import { _FirebaseInstallationsInternal } from '@firebase/installations-types-exp';
4039

4140
let analyticsInstance: AnalyticsService = {} as AnalyticsService;
@@ -288,7 +287,7 @@ describe('FirebaseAnalytics instance tests', () => {
288287
);
289288
window[customGtagName] = gtagStub;
290289
window[customDataLayerName] = [];
291-
analyticsSettings({
290+
settings({
292291
dataLayerName: customDataLayerName,
293292
gtagName: customGtagName
294293
});

packages-exp/analytics-exp/testing/integration-tests/integration.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717

1818
import { initializeApp, deleteApp } from '@firebase/app-exp';
1919
import '@firebase/installations-exp';
20-
import { getAnalytics, logEvent, EventName } from '../../src/index';
20+
import {
21+
getAnalytics,
22+
logEvent,
23+
EventName,
24+
initializeAnalytics
25+
} from '../../src/index';
2126
import '../setup';
2227
import { expect } from 'chai';
2328
import { stub } from 'sinon';
@@ -35,26 +40,37 @@ try {
3540

3641
const RETRY_INTERVAL = 1000;
3742

43+
async function checkForEventCalls(): Promise<number> {
44+
await new Promise(resolve => setTimeout(resolve, RETRY_INTERVAL));
45+
const resources = performance.getEntriesByType('resource');
46+
const callsWithEvent = resources.filter(
47+
resource =>
48+
resource.name.includes('google-analytics.com') &&
49+
resource.name.includes('en=login')
50+
);
51+
if (callsWithEvent.length === 0) {
52+
return checkForEventCalls();
53+
} else {
54+
return callsWithEvent.length;
55+
}
56+
}
57+
3858
describe('FirebaseAnalytics Integration Smoke Tests', () => {
3959
let app: FirebaseApp;
4060
afterEach(() => deleteApp(app));
61+
it('intializeAnalytics() sets options', async () => {
62+
app = initializeApp(config);
63+
const analyticsInstance = initializeAnalytics(app, {
64+
dataLayerName: 'testname'
65+
});
66+
expect(window['testname']).to.exist;
67+
logEvent(analyticsInstance, EventName.LOGIN, { method: 'email' });
68+
await checkForEventCalls();
69+
expect((window['testname'] as any[]).length).to.be.at.least(1);
70+
});
4171
it('logEvent() sends correct network request.', async () => {
4272
app = initializeApp(config);
4373
logEvent(getAnalytics(app), EventName.LOGIN, { method: 'email' });
44-
async function checkForEventCalls(): Promise<number> {
45-
await new Promise(resolve => setTimeout(resolve, RETRY_INTERVAL));
46-
const resources = performance.getEntriesByType('resource');
47-
const callsWithEvent = resources.filter(
48-
resource =>
49-
resource.name.includes('google-analytics.com') &&
50-
resource.name.includes('en=login')
51-
);
52-
if (callsWithEvent.length === 0) {
53-
return checkForEventCalls();
54-
} else {
55-
return callsWithEvent.length;
56-
}
57-
}
5874
const eventCallCount = await checkForEventCalls();
5975
expect(eventCallCount).to.equal(1);
6076
});

0 commit comments

Comments
 (0)