Skip to content

Commit 6be23df

Browse files
author
Athira M
committed
Integrate ABT with Firebase analytics to add experiment as UP
1 parent 5836eaf commit 6be23df

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

packages/remote-config/src/abt/experiment.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { FirebaseAnalyticsInternalName } from '@firebase/analytics-interop-types
2222
export class Experiment {
2323
constructor(
2424
private readonly storage: Storage,
25-
private readonly analytics: Provider<FirebaseAnalyticsInternalName>
25+
private readonly analyticsProvider: Provider<FirebaseAnalyticsInternalName>
2626
) {}
2727

2828
async updateActiveExperiments(
@@ -68,14 +68,18 @@ export class Experiment {
6868
}
6969
}
7070

71-
private addExperimentToAnalytics(
72-
_experimentId: string,
73-
_variantId: string
74-
): void {
75-
// TODO
71+
private async addExperimentToAnalytics(
72+
experimentId: string,
73+
variantId: string|null
74+
): Promise<void> {
75+
const analytics = await this.analyticsProvider.get();
76+
const customProperty = {
77+
[experimentId]: variantId,
78+
};
79+
analytics.setUserProperties({properties: customProperty});
7680
}
7781

78-
private removeExperimentFromAnalytics(_experimentId: string): void {
79-
// TODO
82+
private async removeExperimentFromAnalytics(experimentId: string): Promise<void> {
83+
this.addExperimentToAnalytics(experimentId, null);
8084
}
8185
}

packages/remote-config/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
111111
// config.
112112
return false;
113113
}
114-
const experiment = new Experiment(rc._storage);
114+
const experiment = new Experiment(rc._storage, rc._analyticsProvider);
115115
await Promise.all([
116116
rc._storageCache.setActiveConfig(lastSuccessfulFetchResponse.config),
117117
rc._storage.setActiveConfigEtag(lastSuccessfulFetchResponse.eTag),

packages/remote-config/src/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function registerRemoteConfig(): void {
6666
const installations = container
6767
.getProvider('installations-internal')
6868
.getImmediate();
69-
const analytics = container.getProvider('analytics-internal');
69+
const analyticsProvider = container.getProvider('analytics-internal');
7070

7171
// Normalizes optional inputs.
7272
const { projectId, apiKey, appId } = app.options;
@@ -129,7 +129,7 @@ export function registerRemoteConfig(): void {
129129
storage,
130130
logger,
131131
realtimeHandler,
132-
analytics
132+
analyticsProvider
133133
);
134134

135135
// Starts warming cache.

packages/remote-config/src/remote_config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ export class RemoteConfig implements RemoteConfigType {
9090
/**
9191
* @internal
9292
*/
93-
readonly _realtimeHandler: RealtimeHandler
93+
readonly _realtimeHandler: RealtimeHandler,
9494
/**
9595
* @internal
9696
*/
97-
readonly _analytics: Provider<FirebaseAnalyticsInternalName>
97+
readonly _analyticsProvider: Provider<FirebaseAnalyticsInternalName>
9898
) {}
9999
}

packages/remote-config/test/abt/experiment.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ import { FirebaseAnalyticsInternalName } from '@firebase/analytics-interop-types
2525

2626
describe('Experiment', () => {
2727
const storage = {} as Storage;
28-
const analytics = {} as Provider<FirebaseAnalyticsInternalName>;
29-
const experiment = new Experiment(storage, analytics);
30-
31-
describe('Experiment', () => {
32-
const storage = {} as Storage;
33-
const experiment = new Experiment(storage);
28+
const analyticsProvider = {} as Provider<FirebaseAnalyticsInternalName>;
29+
const experiment = new Experiment(storage, analyticsProvider);
3430

3531
describe('updateActiveExperiments', () => {
3632
beforeEach(() => {
3733
storage.getActiveExperiments = sinon.stub();
3834
storage.setActiveExperiments = sinon.stub();
35+
analyticsProvider.get = sinon.stub().returns(Promise.resolve({
36+
setUserProperties: sinon.stub()
37+
}));
3938
});
4039

4140
it('adds mew experiments to storage', async () => {
@@ -66,12 +65,14 @@ describe('Experiment', () => {
6665
storage.getActiveExperiments = sinon
6766
.stub()
6867
.returns(new Set(['_exp_1', '_exp_2']));
68+
const analytics = await analyticsProvider.get();
6969

7070
await experiment.updateActiveExperiments(latestExperiments);
7171

7272
expect(storage.setActiveExperiments).to.have.been.calledWith(
7373
expectedStoredExperiments
7474
);
75+
expect(analytics.setUserProperties).to.have.been.calledWith({properties: {'_exp_1': '1'}});
7576
});
7677

7778
it('removes missing experiment in fetch response from storage', async () => {
@@ -88,12 +89,14 @@ describe('Experiment', () => {
8889
storage.getActiveExperiments = sinon
8990
.stub()
9091
.returns(new Set(['_exp_1', '_exp_2']));
92+
const analytics = await analyticsProvider.get();
9193

9294
await experiment.updateActiveExperiments(latestExperiments);
9395

9496
expect(storage.setActiveExperiments).to.have.been.calledWith(
9597
expectedStoredExperiments
9698
);
99+
expect(analytics.setUserProperties).to.have.been.calledWith({properties: {'_exp_1': null}});
97100
});
98101
});
99102
});

packages/remote-config/test/remote_config.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('RemoteConfig', () => {
7373
let logger: Logger;
7474
let realtimeHandler: RealtimeHandler;
7575
let rc: RemoteConfigType;
76-
let analytics: Provider<FirebaseAnalyticsInternalName>;
76+
let analyticsProvider: Provider<FirebaseAnalyticsInternalName>;
7777

7878

7979
let getActiveConfigStub: sinon.SinonStub;
@@ -86,7 +86,7 @@ describe('RemoteConfig', () => {
8686
client = {} as RemoteConfigFetchClient;
8787
storageCache = {} as StorageCache;
8888
storage = {} as Storage;
89-
analytics = {} as Provider<FirebaseAnalyticsInternalName>;
89+
analyticsProvider = {} as Provider<FirebaseAnalyticsInternalName>;
9090
realtimeHandler = {} as RealtimeHandler;
9191
logger = new Logger('package-name');
9292
getActiveConfigStub = sinon.stub().returns(undefined);
@@ -100,7 +100,7 @@ describe('RemoteConfig', () => {
100100
storage,
101101
logger,
102102
realtimeHandler,
103-
analytics
103+
analyticsProvider
104104
);
105105
});
106106

0 commit comments

Comments
 (0)