Skip to content

Commit f726f78

Browse files
committed
fix: add GA
1 parent 83938ef commit f726f78

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

src/components/AIModal/InitialPanel.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { sendGAEvent } from "@next/third-parties/google"
12
import { sampleSize } from "lodash"
23
import Image from "next/image"
34
import { useEffect, useState } from "react"
@@ -20,6 +21,14 @@ const InitialPanel = props => {
2021
}
2122
}, [aiModalVisible])
2223

24+
const handleClickDefaultQuestion = (question: string) => {
25+
onChat(question)
26+
27+
sendGAEvent("event", "click_ai_default_question", {
28+
label: question,
29+
})
30+
}
31+
2332
return (
2433
<Stack
2534
direction="column"
@@ -49,7 +58,7 @@ const InitialPanel = props => {
4958
mb: "1.6rem",
5059
cursor: "pointer",
5160
}}
52-
onClick={() => onChat(item)}
61+
onClick={() => handleClickDefaultQuestion(item)}
5362
>
5463
<EnterSvg></EnterSvg>
5564
<Typography sx={{ fontSize: "1.6rem", lineHeight: "2.4rem", flex: 1, cursor: "inherit", textAlign: "left" }}>{item}</Typography>

src/components/AIModal/MessagePanel.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { sendGAEvent } from "@next/third-parties/google"
12
import clsx from "clsx"
23
import { useEffect, useRef, useState } from "react"
34

@@ -18,7 +19,7 @@ const MessagePanel = props => {
1819

1920
useEffect(() => {
2021
bottomRef.current?.scrollIntoView({ behavior: "smooth" })
21-
}, [data])
22+
}, [data[data.length - 1]?.text])
2223

2324
if (!data || data.length === 0) {
2425
return null
@@ -27,11 +28,27 @@ const MessagePanel = props => {
2728
const handleThumbUp = id => {
2829
setFeedbackAlertVisible(true)
2930
onUpdateData({ id, feedback: "good" })
31+
32+
const messageIndex = data.findIndex(message => message.id === id)
33+
34+
sendGAEvent("event", "click_ai_feedback", {
35+
label: data[messageIndex - 1].text,
36+
id,
37+
feedback: "good",
38+
})
3039
}
3140

3241
const handleThumbDown = id => {
3342
setFeedbackAlertVisible(true)
3443
onUpdateData({ id, feedback: "bad" })
44+
45+
const messageIndex = data.findIndex(message => message.id === id)
46+
47+
sendGAEvent("event", "click_ai_feedback", {
48+
label: data[messageIndex - 1].text,
49+
id,
50+
feedback: "bad",
51+
})
3552
}
3653
return (
3754
<Box
@@ -68,7 +85,7 @@ const MessagePanel = props => {
6885
<div ref={bottomRef}></div>
6986

7087
<Box sx={{ position: "absolute", top: "2.4rem", left: "50%", transform: "translateX(-50%)" }}>
71-
<FeedbackAlert open={feedbackAlertVisible} duration={3e3} onClose={() => setFeedbackAlertVisible(false)}>
88+
<FeedbackAlert open={feedbackAlertVisible} duration={5e3} onClose={() => setFeedbackAlertVisible(false)}>
7289
Thanks for your feedback!
7390
</FeedbackAlert>
7491
</Box>

src/components/AIModal/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export const chatWithAI = async ({ message, prevId }: { message: string; prevId?
2222
input: input as InputMessage[],
2323
previous_response_id: prevId ?? null,
2424
stream: true,
25+
metadata: {
26+
env: process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT,
27+
},
2528
})
2629

2730
return response.toReadableStream()

src/components/Header/AskAI.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
import { sendGAEvent } from "@next/third-parties/google"
2+
import { useEffect, useRef } from "react"
3+
14
import { ButtonBase } from "@mui/material"
25

36
import useGlobalStore from "@/stores/globalStore"
47

58
const AskAI = props => {
69
const { isMobile } = props
710
const { aiModalVisible, changeAIModalVisible } = useGlobalStore()
11+
const aiDurationRef = useRef<number>(null)
12+
13+
useEffect(() => {
14+
if (aiModalVisible) {
15+
aiDurationRef.current = Date.now()
16+
} else {
17+
if (aiDurationRef.current) {
18+
const duration = Date.now() - aiDurationRef.current
19+
sendGAEvent("event", "ai_modal_duration", { duration })
20+
aiDurationRef.current = null // reset the start time
21+
}
22+
}
23+
}, [aiModalVisible])
824

925
const handleToggleAIModal = () => {
1026
if (aiModalVisible) {
1127
changeAIModalVisible(false)
1228
} else {
1329
changeAIModalVisible(true)
30+
sendGAEvent("event", "click_ask_ai")
1431
}
1532
}
1633

0 commit comments

Comments
 (0)