Skip to content

Commit 8a4defb

Browse files
committed
chore: more cleanup
1 parent 0f7f090 commit 8a4defb

10 files changed

+57
-91
lines changed

Diff for: __tests__/userJourney/setUserJourneyStepsMap.spec.ts renamed to __tests__/userJourney/setStepsMap.spec.ts

-6
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,5 @@ describe('setUserJourneyStepsMap', () => {
8181
},
8282
});
8383
});
84-
it('correctly sets the finalSteps map', () => {
85-
expect(userJourneyMap.finalSteps).toMatchObject({
86-
load_fourth_screen_first_journey: ['first_journey'],
87-
load_fourth_screen_second_journey: ['second_journey'],
88-
});
89-
});
9084
});
9185
});

Diff for: src/perfume.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import { reportStorageEstimate } from './storageEstimate';
1919
import { IPerfumeOptions } from './types';
2020
import { roundByFour } from './utils';
2121
import { getVitalsScore } from './vitalsScore';
22-
import {
23-
setUserJourneyFinalStepsMap,
24-
setUserJourneyStepsMap,
25-
} from './userJourney/setUserJourneyStepsMap';
22+
import { setStepsMap } from './userJourney/setStepsMap';
2623

2724
let ntbtTimeoutID = 0;
2825

@@ -73,8 +70,7 @@ export default class Perfume {
7370
}
7471
// initializing User Journeys if present
7572
if (config.userJourneySteps) {
76-
setUserJourneyStepsMap();
77-
setUserJourneyFinalStepsMap();
73+
setStepsMap();
7874
}
7975
}
8076

Diff for: src/userJourney/markJourney.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { M, WP } from '../constants';
22

3-
import { measureJourneySteps } from './measureJourneySteps';
3+
import { measureSteps } from './measureSteps';
44

55
/**
66
* Function which creates a journey mark with a name generated
@@ -12,5 +12,5 @@ import { measureJourneySteps } from './measureJourneySteps';
1212
*/
1313
export const markJourney = (mark: string) => {
1414
WP.mark(M + mark);
15-
measureJourneySteps(mark);
15+
measureSteps(mark);
1616
};

Diff for: src/userJourney/markJourneyOnce.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { M, WP } from '../constants';
22

3-
import { measureJourneySteps } from './measureJourneySteps';
3+
import { measureSteps } from './measureSteps';
44

55
/**
66
* Function which creates a journey mark with a name generated
@@ -14,6 +14,6 @@ import { measureJourneySteps } from './measureJourneySteps';
1414
export const markJourneyOnce = (mark: string) => {
1515
if (WP.getEntriesByName(M + mark).length === 0) {
1616
WP.mark(M + mark);
17-
measureJourneySteps(mark);
17+
measureSteps(mark);
1818
}
1919
};

Diff for: src/userJourney/measureJourneyStep.ts

-46
This file was deleted.

Diff for: src/userJourney/measureStep.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { M, S, WP } from '../constants';
2+
import { reportPerf } from '../reportPerf';
3+
import { config } from '../config';
4+
5+
import { STEP_THRESHOLDS } from '../vitalsScore';
6+
import { getRating } from '../vitalsScore';
7+
8+
export const measureStep = (
9+
step: string,
10+
startMark: string,
11+
endMark: string,
12+
) => {
13+
const journeyStepMetricName = S + step;
14+
const startMarkExists = WP.getEntriesByName(M + startMark).length > 0;
15+
const endMarkExists = WP.getEntriesByName(M + endMark).length > 0;
16+
if (!endMarkExists || !config.userJourneySteps) {
17+
return;
18+
}
19+
20+
const { maxOutlierThreshold, vitalsThresholds } =
21+
STEP_THRESHOLDS[config.userJourneySteps[step].threshold];
22+
23+
if (!startMarkExists) {
24+
return;
25+
}
26+
const journeyStepMeasure = WP.measure(
27+
journeyStepMetricName,
28+
M + startMark,
29+
M + endMark,
30+
);
31+
const { duration } = journeyStepMeasure;
32+
if (duration <= maxOutlierThreshold) {
33+
const score = getRating(duration, vitalsThresholds);
34+
// Do not want to measure or log negative metrics
35+
if (duration >= 0) {
36+
reportPerf(step, duration, score, { category: 'step' }, undefined);
37+
WP.measure(`step.${step}_vitals_${score}`, {
38+
start: journeyStepMeasure.startTime + journeyStepMeasure.duration,
39+
end: journeyStepMeasure.startTime + journeyStepMeasure.duration,
40+
detail: {
41+
type: 'userJourneyStepVital',
42+
duration,
43+
},
44+
});
45+
}
46+
}
47+
};

Diff for: src/userJourney/measureJourneySteps.ts renamed to src/userJourney/measureSteps.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { config } from '../config';
22

3+
import { measureStep } from './measureStep';
34
import {
45
userJourneyMap,
56
addActiveSteps,
67
removeActiveStep,
78
} from './userJourneyMap';
89

9-
import { measureJourneyStep } from './measureJourneyStep';
10-
11-
export const measureJourneySteps = (endMark: string) => {
10+
export const measureSteps = (endMark: string) => {
1211
if (userJourneyMap.finalMarkToStepsMap[endMark]) {
1312
// this is an end mark so we delete the entry
1413
const finalSteps = userJourneyMap.finalMarkToStepsMap[endMark];
@@ -18,7 +17,7 @@ export const measureJourneySteps = (endMark: string) => {
1817
Promise.all(
1918
steps.map(async step => {
2019
// measure
21-
await measureJourneyStep(step, startMark, endMark);
20+
await measureStep(step, startMark, endMark);
2221
}),
2322
).catch(() => {
2423
// TODO @zizzamia log error

Diff for: src/userJourney/setUserJourneyStepsMap.ts renamed to src/userJourney/setStepsMap.ts

+1-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IStepConfig, IUserJourney } from '../types';
33

44
import { resetUserJourneyMap, userJourneyMap } from './userJourneyMap';
55

6-
export const setUserJourneyStepsMap = () => {
6+
export const setStepsMap = () => {
77
if (!config.userJourneySteps) {
88
return;
99
}
@@ -33,25 +33,3 @@ export const setUserJourneyStepsMap = () => {
3333
},
3434
);
3535
};
36-
37-
export const setUserJourneyFinalStepsMap = () => {
38-
if (!config.userJourneys) {
39-
return;
40-
}
41-
// reset values
42-
userJourneyMap.finalSteps = {};
43-
44-
Object.entries<IUserJourney<string> | IStepConfig<string>>(
45-
config.userJourneys,
46-
).forEach(([key, value]) => {
47-
if (key !== 'steps') {
48-
const { steps } = value as IUserJourney<string>;
49-
const finalStep = steps[steps.length - 1];
50-
if (userJourneyMap.finalSteps[finalStep]) {
51-
userJourneyMap.finalSteps[finalStep].push(key);
52-
} else {
53-
userJourneyMap.finalSteps[finalStep] = [key];
54-
}
55-
}
56-
});
57-
};

Diff for: src/userJourney/userJourneyMap.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export const userJourneyMap = {
22
finalMarkToStepsMap: {} as Record<string, Record<string, string[]>>,
33
startMarkToStepsMap: {} as Record<string, Record<string, boolean>>,
4-
finalSteps: {} as Record<string, string[]>,
54
activeSteps: {} as Record<string, boolean>,
65
};
76

@@ -35,6 +34,5 @@ export const resetUserJourneyMap = () => {
3534
// reset all values
3635
userJourneyMap.startMarkToStepsMap = {};
3736
userJourneyMap.finalMarkToStepsMap = {};
38-
userJourneyMap.finalSteps = {};
3937
resetActiveSteps();
4038
};

0 commit comments

Comments
 (0)