Skip to content

Commit 1f2e4fc

Browse files
committed
refactor: move counting impression to promise all
1 parent 0b6fc77 commit 1f2e4fc

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/Contentpass.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ describe('Contentpass', () => {
359359

360360
// after 6 retries the state should change to error
361361
await jest.advanceTimersByTimeAsync(120001);
362-
expect(reportErrorSpy).toHaveBeenCalledTimes(7);
362+
expect(reportErrorSpy).toHaveBeenCalledTimes(1);
363363
expect(reportErrorSpy).toHaveBeenCalledWith(refreshError, {
364364
msg: 'Failed to refresh token after 6 retries',
365365
});

src/Contentpass.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export default class Contentpass implements ContentpassInterface {
3838
private authStateStorage: OidcAuthStateStorage;
3939
private readonly config: ContentpassConfig;
4040
private readonly samplingRate: number;
41-
private readonly instanceId: string;
4241

4342
private contentpassState: ContentpassState = {
4443
state: ContentpassStateType.INITIALISING,
@@ -61,7 +60,6 @@ export default class Contentpass implements ContentpassInterface {
6160
throw new Error('Sampling rate must be between 0 and 1');
6261
}
6362
this.samplingRate = config.samplingRate || DEFAULT_SAMPLING_RATE;
64-
this.instanceId = uuid.v4();
6563
this.authStateStorage = new OidcAuthStateStorage(config.propertyId);
6664
this.config = config;
6765
setSentryExtraAttribute('propertyId', config.propertyId);
@@ -134,48 +132,52 @@ export default class Contentpass implements ContentpassInterface {
134132
};
135133

136134
public countImpression = async () => {
137-
if (this.hasValidSubscriptionAndAccessToken()) {
138-
try {
139-
await this.countPaidImpression();
140-
} catch (err: any) {
141-
reportError(err, { msg: 'Failed to count paid impression' });
142-
}
143-
}
135+
await Promise.all([
136+
this.countPaidImpressionWhenUserHasValidSub(),
137+
this.countSampledImpression(),
138+
]);
139+
};
144140

145-
try {
146-
await this.countSampledImpression();
147-
} catch (err: any) {
148-
reportError(err, { msg: 'Failed to count sampled impression' });
141+
private countPaidImpressionWhenUserHasValidSub = async () => {
142+
if (!this.hasValidSubscriptionAndAccessToken()) {
143+
return;
149144
}
150-
};
151145

152-
private countPaidImpression = async () => {
153146
logger.info('Counting paid impression');
154147
const impressionId = uuid.v4();
155148

156-
await sendPageViewEvent(this.config.apiUrl, {
157-
propertyId: this.config.propertyId,
158-
impressionId,
159-
accessToken: this.oidcAuthState!.accessToken,
160-
});
149+
try {
150+
await sendPageViewEvent(this.config.apiUrl, {
151+
propertyId: this.config.propertyId,
152+
impressionId,
153+
accessToken: this.oidcAuthState!.accessToken,
154+
});
155+
} catch (err: any) {
156+
reportError(err, { msg: 'Failed to count paid impression' });
157+
}
161158
};
162159

163160
private countSampledImpression = async () => {
164161
const generatedSample = Math.random();
165162
const publicId = this.config.propertyId.slice(0, 8);
163+
const instanceId = uuid.v4();
166164

167165
if (generatedSample >= this.samplingRate) {
168166
return;
169167
}
170168

171169
logger.info('Counting sampled impression');
172-
await sendStats(this.config.apiUrl, {
173-
ea: 'load',
174-
ec: 'tcf-sampled',
175-
cpabid: this.instanceId,
176-
cppid: publicId,
177-
cpsr: this.samplingRate,
178-
});
170+
try {
171+
await sendStats(this.config.apiUrl, {
172+
ea: 'load',
173+
ec: 'tcf-sampled',
174+
cpabid: instanceId,
175+
cppid: publicId,
176+
cpsr: this.samplingRate,
177+
});
178+
} catch (err: any) {
179+
reportError(err, { msg: 'Failed to count sampled impression' });
180+
}
179181
};
180182

181183
private initialiseAuthState = async () => {

0 commit comments

Comments
 (0)