Skip to content

Commit e21e0ad

Browse files
densumeshcdxker
authored andcommitted
feature: pass ab treatment w/ metadata on analytics
1 parent 3c7a726 commit e21e0ad

File tree

9 files changed

+55
-14
lines changed

9 files changed

+55
-14
lines changed

clients/search-component/src/TrieveModal/Chat/FollowupQueries.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useAutoAnimate } from "@formkit/auto-animate/react";
66
import { useModalState } from "../../utils/hooks/modal-context";
77

88
export const FollowupQueries = () => {
9-
const { props, trieveSDK, fingerprint } = useModalState();
9+
const { props, trieveSDK, fingerprint, abTreatment } = useModalState();
1010
const { isDoneReading, askQuestion, messages } = useChatState();
1111
const { suggestedQuestions, isLoadingSuggestedQueries } =
1212
useFollowupQuestions();
@@ -31,6 +31,7 @@ export const FollowupQueries = () => {
3131
metadata: {
3232
followup_query: q,
3333
component_props: props,
34+
ab_treatment: abTreatment,
3435
},
3536
request: {
3637
request_id: requestId,

clients/search-component/src/TrieveModal/Chat/ResponseMessage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const Message = ({
107107
useChatState();
108108
const [positive, setPositive] = React.useState<boolean | null>(null);
109109
const [copied, setCopied] = React.useState<boolean>(false);
110-
const { props, trieveSDK, fingerprint } = useModalState();
110+
const { props, trieveSDK, fingerprint, abTreatment } = useModalState();
111111

112112
useEffect(() => {
113113
if (props.analytics) {
@@ -130,6 +130,7 @@ export const Message = ({
130130
return chunk.tracking_id ?? "";
131131
}),
132132
fingerprint,
133+
abTreatment,
133134
});
134135
}
135136
}
@@ -189,6 +190,7 @@ export const Message = ({
189190
requestID: message.queryId,
190191
chunkID: item.id,
191192
fingerprint,
193+
abTreatment,
192194
});
193195
}
194196
}}
@@ -285,6 +287,7 @@ export const Message = ({
285287
requestID: message.queryId,
286288
chunkID: item.id,
287289
fingerprint,
290+
abTreatment,
288291
});
289292
}
290293
}}
@@ -351,6 +354,7 @@ export const Message = ({
351354
requestID: message.queryId,
352355
chunkID: item.id,
353356
fingerprint,
357+
abTreatment,
354358
});
355359
}
356360
}}

clients/search-component/src/TrieveModal/Chat/SuggestedQuestions.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const SuggestedQuestions = ({
2020
const { suggestedQuestions, isLoadingSuggestedQueries, getQuestions } =
2121
useSuggestedQuestions();
2222

23-
const { props, trieveSDK, fingerprint } = useModalState();
23+
const { props, trieveSDK, fingerprint, abTreatment } = useModalState();
2424
const [parent] = useAutoAnimate({ duration: 100 });
2525

2626
if (messages.length) {
@@ -48,6 +48,7 @@ export const SuggestedQuestions = ({
4848
metadata: {
4949
followup_query: q,
5050
component_props: props,
51+
ab_treatment: abTreatment,
5152
},
5253
request: {
5354
request_id: requestId,

clients/search-component/src/TrieveModal/Search/DocsItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Props = {
1111
};
1212

1313
export const DocsItem = ({ item, requestID, index, className }: Props) => {
14-
const { props, trieveSDK, fingerprint } = useModalState();
14+
const { props, trieveSDK, fingerprint, abTreatment } = useModalState();
1515
const Component = item.chunk.link ? "a" : "button";
1616
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1717
const itemRef = useRef<HTMLButtonElement | HTMLLinkElement | any>(null);
@@ -143,6 +143,7 @@ export const DocsItem = ({ item, requestID, index, className }: Props) => {
143143
requestID: requestID,
144144
chunkID: chunk.id,
145145
fingerprint,
146+
abTreatment,
146147
});
147148
}
148149
};

clients/search-component/src/TrieveModal/Search/ProductItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const ProductItem = ({
4040
betterGroupName,
4141
ctrType: type,
4242
}: Props) => {
43-
const { props, trieveSDK, fingerprint } = useModalState();
43+
const { props, trieveSDK, fingerprint, abTreatment } = useModalState();
4444
const { chatWithGroup } = useChatState();
4545
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4646
const itemRef = useRef<HTMLButtonElement | HTMLLinkElement | any>(null);
@@ -128,6 +128,7 @@ export const ProductItem = ({
128128
requestID: requestID,
129129
chunkID: chunk.id,
130130
fingerprint,
131+
abTreatment,
131132
});
132133
}
133134

clients/search-component/src/TrieveModal/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import { createPortal } from "react-dom";
2121

2222
const Modal = () => {
2323
useKeyboardNavigation();
24-
const { open, setOpen, setMode, setQuery, props, display } = useModalState();
24+
const { open, setOpen, setMode, setQuery, props, display, abTreatment } =
25+
useModalState();
2526
const { askQuestion, chatWithGroup, cancelGroupChat, clearConversation } =
2627
useChatState();
2728

@@ -87,6 +88,7 @@ const Modal = () => {
8788
location: window.location.href,
8889
metadata: {
8990
component_props: props,
91+
ab_treatment: abTreatment,
9092
},
9193
},
9294
abortController.signal,
@@ -99,6 +101,10 @@ const Modal = () => {
99101
items: [],
100102
user_id: fingerprint,
101103
location: window.location.href,
104+
metadata: {
105+
component_props: props,
106+
ab_treatment: abTreatment,
107+
},
102108
});
103109
}
104110
}

clients/search-component/src/utils/hooks/chat-context.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
119119
selectedTags,
120120
currentGroup,
121121
props,
122+
abTreatment,
122123
} = useModalState();
123124
const [currentQuestion, setCurrentQuestion] = useState(query);
124125
const [currentTopic, setCurrentTopic] = useState("");
@@ -163,6 +164,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
163164
owner_id: fingerprint,
164165
metadata: {
165166
component_props: props,
167+
ab_treatment: abTreatment,
166168
},
167169
});
168170
});
@@ -322,6 +324,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
322324
return chunk.tracking_id ?? "";
323325
}),
324326
fingerprint,
327+
abTreatment,
325328
});
326329
}
327330
}
@@ -1113,6 +1116,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
11131116
filters: createMessageFilters,
11141117
metadata: {
11151118
component_props: props,
1119+
ab_treatment: abTreatment,
11161120
},
11171121
currency: props.defaultCurrency,
11181122
highlight_options: {
@@ -1337,6 +1341,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
13371341
query_id: queryId,
13381342
metadata: {
13391343
component_props: props,
1344+
ab_treatment: abTreatment,
13401345
},
13411346
});
13421347
}

clients/search-component/src/utils/hooks/modal-context.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ const ModalContext = createContext<{
363363
resetHeight: () => void;
364364
addHeight: (height: number) => void;
365365
display: boolean;
366+
abTreatment?: string;
366367
}>({
367368
props: defaultProps,
368369
trieveSDK: (() => {}) as unknown as TrieveSDK,
@@ -404,6 +405,7 @@ const ModalContext = createContext<{
404405
// eslint-disable-next-line @typescript-eslint/no-unused-vars
405406
addHeight: (height: number) => {},
406407
display: true,
408+
abTreatment: undefined,
407409
});
408410

409411
const ModalProvider = ({
@@ -453,6 +455,7 @@ const ModalProvider = ({
453455
const [display, setDisplay] = useState(
454456
!props.experimentIds || props.experimentIds.length === 0,
455457
);
458+
const [abTreatment, setAbTreatment] = useState<string | undefined>(undefined);
456459

457460
const trieve = new TrieveSDK({
458461
baseUrl: props.baseUrl,
@@ -538,6 +541,7 @@ const ModalProvider = ({
538541
abortController,
539542
filters,
540543
type: props.type,
544+
abTreatment,
541545
});
542546
const groupMap = new Map<string, GroupChunk[]>();
543547
results.groups.forEach((group) => {
@@ -591,6 +595,7 @@ const ModalProvider = ({
591595
abortController,
592596
filters,
593597
type: props.type,
598+
abTreatment,
594599
});
595600
if (results.transcribedQuery && audioBase64) {
596601
setQuery(results.transcribedQuery);
@@ -647,16 +652,17 @@ const ModalProvider = ({
647652
for (const experimentId of props.experimentIds) {
648653
trieve
649654
.getTreatment({
650-
experiment_id: experimentId,
651-
user_id: fingerprint,
655+
experiment_id: experimentId,
656+
user_id: fingerprint,
652657
})
653658
.then((treatment) => {
654-
if (treatment.treatment_name === "Don't show") {
655-
setDisplay(false);
656-
} else {
657-
setDisplay(true);
658-
}
659-
});
659+
if (treatment.treatment_name === "Don't show") {
660+
setDisplay(false);
661+
} else {
662+
setDisplay(true);
663+
}
664+
setAbTreatment(treatment.treatment_name);
665+
});
660666
}
661667
}
662668
}, [props.experimentIds, fingerprint]);
@@ -686,6 +692,7 @@ const ModalProvider = ({
686692
location: window.location.href,
687693
metadata: {
688694
component_props: props,
695+
ab_treatment: abTreatment,
689696
},
690697
},
691698
abortController.signal,
@@ -716,6 +723,7 @@ const ModalProvider = ({
716723
location: window.location.href,
717724
metadata: {
718725
component_props: props,
726+
ab_treatment: abTreatment,
719727
},
720728
},
721729
abortController.signal,
@@ -865,6 +873,7 @@ const ModalProvider = ({
865873
resetHeight,
866874
addHeight,
867875
display,
876+
abTreatment,
868877
}}
869878
>
870879
{children}

clients/search-component/src/utils/trieve.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const searchWithTrieve = async ({
4040
filters,
4141
type,
4242
fingerprint,
43+
abTreatment,
4344
}: {
4445
trieve: TrieveSDK;
4546
props: ModalProps;
@@ -51,6 +52,7 @@ export const searchWithTrieve = async ({
5152
filters?: ChunkFilter;
5253
type?: ModalTypes;
5354
fingerprint?: string;
55+
abTreatment?: string;
5456
}) => {
5557
const scoreThreshold =
5658
searchOptions.score_threshold ??
@@ -90,6 +92,7 @@ export const searchWithTrieve = async ({
9092
filters,
9193
metadata: {
9294
component_props: props,
95+
ab_treatment: abTreatment,
9396
},
9497
user_id: fingerprint,
9598
typo_options: {
@@ -119,6 +122,7 @@ export const searchWithTrieve = async ({
119122
filters,
120123
metadata: {
121124
component_props: props,
125+
ab_treatment: abTreatment,
122126
},
123127
user_id: fingerprint,
124128
typo_options: {
@@ -167,6 +171,7 @@ export const groupSearchWithTrieve = async ({
167171
filters,
168172
type,
169173
fingerprint,
174+
abTreatment,
170175
}: {
171176
props: ModalProps;
172177
trieve: TrieveSDK;
@@ -178,6 +183,7 @@ export const groupSearchWithTrieve = async ({
178183
filters?: ChunkFilter;
179184
type?: ModalTypes;
180185
fingerprint?: string;
186+
abTreatment?: string;
181187
}) => {
182188
const scoreThreshold =
183189
searchOptions.score_threshold ??
@@ -213,6 +219,7 @@ export const groupSearchWithTrieve = async ({
213219
group_size: 1,
214220
metadata: {
215221
component_props: props,
222+
ab_treatment: abTreatment,
216223
},
217224
user_id: fingerprint,
218225
search_type: searchOptions.search_type ?? "fulltext",
@@ -296,6 +303,7 @@ export const sendCtrData = async ({
296303
index,
297304
props,
298305
fingerprint,
306+
abTreatment,
299307
}: {
300308
trieve: TrieveSDK;
301309
chunkID: string;
@@ -304,6 +312,7 @@ export const sendCtrData = async ({
304312
index: number;
305313
props: ModalProps;
306314
fingerprint: string;
315+
abTreatment?: string;
307316
}) => {
308317
if (props.previewTopicId === undefined) {
309318
await trieve.sendAnalyticsEvent({
@@ -321,6 +330,7 @@ export const sendCtrData = async ({
321330
user_id: fingerprint,
322331
metadata: {
323332
component_props: props,
333+
ab_treatment: abTreatment,
324334
},
325335
});
326336
}
@@ -335,13 +345,15 @@ export const trackViews = async ({
335345
items,
336346
props,
337347
fingerprint,
348+
abTreatment,
338349
}: {
339350
trieve: TrieveSDK;
340351
requestID: string;
341352
type: CTRType;
342353
items: string[];
343354
props: ModalProps;
344355
fingerprint: string;
356+
abTreatment?: string;
345357
}) => {
346358
if (props.previewTopicId === undefined) {
347359
let lastMessageString = "{}";
@@ -370,6 +382,7 @@ export const trackViews = async ({
370382
user_id: fingerprint,
371383
metadata: {
372384
component_props: props,
385+
ab_treatment: abTreatment,
373386
},
374387
});
375388
}

0 commit comments

Comments
 (0)