Skip to content

Get installations service from the container #2376

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

Merged
merged 6 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/analytics/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
factory as analyticsFactory,
resetGlobalVars
} from './index';
import { getFakeApp } from './testing/get-fake-app';
import {
getFakeApp,
getFakeInstallations
} from './testing/get-fake-firebase-services';
import { FirebaseApp } from '@firebase/app-types';
import { GtagCommand, EventName } from './src/constants';
import { findGtagScriptOnPage } from './src/helpers';
Expand All @@ -39,21 +42,29 @@ const customDataLayerName = 'customDataLayer';
describe('FirebaseAnalytics instance tests', () => {
it('Throws if no analyticsId in config', () => {
const app = getFakeApp();
expect(() => analyticsFactory(app)).to.throw('field is empty');
const installations = getFakeInstallations();
expect(() => analyticsFactory(app, installations)).to.throw(
'field is empty'
);
});
it('Throws if creating an instance with already-used analytics ID', () => {
const app = getFakeApp(analyticsId);
const installations = getFakeInstallations();
resetGlobalVars(false, { [analyticsId]: Promise.resolve() });
expect(() => analyticsFactory(app)).to.throw('already exists');
expect(() => analyticsFactory(app, installations)).to.throw(
'already exists'
);
});
describe('Standard app, page already has user gtag script', () => {
let app: FirebaseApp = {} as FirebaseApp;
before(() => {
resetGlobalVars();
app = getFakeApp(analyticsId);
const installations = getFakeInstallations();

window['gtag'] = gtagStub;
window['dataLayer'] = [];
analyticsInstance = analyticsFactory(app);
analyticsInstance = analyticsFactory(app, installations);
});
after(() => {
delete window['gtag'];
Expand Down Expand Up @@ -113,13 +124,14 @@ describe('FirebaseAnalytics instance tests', () => {
before(() => {
resetGlobalVars();
const app = getFakeApp(analyticsId);
const installations = getFakeInstallations();
window[customGtagName] = gtagStub;
window[customDataLayerName] = [];
analyticsSettings({
dataLayerName: customDataLayerName,
gtagName: customGtagName
});
analyticsInstance = analyticsFactory(app);
analyticsInstance = analyticsFactory(app, installations);
});
after(() => {
delete window[customGtagName];
Expand Down Expand Up @@ -162,7 +174,8 @@ describe('FirebaseAnalytics instance tests', () => {
before(() => {
resetGlobalVars();
const app = getFakeApp(analyticsId);
analyticsInstance = analyticsFactory(app);
const installations = getFakeInstallations();
analyticsInstance = analyticsFactory(app, installations);
});
after(() => {
delete window['gtag'];
Expand Down
7 changes: 6 additions & 1 deletion packages/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/
import firebase from '@firebase/app';
import '@firebase/installations';
import { FirebaseAnalytics } from '@firebase/analytics-types';
import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
import { _FirebaseNamespace } from '@firebase/app-types/private';
Expand Down Expand Up @@ -44,7 +45,11 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
container => {
// getImmediate for FirebaseApp will always succeed
const app = container.getProvider('app').getImmediate();
return factory(app);
const installations = container
.getProvider('installations')
.getImmediate();

return factory(app, installations);
},
ComponentType.PUBLIC
).setServiceProps({
Expand Down
13 changes: 10 additions & 3 deletions packages/analytics/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
setUserProperties,
setAnalyticsCollectionEnabled
} from './functions';
import '@firebase/installations';
import {
initializeGAId,
insertScriptTag,
Expand All @@ -38,6 +37,7 @@ import {
import { ANALYTICS_ID_FIELD } from './constants';
import { AnalyticsError, ERROR_FACTORY } from './errors';
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseInstallations } from '@firebase/installations-types';

/**
* Maps gaId to FID fetch promises.
Expand Down Expand Up @@ -102,7 +102,10 @@ export function settings(options: SettingsOptions): void {
}
}

export function factory(app: FirebaseApp): FirebaseAnalytics {
export function factory(
app: FirebaseApp,
installations: FirebaseInstallations
): FirebaseAnalytics {
const analyticsId = app.options[ANALYTICS_ID_FIELD];
if (!analyticsId) {
throw ERROR_FACTORY.create(AnalyticsError.NO_GA_ID);
Expand Down Expand Up @@ -135,7 +138,11 @@ export function factory(app: FirebaseApp): FirebaseAnalytics {
globalInitDone = true;
}
// Async but non-blocking.
initializedIdPromisesMap[analyticsId] = initializeGAId(app, gtagCoreFunction);
initializedIdPromisesMap[analyticsId] = initializeGAId(
app,
installations,
gtagCoreFunction
);

const analyticsInstance: FirebaseAnalytics = {
app,
Expand Down
10 changes: 7 additions & 3 deletions packages/analytics/src/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
wrapOrCreateGtag,
findGtagScriptOnPage
} from './helpers';
import { getFakeApp } from '../testing/get-fake-app';
import {
getFakeApp,
getFakeInstallations
} from '../testing/get-fake-firebase-services';
import { GtagCommand } from './constants';
import { Deferred } from '@firebase/util';

Expand All @@ -36,8 +39,9 @@ const mockFid = 'fid-1234-zyxw';
describe('FirebaseAnalytics methods', () => {
it('initializeGAId gets FID from installations and calls gtag config with it', async () => {
const gtagStub: SinonStub = stub();
const app = getFakeApp(mockAnalyticsId, mockFid);
await initializeGAId(app, gtagStub);
const app = getFakeApp(mockAnalyticsId);
const installations = getFakeInstallations(mockFid);
await initializeGAId(app, installations, gtagStub);
expect(gtagStub).to.be.calledWith(GtagCommand.CONFIG, mockAnalyticsId, {
'firebase_id': mockFid,
'origin': 'firebase',
Expand Down
5 changes: 3 additions & 2 deletions packages/analytics/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
ORIGIN_KEY,
GTAG_URL
} from './constants';
import '@firebase/installations';
import { FirebaseInstallations } from '@firebase/installations-types';

/**
* Initialize the analytics instance in gtag.js by calling config command with fid.
Expand All @@ -42,9 +42,10 @@ import '@firebase/installations';
*/
export async function initializeGAId(
app: FirebaseApp,
installations: FirebaseInstallations,
gtagCore: Gtag
): Promise<void> {
const fid = await app.installations().getId();
const fid = await installations.getId();

// This command initializes gtag.js and only needs to be called once for the entire web app,
// but since it is idempotent, we can call it multiple times.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
*/

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

export function getFakeApp(
measurementId?: string,
fid: string = 'fid-1234'
): FirebaseApp {
export function getFakeApp(measurementId?: string): FirebaseApp {
return {
name: 'appName',
options: {
Expand All @@ -36,8 +33,19 @@ export function getFakeApp(
},
automaticDataCollectionEnabled: true,
delete: async () => {},
installations: stub().returns({ getId: () => Promise.resolve(fid) }),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
installations: null as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
analytics: null as any
};
}

export function getFakeInstallations(
fid: string = 'fid-1234'
): FirebaseInstallations {
return {
getId: () => Promise.resolve(fid),
getToken: () => Promise.resolve('authToken'),
delete: () => Promise.resolve()
};
}