Skip to content

Commit 085ab44

Browse files
authored
feat: SDKE-852 add joint sdk select placements timestamp (#1151)
* added jointsdkselectplacements type * added jointsdkselectplacements timestamp and tests * fixed spacing
1 parent 670552c commit 085ab44

4 files changed

Lines changed: 75 additions & 4 deletions

File tree

src/mp-instance.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,8 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
14381438
mpInstance.Identity,
14391439
mpInstance._Store,
14401440
mpInstance.Logger,
1441-
roktOptions
1441+
roktOptions,
1442+
mpInstance.captureTiming
14421443
);
14431444
}
14441445

src/roktManager.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SDKIdentityApi } from "./identity.interfaces";
1414
import { SDKLoggerApi } from "./sdkRuntimeModels";
1515
import { IStore, LocalSessionAttributes } from "./store";
1616
import { UserIdentities } from "@mparticle/web-sdk";
17-
import { IdentityType } from "./types";
17+
import { IdentityType, PerformanceMarkType } from "./types";
1818

1919
// https://docs.rokt.com/developers/integration-guides/web/library/attributes
2020
export type RoktAttributeValueArray = Array<string | number | boolean>;
@@ -101,6 +101,7 @@ export default class RoktManager {
101101
private logger: SDKLoggerApi;
102102
private domain?: string;
103103
private mappedEmailShaIdentityType?: string | null;
104+
private captureTiming?: (metricsName: string) => void;
104105
/**
105106
* Initializes the RoktManager with configuration settings and user data.
106107
*
@@ -109,6 +110,7 @@ export default class RoktManager {
109110
* @param {SDKIdentityApi} identityService - The mParticle Identity instance
110111
* @param {SDKLoggerApi} logger - The mParticle Logger instance
111112
* @param {IRoktOptions} options - Options for the RoktManager
113+
* @param {Function} captureTiming - Function to capture performance timing marks
112114
*
113115
* @throws Logs error to console if placementAttributesMapping parsing fails
114116
*/
@@ -118,7 +120,8 @@ export default class RoktManager {
118120
identityService: SDKIdentityApi,
119121
store: IStore,
120122
logger?: SDKLoggerApi,
121-
options?: IRoktOptions
123+
options?: IRoktOptions,
124+
captureTiming?: (metricsName: string) => void
122125
): void {
123126
const { userAttributeFilters, settings } = roktConfig || {};
124127
const { placementAttributesMapping, hashedEmailUserIdentityType } = settings || {};
@@ -127,6 +130,7 @@ export default class RoktManager {
127130
this.identityService = identityService;
128131
this.store = store;
129132
this.logger = logger;
133+
this.captureTiming = captureTiming;
130134

131135
this.filters = {
132136
userAttributeFilters,
@@ -176,6 +180,10 @@ export default class RoktManager {
176180
* });
177181
*/
178182
public async selectPlacements(options: IRoktSelectPlacementsOptions): Promise<IRoktSelection> {
183+
if (this.captureTiming) {
184+
this.captureTiming(PerformanceMarkType.JointSdkSelectPlacements);
185+
}
186+
179187
if (!this.isReady()) {
180188
return this.deferredCall<IRoktSelection>('selectPlacements', options);
181189
}
@@ -514,4 +522,4 @@ export default class RoktManager {
514522

515523
return false; // Values are the same
516524
}
517-
}
525+
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ export const ApplicationTransitionType = {
393393

394394
export const PerformanceMarkType = {
395395
SdkStart: 'mp:sdkStart' as const,
396+
JointSdkSelectPlacements: 'mp:jointSdkSelectPlacements' as const,
396397
}
397398

398399
export default {

test/jest/roktManager.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SDKIdentityApi } from "../../src/identity.interfaces";
44
import { IMParticleWebSDKInstance } from "../../src/mp-instance";
55
import RoktManager, { IRoktKit, IRoktSelectPlacementsOptions } from "../../src/roktManager";
66
import { testMPID } from '../src/config/constants';
7+
import { PerformanceMarkType } from "../../src/types";
78

89
const resolvePromise = () => new Promise(resolve => setTimeout(resolve, 0));
910

@@ -635,6 +636,66 @@ describe('RoktManager', () => {
635636
jest.clearAllMocks();
636637
});
637638

639+
it('should capture jointSdkSelectPlacements timing when selectPlacements is called', () => {
640+
const mockCaptureTiming = jest.fn();
641+
roktManager.init(
642+
{} as IKitConfigs,
643+
{} as IMParticleUser,
644+
mockMPInstance.Identity,
645+
mockMPInstance._Store,
646+
mockMPInstance.Logger,
647+
undefined,
648+
mockCaptureTiming
649+
);
650+
651+
const kit: IRoktKit = {
652+
launcher: {
653+
selectPlacements: jest.fn(),
654+
hashAttributes: jest.fn(),
655+
use: jest.fn(),
656+
},
657+
filters: undefined,
658+
filteredUser: undefined,
659+
userAttributes: undefined,
660+
selectPlacements: jest.fn(),
661+
hashAttributes: jest.fn(),
662+
setExtensionData: jest.fn(),
663+
use: jest.fn(),
664+
};
665+
666+
roktManager.attachKit(kit);
667+
668+
const options = {
669+
attributes: {}
670+
} as IRoktSelectPlacementsOptions;
671+
672+
roktManager.selectPlacements(options);
673+
expect(mockCaptureTiming).toHaveBeenCalledWith(PerformanceMarkType.JointSdkSelectPlacements);
674+
expect(mockCaptureTiming).toHaveBeenCalledTimes(1);
675+
});
676+
677+
it('should capture jointSdkSelectPlacements timing even when kit is not ready (deferred call)', () => {
678+
const mockCaptureTiming = jest.fn();
679+
roktManager.init(
680+
{} as IKitConfigs,
681+
{} as IMParticleUser,
682+
mockMPInstance.Identity,
683+
mockMPInstance._Store,
684+
mockMPInstance.Logger,
685+
undefined,
686+
mockCaptureTiming
687+
);
688+
689+
const options = {
690+
attributes: {}
691+
} as IRoktSelectPlacementsOptions;
692+
693+
roktManager.selectPlacements(options);
694+
expect(mockCaptureTiming).toHaveBeenCalledWith(PerformanceMarkType.JointSdkSelectPlacements);
695+
expect(mockCaptureTiming).toHaveBeenCalledTimes(1);
696+
});
697+
698+
638699
it('should call kit.selectPlacements with empty attributes', () => {
639700
const kit: IRoktKit = {
640701
launcher: {

0 commit comments

Comments
 (0)