Skip to content

Commit c483f11

Browse files
committed
refactor: move counting impression to promise all
1 parent ef6abf5 commit c483f11

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
@@ -361,7 +361,7 @@ describe('Contentpass', () => {
361361

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

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);
@@ -135,48 +133,52 @@ export default class Contentpass implements ContentpassInterface {
135133
};
136134

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

146-
try {
147-
await this.countSampledImpression();
148-
} catch (err: any) {
149-
reportError(err, { msg: 'Failed to count sampled impression' });
142+
private countPaidImpressionWhenUserHasValidSub = async () => {
143+
if (!this.hasValidSubscriptionAndAccessToken()) {
144+
return;
150145
}
151-
};
152146

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

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

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

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

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

182184
private initialiseAuthState = async () => {

0 commit comments

Comments
 (0)