Skip to content

Commit 8045dcb

Browse files
authored
Add additional event capturing for some interactions (#1596)
Adds event capturing for facet selection and search term updating, so we can track these more efficiently in PostHog.
1 parent e93a23a commit 8045dcb

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

frontends/mit-learn/src/page-components/Dialogs/AddToListDialog.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "ol-components"
1111

1212
import { RiAddLine } from "@remixicon/react"
13+
import { usePostHog } from "posthog-js/react"
1314

1415
import * as NiceModal from "@ebay/nice-modal-react"
1516

@@ -67,6 +68,7 @@ const AddToListDialogInner: React.FC<AddToListDialogInnerProps> = ({
6768
isLoading: isSavingLearningPathRelationships,
6869
mutateAsync: setLearningPathRelationships,
6970
} = useLearningResourceSetLearningPathRelationships()
71+
const posthog = usePostHog()
7072
const isSaving =
7173
isSavingLearningPathRelationships || isSavingUserListRelationships
7274
let dialogTitle = "Add to list"
@@ -93,6 +95,9 @@ const AddToListDialogInner: React.FC<AddToListDialogInnerProps> = ({
9395
: null,
9496
)
9597
.filter((value) => value !== null)
98+
99+
const { POSTHOG } = APP_SETTINGS
100+
96101
const formik = useFormik({
97102
enableReinitialize: true,
98103
validateOnChange: false,
@@ -103,6 +108,15 @@ const AddToListDialogInner: React.FC<AddToListDialogInnerProps> = ({
103108
},
104109
onSubmit: async (values) => {
105110
if (resource) {
111+
if (!(!POSTHOG?.api_key || POSTHOG.api_key.length < 1)) {
112+
posthog.capture("lr_add_to_list", {
113+
listType: listType,
114+
resourceId: resource?.id,
115+
readableId: resource?.readable_id,
116+
platformCode: resource?.platform?.code,
117+
resourceType: resource?.resource_type,
118+
})
119+
}
106120
if (listType === ListType.LearningPath) {
107121
const newParents = values.learning_paths.map((id) => parseInt(id))
108122
await setLearningPathRelationships({

frontends/mit-learn/src/pages/SearchPage/SearchPage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { LearningResourceOfferor } from "api"
2020
import { useOfferorsList } from "api/hooks/learningResources"
2121
import { capitalize } from "ol-utilities"
2222
import MetaTags from "@/page-components/MetaTags/MetaTags"
23+
import { usePostHog } from "posthog-js/react"
2324

2425
const cssGradient = `
2526
linear-gradient(
@@ -175,6 +176,8 @@ const useFacetManifest = (resourceCategory: string | null) => {
175176
const SearchPage: React.FC = () => {
176177
const [searchParams, setSearchParams] = useSearchParams()
177178
const facetManifest = useFacetManifest(searchParams.get("resource_category"))
179+
const posthog = usePostHog()
180+
const { POSTHOG } = APP_SETTINGS
178181

179182
const setPage = useCallback(
180183
(newPage: number) => {
@@ -191,8 +194,11 @@ const SearchPage: React.FC = () => {
191194
[setSearchParams],
192195
)
193196
const onFacetsChange = useCallback(() => {
197+
if (!(!POSTHOG?.api_key || POSTHOG.api_key.length < 1)) {
198+
posthog.capture("search_update")
199+
}
194200
setPage(1)
195-
}, [setPage])
201+
}, [setPage, posthog, POSTHOG])
196202

197203
const {
198204
params,

frontends/ol-ckeditor/src/types/settings.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/* eslint-disable no-var */
22

3+
export type PostHogSettings = {
4+
api_key: string
5+
timeout?: int
6+
bootstrap_flags?: Record<string, string | boolean>
7+
}
8+
39
export declare global {
410
const APP_SETTINGS: {
511
EMBEDLY_KEY: string
@@ -9,5 +15,6 @@ export declare global {
915
PUBLIC_URL: string
1016
SITE_NAME: string
1117
CSRF_COOKIE_NAME: string
18+
POSTHOG?: PostHogSettings
1219
}
1320
}

frontends/ol-components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"material-ui-popup-state": "^5.1.0",
2727
"ol-test-utilities": "0.0.0",
2828
"ol-utilities": "0.0.0",
29+
"posthog-js": "^1.165.0",
2930
"react": "18.3.1",
3031
"react-router": "^6.22.2",
3132
"react-router-dom": "^6.22.2",

frontends/ol-components/src/components/SearchInput/SearchInput.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RiSearch2Line, RiCloseLine } from "@remixicon/react"
33
import { Input, AdornmentButton } from "../Input/Input"
44
import type { InputProps } from "../Input/Input"
55
import styled from "@emotion/styled"
6+
import { usePostHog } from "posthog-js/react"
67

78
const StyledInput = styled(Input)(({ theme }) => ({
89
boxShadow: "0px 8px 20px 0px rgba(120, 147, 172, 0.10)",
@@ -53,18 +54,32 @@ const muiInputProps = { "aria-label": "Search for" }
5354

5455
const SearchInput: React.FC<SearchInputProps> = (props) => {
5556
const { onSubmit, value } = props
56-
const handleSubmit = useCallback(() => {
57-
const event = {
58-
target: { value },
59-
preventDefault: () => null,
60-
}
61-
onSubmit(event)
62-
}, [onSubmit, value])
57+
const posthog = usePostHog()
58+
const { POSTHOG } = APP_SETTINGS
59+
60+
const handleSubmit = useCallback(
61+
(
62+
ev:
63+
| React.SyntheticEvent<HTMLInputElement>
64+
| React.SyntheticEvent<HTMLButtonElement, MouseEvent>,
65+
isEnter: boolean = false,
66+
) => {
67+
const event = {
68+
target: { value },
69+
preventDefault: () => null,
70+
}
71+
if (!(!POSTHOG?.api_key || POSTHOG.api_key.length < 1)) {
72+
posthog.capture("search_update", { isEnter: isEnter })
73+
}
74+
onSubmit(event)
75+
},
76+
[onSubmit, value, posthog, POSTHOG],
77+
)
6378
const onInputKeyDown: React.KeyboardEventHandler<HTMLInputElement> =
6479
useCallback(
6580
(e) => {
6681
if (e.key !== "Enter") return
67-
handleSubmit()
82+
handleSubmit(e, true)
6883
},
6984
[handleSubmit],
7085
)

yarn.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)