Skip to content

Commit 5ac62c2

Browse files
committed
CLOUDP-349749: useTrackInSample
1 parent 3bbfc67 commit 5ac62c2

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

packages/compass-telemetry/src/atlas-skills-banner.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ExperimentTestGroup,
1212
ExperimentTestName,
1313
useAssignment,
14-
useFireExperimentViewed,
14+
useTrackInSample,
1515
} from './provider';
1616

1717
const skillsCTAContent = css({
@@ -62,10 +62,8 @@ export const useAtlasSkillsBanner = (context: SkillsBannerContextEnum) => {
6262
ExperimentTestGroup.atlasSkillsVariant;
6363

6464
// Track experiment viewed when user is in experiment and banner would be shown
65-
useFireExperimentViewed({
66-
testName: ExperimentTestName.atlasSkills,
67-
shouldFire: !!atlasSkillsAssignment,
68-
additionalProperties: { screen: context },
65+
useTrackInSample(ExperimentTestName.atlasSkills, !!atlasSkillsAssignment, {
66+
screen: context,
6967
});
7068

7169
return {

packages/compass-telemetry/src/experimentation-provider.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ type GetAssignmentFn = (
2020
options?: types.GetAssignmentOptions<types.TypeData>
2121
) => Promise<types.SDKAssignment<ExperimentTestName, string> | null>;
2222

23+
type UseTrackInSampleHook = (
24+
experimentName: ExperimentTestName,
25+
shouldFireEvent?: boolean,
26+
customProperties?: types.TypeData['experimentViewedProps'],
27+
team?: types.TypeData['loggerTeam']
28+
) => typesReact.BasicHookResponse;
29+
2330
interface CompassExperimentationProviderContextValue {
2431
useAssignment: UseAssignmentHook;
2532
assignExperiment: AssignExperimentFn;
2633
getAssignment: GetAssignmentFn;
34+
useTrackInSample: UseTrackInSampleHook;
2735
}
2836

2937
const initialContext: CompassExperimentationProviderContextValue = {
@@ -43,6 +51,15 @@ const initialContext: CompassExperimentationProviderContextValue = {
4351
getAssignment() {
4452
return Promise.resolve(null);
4553
},
54+
useTrackInSample() {
55+
return {
56+
asyncStatus: null,
57+
error: null,
58+
isLoading: false,
59+
isError: false,
60+
isSuccess: true,
61+
};
62+
},
4663
};
4764

4865
export const ExperimentationContext =
@@ -54,17 +71,26 @@ export const CompassExperimentationProvider: React.FC<{
5471
useAssignment: UseAssignmentHook;
5572
assignExperiment: AssignExperimentFn;
5673
getAssignment: GetAssignmentFn;
57-
}> = ({ children, useAssignment, assignExperiment, getAssignment }) => {
74+
useTrackInSample: UseTrackInSampleHook;
75+
}> = ({
76+
children,
77+
useAssignment,
78+
assignExperiment,
79+
getAssignment,
80+
useTrackInSample,
81+
}) => {
5882
// Use useRef to keep the functions up-to-date; Use mutation pattern to maintain the
5983
// same object reference to prevent unnecessary re-renders of consuming components
6084
const { current: contextValue } = useRef({
6185
useAssignment,
6286
assignExperiment,
6387
getAssignment,
88+
useTrackInSample,
6489
});
6590
contextValue.useAssignment = useAssignment;
6691
contextValue.assignExperiment = assignExperiment;
6792
contextValue.getAssignment = getAssignment;
93+
contextValue.useTrackInSample = useTrackInSample;
6894

6995
return (
7096
<ExperimentationContext.Provider value={contextValue}>
@@ -77,3 +103,8 @@ export const CompassExperimentationProvider: React.FC<{
77103
export const useAssignment = (...args: Parameters<UseAssignmentHook>) => {
78104
return useContext(ExperimentationContext).useAssignment(...args);
79105
};
106+
107+
// Hook for components to access experiment assignment
108+
export const useTrackInSample = (...args: Parameters<UseTrackInSampleHook>) => {
109+
return useContext(ExperimentationContext).useTrackInSample(...args);
110+
};

packages/compass-telemetry/src/provider.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,19 @@ export function useTrackOnChange(
132132
*
133133
* @param testName - The name of the experiment to track.
134134
* @param shouldFire - A boolean indicating whether to fire the event. Defaults to true.
135-
* @param additionalProperties - Optional additional properties to include in the track call.
136135
*
137136
* @example
138137
* useFireExperimentViewed({
139138
* testName: ExperimentTestName.earlyJourneyIndexesGuidance,
140139
* shouldFire: enableInIndexesGuidanceExp ,
141140
* });
142-
*
143-
* @example
144-
* useFireExperimentViewed({
145-
* testName: ExperimentTestName.atlasSkills,
146-
* shouldFire: showBanner,
147-
* additionalProperties: { screen: 'aggregations' },
148-
* });
149141
*/
150142
export const useFireExperimentViewed = ({
151143
testName,
152144
shouldFire = true,
153-
additionalProperties,
154145
}: {
155146
testName: ExperimentTestName;
156147
shouldFire?: boolean;
157-
additionalProperties?: Record<string, unknown>;
158148
}) => {
159149
useTrackOnChange(
160150
(track: TrackFunction) => {
@@ -163,14 +153,13 @@ export const useFireExperimentViewed = ({
163153
}
164154
track('Experiment Viewed', {
165155
test_name: testName,
166-
...additionalProperties,
167-
} as any);
156+
});
168157
},
169-
[shouldFire, testName, additionalProperties],
158+
[shouldFire, testName],
170159
undefined
171160
);
172161
};
173162

174163
export type { TrackFunction };
175164
export { ExperimentTestName, ExperimentTestGroup } from './growth-experiments';
176-
export { useAssignment } from './experimentation-provider';
165+
export { useAssignment, useTrackInSample } from './experimentation-provider';

0 commit comments

Comments
 (0)