From f9dc9474a7b571137c5d9aa6216b8aa82ad5b02f Mon Sep 17 00:00:00 2001 From: Emiliano Sanchez Date: Wed, 11 Dec 2024 14:02:25 -0300 Subject: [PATCH] Remove 'track' property from ImpressionDTO, to not modify the ImpressionListener types --- src/sdkClient/client.ts | 19 +++++++++---------- .../__tests__/impressionsTracker.spec.ts | 16 ++++++++-------- src/trackers/impressionsTracker.ts | 3 ++- src/trackers/types.ts | 2 +- types/splitio.d.ts | 1 - 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/sdkClient/client.ts b/src/sdkClient/client.ts index 0bd00f97..5216d2d2 100644 --- a/src/sdkClient/client.ts +++ b/src/sdkClient/client.ts @@ -34,11 +34,11 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const stopTelemetryTracker = telemetryTracker.trackEval(withConfig ? TREATMENT_WITH_CONFIG : TREATMENT); const wrapUp = (evaluationResult: IEvaluationResult) => { - const queue: SplitIO.ImpressionDTO[] = []; + const queue: [impression: SplitIO.ImpressionDTO, track?: boolean][] = []; const treatment = processEvaluation(evaluationResult, featureFlagName, key, attributes, withConfig, methodName, queue); impressionsTracker.track(queue, attributes); - stopTelemetryTracker(queue[0] && queue[0].label); + stopTelemetryTracker(queue[0] && queue[0][0].label); return treatment; }; @@ -59,14 +59,14 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const stopTelemetryTracker = telemetryTracker.trackEval(withConfig ? TREATMENTS_WITH_CONFIG : TREATMENTS); const wrapUp = (evaluationResults: Record) => { - const queue: SplitIO.ImpressionDTO[] = []; + const queue: [impression: SplitIO.ImpressionDTO, track?: boolean][] = []; const treatments: Record = {}; Object.keys(evaluationResults).forEach(featureFlagName => { treatments[featureFlagName] = processEvaluation(evaluationResults[featureFlagName], featureFlagName, key, attributes, withConfig, methodName, queue); }); impressionsTracker.track(queue, attributes); - stopTelemetryTracker(queue[0] && queue[0].label); + stopTelemetryTracker(queue[0] && queue[0][0].label); return treatments; }; @@ -87,7 +87,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl const stopTelemetryTracker = telemetryTracker.trackEval(method); const wrapUp = (evaluationResults: Record) => { - const queue: SplitIO.ImpressionDTO[] = []; + const queue: [impression: SplitIO.ImpressionDTO, track?: boolean][] = []; const treatments: Record = {}; const evaluations = evaluationResults; Object.keys(evaluations).forEach(featureFlagName => { @@ -95,7 +95,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl }); impressionsTracker.track(queue, attributes); - stopTelemetryTracker(queue[0] && queue[0].label); + stopTelemetryTracker(queue[0] && queue[0][0].label); return treatments; }; @@ -128,7 +128,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl attributes: SplitIO.Attributes | undefined, withConfig: boolean, invokingMethodName: string, - queue: SplitIO.ImpressionDTO[] + queue: [impression: SplitIO.ImpressionDTO, track?: boolean][] ): SplitIO.Treatment | SplitIO.TreatmentWithConfig { const matchingKey = getMatching(key); const bucketingKey = getBucketing(key); @@ -138,7 +138,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl if (validateSplitExistence(log, readinessManager, featureFlagName, label, invokingMethodName)) { log.info(IMPRESSION_QUEUEING); - queue.push({ + queue.push([{ feature: featureFlagName, keyName: matchingKey, treatment, @@ -146,8 +146,7 @@ export function clientFactory(params: ISdkFactoryContext): SplitIO.IClient | Spl bucketingKey, label, changeNumber: changeNumber as number, - track - }); + }, track]); } if (withConfig) { diff --git a/src/trackers/__tests__/impressionsTracker.spec.ts b/src/trackers/__tests__/impressionsTracker.spec.ts index 08ec9f71..8a0cfd51 100644 --- a/src/trackers/__tests__/impressionsTracker.spec.ts +++ b/src/trackers/__tests__/impressionsTracker.spec.ts @@ -70,7 +70,7 @@ describe('Impressions Tracker', () => { expect(fakeImpressionsCache.track).not.toBeCalled(); // cache method should not be called by just creating a tracker - tracker.track([imp1, imp2, imp3]); + tracker.track([[imp1], [imp2], [imp3]]); expect(fakeImpressionsCache.track.mock.calls[0][0]).toEqual([imp1, imp2, imp3]); // Should call the storage track method once we invoke .track() method, passing queued params in a sequence. }); @@ -93,7 +93,7 @@ describe('Impressions Tracker', () => { expect(fakeIntegrationsManager.handleImpression).not.toBeCalled(); // The integrations manager handleImpression method should not be invoked if we haven't tracked impressions. // We signal that we actually want to track the queued impressions. - tracker.track([fakeImpression, fakeImpression2], fakeAttributes); + tracker.track([[fakeImpression], [fakeImpression2]], fakeAttributes); expect(fakeImpressionsCache.track.mock.calls[0][0]).toEqual([fakeImpression, fakeImpression2]); // Even with a listener, impression should be sent to the cache expect(fakeListener.logImpression).not.toBeCalled(); // The listener should not be executed synchronously. @@ -157,7 +157,7 @@ describe('Impressions Tracker', () => { expect(fakeImpressionsCache.track).not.toBeCalled(); // storage method should not be called until impressions are tracked. trackers.forEach(tracker => { - tracker.track([impression, impression2, impression3]); + tracker.track([[impression], [impression2], [impression3]]); const lastArgs = fakeImpressionsCache.track.mock.calls[fakeImpressionsCache.track.mock.calls.length - 1]; @@ -182,7 +182,7 @@ describe('Impressions Tracker', () => { expect(fakeImpressionsCache.track).not.toBeCalled(); // cache method should not be called by just creating a tracker - tracker.track([impression, impression2, impression3]); + tracker.track([[impression], [impression2], [impression3]]); const lastArgs = fakeImpressionsCache.track.mock.calls[fakeImpressionsCache.track.mock.calls.length - 1]; @@ -203,19 +203,19 @@ describe('Impressions Tracker', () => { const tracker = impressionsTrackerFactory(settings, fakeImpressionsCache, strategy, fakeWhenInit); - tracker.track([impression]); + tracker.track([[impression]]); expect(fakeImpressionsCache.track).toBeCalledTimes(1); // impression should be tracked if userConsent is undefined settings.userConsent = 'UNKNOWN'; - tracker.track([impression]); + tracker.track([[impression]]); expect(fakeImpressionsCache.track).toBeCalledTimes(2); // impression should be tracked if userConsent is unknown settings.userConsent = 'GRANTED'; - tracker.track([impression]); + tracker.track([[impression]]); expect(fakeImpressionsCache.track).toBeCalledTimes(3); // impression should be tracked if userConsent is granted settings.userConsent = 'DECLINED'; - tracker.track([impression]); + tracker.track([[impression]]); expect(fakeImpressionsCache.track).toBeCalledTimes(3); // impression should not be tracked if userConsent is declined }); diff --git a/src/trackers/impressionsTracker.ts b/src/trackers/impressionsTracker.ts index 485d0694..54a5ed0b 100644 --- a/src/trackers/impressionsTracker.ts +++ b/src/trackers/impressionsTracker.ts @@ -28,9 +28,10 @@ export function impressionsTrackerFactory( const { log, impressionListener, runtime: { ip, hostname }, version } = settings; return { - track(impressions: SplitIO.ImpressionDTO[], attributes?: SplitIO.Attributes) { + track(imps: [impression: SplitIO.ImpressionDTO, track?: boolean][], attributes?: SplitIO.Attributes) { if (settings.userConsent === CONSENT_DECLINED) return; + const impressions = imps.map((item) => item[0]); const impressionsCount = impressions.length; const { impressionsToStore, impressionsToListener, deduped } = strategy.process(impressions); diff --git a/src/trackers/types.ts b/src/trackers/types.ts index db6d5bcb..71c5534a 100644 --- a/src/trackers/types.ts +++ b/src/trackers/types.ts @@ -18,7 +18,7 @@ export interface IImpressionsHandler { } export interface IImpressionsTracker { - track(impressions: SplitIO.ImpressionDTO[], attributes?: SplitIO.Attributes): void + track(impressions: [impression: SplitIO.ImpressionDTO, track?: boolean][], attributes?: SplitIO.Attributes): void } /** Telemetry tracker */ diff --git a/types/splitio.d.ts b/types/splitio.d.ts index d520e64c..f2faa2df 100644 --- a/types/splitio.d.ts +++ b/types/splitio.d.ts @@ -811,7 +811,6 @@ declare namespace SplitIO { label: string; changeNumber: number; pt?: number; - track?: boolean; } /** * Object with information about an impression. It contains the generated impression DTO as well as