@@ -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+
2330interface CompassExperimentationProviderContextValue {
2431 useAssignment : UseAssignmentHook ;
2532 assignExperiment : AssignExperimentFn ;
2633 getAssignment : GetAssignmentFn ;
34+ useTrackInSample : UseTrackInSampleHook ;
2735}
2836
2937const 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
4865export 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<{
77103export 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+ } ;
0 commit comments