Skip to content

Commit 943e0e5

Browse files
authored
Get installations service from the container (#2376)
* Get installations from the container * [AUTOMATED]: Prettier Code Styling * revert dev changes * convert eslint from json to js. * [AUTOMATED]: Prettier Code Styling * fix broken scripts
1 parent ae956b1 commit 943e0e5

File tree

9 files changed

+67
-29
lines changed

9 files changed

+67
-29
lines changed

packages/analytics/index.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
factory as analyticsFactory,
2525
resetGlobalVars
2626
} from './index';
27-
import { getFakeApp } from './testing/get-fake-app';
27+
import {
28+
getFakeApp,
29+
getFakeInstallations
30+
} from './testing/get-fake-firebase-services';
2831
import { FirebaseApp } from '@firebase/app-types';
2932
import { GtagCommand, EventName } from './src/constants';
3033
import { findGtagScriptOnPage } from './src/helpers';
@@ -39,21 +42,29 @@ const customDataLayerName = 'customDataLayer';
3942
describe('FirebaseAnalytics instance tests', () => {
4043
it('Throws if no analyticsId in config', () => {
4144
const app = getFakeApp();
42-
expect(() => analyticsFactory(app)).to.throw('field is empty');
45+
const installations = getFakeInstallations();
46+
expect(() => analyticsFactory(app, installations)).to.throw(
47+
'field is empty'
48+
);
4349
});
4450
it('Throws if creating an instance with already-used analytics ID', () => {
4551
const app = getFakeApp(analyticsId);
52+
const installations = getFakeInstallations();
4653
resetGlobalVars(false, { [analyticsId]: Promise.resolve() });
47-
expect(() => analyticsFactory(app)).to.throw('already exists');
54+
expect(() => analyticsFactory(app, installations)).to.throw(
55+
'already exists'
56+
);
4857
});
4958
describe('Standard app, page already has user gtag script', () => {
5059
let app: FirebaseApp = {} as FirebaseApp;
5160
before(() => {
5261
resetGlobalVars();
5362
app = getFakeApp(analyticsId);
63+
const installations = getFakeInstallations();
64+
5465
window['gtag'] = gtagStub;
5566
window['dataLayer'] = [];
56-
analyticsInstance = analyticsFactory(app);
67+
analyticsInstance = analyticsFactory(app, installations);
5768
});
5869
after(() => {
5970
delete window['gtag'];
@@ -113,13 +124,14 @@ describe('FirebaseAnalytics instance tests', () => {
113124
before(() => {
114125
resetGlobalVars();
115126
const app = getFakeApp(analyticsId);
127+
const installations = getFakeInstallations();
116128
window[customGtagName] = gtagStub;
117129
window[customDataLayerName] = [];
118130
analyticsSettings({
119131
dataLayerName: customDataLayerName,
120132
gtagName: customGtagName
121133
});
122-
analyticsInstance = analyticsFactory(app);
134+
analyticsInstance = analyticsFactory(app, installations);
123135
});
124136
after(() => {
125137
delete window[customGtagName];
@@ -162,7 +174,8 @@ describe('FirebaseAnalytics instance tests', () => {
162174
before(() => {
163175
resetGlobalVars();
164176
const app = getFakeApp(analyticsId);
165-
analyticsInstance = analyticsFactory(app);
177+
const installations = getFakeInstallations();
178+
analyticsInstance = analyticsFactory(app, installations);
166179
});
167180
after(() => {
168181
delete window['gtag'];

packages/analytics/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import firebase from '@firebase/app';
18+
import '@firebase/installations';
1819
import { FirebaseAnalytics } from '@firebase/analytics-types';
1920
import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
2021
import { _FirebaseNamespace } from '@firebase/app-types/private';
@@ -44,7 +45,11 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
4445
container => {
4546
// getImmediate for FirebaseApp will always succeed
4647
const app = container.getProvider('app').getImmediate();
47-
return factory(app);
48+
const installations = container
49+
.getProvider('installations')
50+
.getImmediate();
51+
52+
return factory(app, installations);
4853
},
4954
ComponentType.PUBLIC
5055
).setServiceProps({

packages/analytics/src/factory.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
setUserProperties,
2828
setAnalyticsCollectionEnabled
2929
} from './functions';
30-
import '@firebase/installations';
3130
import {
3231
initializeGAId,
3332
insertScriptTag,
@@ -38,6 +37,7 @@ import {
3837
import { ANALYTICS_ID_FIELD } from './constants';
3938
import { AnalyticsError, ERROR_FACTORY } from './errors';
4039
import { FirebaseApp } from '@firebase/app-types';
40+
import { FirebaseInstallations } from '@firebase/installations-types';
4141

4242
/**
4343
* Maps gaId to FID fetch promises.
@@ -102,7 +102,10 @@ export function settings(options: SettingsOptions): void {
102102
}
103103
}
104104

105-
export function factory(app: FirebaseApp): FirebaseAnalytics {
105+
export function factory(
106+
app: FirebaseApp,
107+
installations: FirebaseInstallations
108+
): FirebaseAnalytics {
106109
const analyticsId = app.options[ANALYTICS_ID_FIELD];
107110
if (!analyticsId) {
108111
throw ERROR_FACTORY.create(AnalyticsError.NO_GA_ID);
@@ -135,7 +138,11 @@ export function factory(app: FirebaseApp): FirebaseAnalytics {
135138
globalInitDone = true;
136139
}
137140
// Async but non-blocking.
138-
initializedIdPromisesMap[analyticsId] = initializeGAId(app, gtagCoreFunction);
141+
initializedIdPromisesMap[analyticsId] = initializeGAId(
142+
app,
143+
installations,
144+
gtagCoreFunction
145+
);
139146

140147
const analyticsInstance: FirebaseAnalytics = {
141148
app,

packages/analytics/src/helpers.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import {
2626
wrapOrCreateGtag,
2727
findGtagScriptOnPage
2828
} from './helpers';
29-
import { getFakeApp } from '../testing/get-fake-app';
29+
import {
30+
getFakeApp,
31+
getFakeInstallations
32+
} from '../testing/get-fake-firebase-services';
3033
import { GtagCommand } from './constants';
3134
import { Deferred } from '@firebase/util';
3235

@@ -36,8 +39,9 @@ const mockFid = 'fid-1234-zyxw';
3639
describe('FirebaseAnalytics methods', () => {
3740
it('initializeGAId gets FID from installations and calls gtag config with it', async () => {
3841
const gtagStub: SinonStub = stub();
39-
const app = getFakeApp(mockAnalyticsId, mockFid);
40-
await initializeGAId(app, gtagStub);
42+
const app = getFakeApp(mockAnalyticsId);
43+
const installations = getFakeInstallations(mockFid);
44+
await initializeGAId(app, installations, gtagStub);
4145
expect(gtagStub).to.be.calledWith(GtagCommand.CONFIG, mockAnalyticsId, {
4246
'firebase_id': mockFid,
4347
'origin': 'firebase',

packages/analytics/src/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
ORIGIN_KEY,
3131
GTAG_URL
3232
} from './constants';
33-
import '@firebase/installations';
33+
import { FirebaseInstallations } from '@firebase/installations-types';
3434

3535
/**
3636
* Initialize the analytics instance in gtag.js by calling config command with fid.
@@ -42,9 +42,10 @@ import '@firebase/installations';
4242
*/
4343
export async function initializeGAId(
4444
app: FirebaseApp,
45+
installations: FirebaseInstallations,
4546
gtagCore: Gtag
4647
): Promise<void> {
47-
const fid = await app.installations().getId();
48+
const fid = await installations.getId();
4849

4950
// This command initializes gtag.js and only needs to be called once for the entire web app,
5051
// but since it is idempotent, we can call it multiple times.

packages/analytics/testing/get-fake-app.ts renamed to packages/analytics/testing/get-fake-firebase-services.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app-types';
19-
import { stub } from 'sinon';
19+
import { FirebaseInstallations } from '@firebase/installations-types';
2020

21-
export function getFakeApp(
22-
measurementId?: string,
23-
fid: string = 'fid-1234'
24-
): FirebaseApp {
21+
export function getFakeApp(measurementId?: string): FirebaseApp {
2522
return {
2623
name: 'appName',
2724
options: {
@@ -36,8 +33,19 @@ export function getFakeApp(
3633
},
3734
automaticDataCollectionEnabled: true,
3835
delete: async () => {},
39-
installations: stub().returns({ getId: () => Promise.resolve(fid) }),
36+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37+
installations: null as any,
4038
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4139
analytics: null as any
4240
};
4341
}
42+
43+
export function getFakeInstallations(
44+
fid: string = 'fid-1234'
45+
): FirebaseInstallations {
46+
return {
47+
getId: () => Promise.resolve(fid),
48+
getToken: () => Promise.resolve('authToken'),
49+
delete: () => Promise.resolve()
50+
};
51+
}

packages/component/.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: '../../config/.eslintrc.js',
3+
parserOptions: {
4+
project: 'tsconfig.json'
5+
}
6+
};

packages/component/.eslintrc.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/component/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"dist"
1313
],
1414
"scripts": {
15-
"lint": "eslint -c .eslintrc.json '**/*.ts' --ignore-path '../../.gitignore'",
16-
"lint:fix": "eslint --fix -c .eslintrc.json '**/*.ts' --ignore-path '../../.gitignore'",
15+
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
16+
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1717
"build": "rollup -c",
1818
"dev": "rollup -c -w",
1919
"test": "run-p lint test:browser test:node",

0 commit comments

Comments
 (0)