Skip to content

Commit 3dbbffe

Browse files
gtamanahaGuillermo Tamanaha
andauthored
feat: Add performance timing for SDK start measurement (#1101)
* Add EventTimingService to capture event timestamps * Update jest configuration to include fetch-mock in transformIgnorePatterns and clean up test setup * refactor: Simplify EventTimingService by removing unnecessary self reference and updating test cases with consistent API keys * test: Update mParticle instance manager test to set requestConfig to true * build: Update jest test command to include build step before execution * fix(tests): Update mParticle instance manager tests to use globalThis for compatibility * refactor: Update EventTimingService to use EventTimingName type and improve consistency in mParticle instance manager * fix(tests): Replace global.fetch with globalThis.fetch in mParticle instance manager tests for improved compatibility * fix(tests): Update mParticle instance manager tests to use globalThis.fetch for better compatibility across environments * refactor: Remove EventTimingService and related tests; update jest configuration and introduce performance metrics tracking * refactor: Remove isSelfHosted method from IMParticleInstanceManager interface for cleaner API * Minor * feat: Add getLauncherInstanceGuid method to IMParticleWebSDKInstance and IMParticleInstanceManager for unique identifier generation * feat: Update mParticle instance manager to set Rokt launcher instance GUID and capture performance timings * chore: Remove unnecessary blank line in mParticle instance manager for cleaner code * chore: Remove unused variable in mParticle instance manager for improved code clarity * feat: Introduce PerformanceMarkType for capturing performance metrics and update related methods in mParticle instance manager * feat: Update mParticle instance manager to use Constants for Rokt launcher instance GUID and enhance related tests for improved functionality * fix(tests): Update mParticle instance manager tests to reference globalThis instead of window for improved compatibility * feat: Add captureTiming method to MParticleWebSDK interface and update tests to include PerformanceMarkType for performance metrics tracking * chore: Remove unused PerformanceMarkType import from mParticle instance manager for improved code clarity * chore: Remove all references to PerformanceMarkType across the codebase for improved clarity and maintainability * refactor: Use Constants for accessing Rokt launcher instance GUID in setLauncherInstanceGuid method for improved code clarity --------- Co-authored-by: Guillermo Tamanaha <[email protected]>
1 parent ca4a655 commit 3dbbffe

File tree

10 files changed

+86
-3
lines changed

10 files changed

+86
-3
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'jsdom',
44
// The built mParticle.js file needs to exist for integration tests
5-
setupFiles: ['./dist/mparticle.js'],
5+
setupFiles: ['./test/jest/setup.ts', './dist/mparticle.js'],
66
setupFilesAfterEnv: ['jest-expect-message'],
77
transform: {
88
'^.+\\.(js)$': 'ts-jest',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"test:karma:rollup:cjs": "cross-env BUNDLER=rollup karma start test/integrations/cjs/karma.rollup.config.js",
6262
"test:karma:webpack:module": "cross-env BUNDLER=webpack karma start test/integrations/module/karma.webpack.config.js",
6363
"test:karma:rollup:module": "cross-env BUNDLER=rollup karma start test/integrations/module/karma.rollup.config.js",
64-
"test:jest": "jest --config jest.config.js",
64+
"test:jest": "npm run build && jest --config jest.config.js",
6565
"test:jest:watch": "jest --config jest.config.js --watch",
6666
"watch": "cross-env ENVIRONMENT=dev BUILD=iife rollup --config rollup.config.js -w",
6767
"watch:all": "cross-env ENVIRONMENT=prod BUILDALL=true rollup --config rollup.config.js -w",

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ const Constants = {
214214
None: 'none',
215215
RoktOnly: 'roktonly',
216216
},
217+
Rokt: {
218+
LauncherInstanceGuidKey: '__rokt_li_guid__',
219+
},
217220
} as const;
218221

219222
export default Constants;

src/mp-instance.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export interface IMParticleWebSDKInstance extends MParticleWebSDK {
8989
_instanceName: string;
9090
_preInit: IPreInit;
9191
_timeOnSiteTimer: ForegroundTimer;
92+
setLauncherInstanceGuid: () => void;
93+
captureTiming(metricName: string);
9294
}
9395

9496
const { Messages, HTTPCodes, FeatureFlags, CaptureIntegrationSpecificIdsV2Modes } = Constants;
@@ -1341,6 +1343,20 @@ export default function mParticleInstance(this: IMParticleWebSDKInstance, instan
13411343
};
13421344
}
13431345
};
1346+
1347+
const launcherInstanceGuidKey = Constants.Rokt.LauncherInstanceGuidKey;
1348+
this.setLauncherInstanceGuid = function() {
1349+
if (!window[launcherInstanceGuidKey]
1350+
|| typeof window[launcherInstanceGuidKey] !== 'string') {
1351+
window[launcherInstanceGuidKey] = self._Helpers.generateUniqueId();
1352+
}
1353+
};
1354+
1355+
this.captureTiming = function(metricsName) {
1356+
if (typeof window !== 'undefined' && window.performance?.mark) {
1357+
window.performance.mark(metricsName);
1358+
}
1359+
}
13441360
}
13451361

13461362
// Some (server) config settings need to be returned before they are set on SDKConfig in a self hosted environment

src/mparticle-instance-manager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Polyfill from './polyfill';
2-
import { CommerceEventType, EventType, IdentityType, ProductActionType, PromotionActionType } from './types';
2+
import { CommerceEventType, EventType, IdentityType, PerformanceMarkType, ProductActionType, PromotionActionType } from './types';
33
import Constants from './constants';
44
import mParticleInstance, { IMParticleWebSDKInstance } from './mp-instance.js';
55
import _BatchValidator from './mockBatchCreator';
@@ -77,9 +77,15 @@ function mParticleInstanceManager(this: IMParticleInstanceManager) {
7777
self._instances[instanceName] = client;
7878
}
7979

80+
client.captureTiming(PerformanceMarkType.SdkStart);
81+
client.setLauncherInstanceGuid();
8082
client.init(apiKey, config, instanceName);
8183
};
8284

85+
this.captureTiming = function(metricsName) {
86+
self.getInstance().captureTiming(metricsName);
87+
}
88+
8389
this.getInstance = function getInstance(instanceName) {
8490
let client: IMParticleWebSDKInstance;
8591
if (!instanceName) {

src/sdkRuntimeModels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export interface MParticleWebSDK {
228228
generateHash(value: string): string;
229229
setIntegrationAttribute(integrationModuleId: number, attrs: IntegrationAttribute): void;
230230
getIntegrationAttributes(integrationModuleId: number): IntegrationAttribute;
231+
captureTiming(metricName: string): void;
231232
}
232233

233234
// https://go.mparticle.com/work/SQDSDKS-4805

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ export const ApplicationTransitionType = {
391391
AppInit: 1 as const,
392392
};
393393

394+
export const PerformanceMarkType = {
395+
SdkStart: 'mp:sdkStart' as const,
396+
}
397+
394398
export default {
395399
MessageType,
396400
EventType,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Constants from '../../src/constants';
2+
import { PerformanceMarkType } from '../../src/types';
3+
import { IMParticleInstanceManager } from '../../src/sdkRuntimeModels';
4+
5+
const mockPerformanceMark = (globalThis as any).__mockPerformanceMark;
6+
const mParticle = (globalThis as any).mParticle as IMParticleInstanceManager;
7+
8+
describe('mParticle instance manager', () => {
9+
beforeEach(() => {
10+
mockPerformanceMark.mockClear();
11+
delete (globalThis as any)[Constants.Rokt.LauncherInstanceGuidKey];
12+
});
13+
14+
it('sets sdkStart event timing', async () => {
15+
mParticle.init("testApiKey", {});
16+
expect(mockPerformanceMark).toHaveBeenCalledWith(PerformanceMarkType.SdkStart);
17+
});
18+
19+
it('does not capture timing if window.performance.mark is not available', () => {
20+
(globalThis as any).performance.mark = undefined;
21+
mParticle.init("testApiKey", {});
22+
expect(mockPerformanceMark).not.toHaveBeenCalled();
23+
});
24+
25+
it('sets rokt launcher instance guid', async () => {
26+
mParticle.init("testApiKey", {});
27+
expect(globalThis).toHaveProperty(Constants.Rokt.LauncherInstanceGuidKey);
28+
expect(typeof globalThis[Constants.Rokt.LauncherInstanceGuidKey]).toBe('string');
29+
});
30+
31+
it('does not set rokt launcher instance guid if it already exists', () => {
32+
globalThis[Constants.Rokt.LauncherInstanceGuidKey] = 'testGuid';
33+
mParticle.init("testApiKey", {});
34+
expect(globalThis).toHaveProperty(Constants.Rokt.LauncherInstanceGuidKey);
35+
expect(typeof globalThis[Constants.Rokt.LauncherInstanceGuidKey]).toBe('string');
36+
expect(globalThis[Constants.Rokt.LauncherInstanceGuidKey]).toBe('testGuid');
37+
});
38+
});

test/jest/setup.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest setup file to mock performance API before mParticle loads
2+
const mockPerformanceMark = jest.fn();
3+
4+
// Extend jsdom's performance object with mark function
5+
if (globalThis.performance) {
6+
globalThis.performance.mark = mockPerformanceMark;
7+
} else {
8+
globalThis.performance = {
9+
mark: mockPerformanceMark,
10+
} as any;
11+
}
12+
13+
// Make the mock available globally for tests
14+
globalThis.__mockPerformanceMark = mockPerformanceMark;

test/src/tests-mparticle-instance-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ describe('mParticle instance manager', () => {
215215
'getEnvironment',
216216
'upload',
217217
'Rokt',
218+
'captureTiming',
218219
]);
219220
});
220221

0 commit comments

Comments
 (0)