Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add matomo event to lang picker input #12276

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/components/LanguagePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ const LanguagePicker = ({
menuState,
...props
}: LanguagePickerProps) => {
const { t, refs, disclosure, filterValue, setFilterValue, filteredNames } =
useLanguagePicker(handleClose, menuState)
const {
t,
refs,
disclosure,
filterValue,
setFilterValue,
filteredNames,
handleInputFocus,
} = useLanguagePicker(handleClose, menuState)
const { inputRef, firstItemRef, noResultsRef, footerRef } = refs
const { onClose } = disclosure

Expand Down Expand Up @@ -137,6 +144,7 @@ const LanguagePicker = ({
e.stopPropagation()
}
}}
onFocus={handleInputFocus}
/>
<InputRightElement
hideBelow="lg" // TODO: Confirm breakpoint after nav-menu PR merged
Expand Down
24 changes: 24 additions & 0 deletions src/components/LanguagePicker/useLanguagePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export const useLanguagePicker = (

const [filteredNames, setFilteredNames] = useState<LocaleDisplayInfo[]>([])

// Used to only send one matomo event for users who focus the filter input
const [hasFocusedInput, setHasFocusedInput] = useState(false)

// Reset if user switches languages
useEffect(() => {
setHasFocusedInput(false)
}, [locale])

// perform all the filtering and mapping when the filter value change
useEffect(() => {
const locales = filterRealLocales(rawLocales)
Expand Down Expand Up @@ -188,12 +196,28 @@ export const useLanguagePicker = (
)
}

/**
* Send Matomo event when user focuses in the filter input.
* Only send once per user per session per language
* @returns void
*/
const handleInputFocus = (): void => {
if (hasFocusedInput) return
trackCustomEvent({
...eventBase,
eventAction: "Filter input",
eventName: "Focused inside filter input",
})
setHasFocusedInput(true)
}

return {
t,
refs,
disclosure: { isOpen, onOpen, onClose },
filterValue,
setFilterValue,
filteredNames,
handleInputFocus,
}
}
3 changes: 2 additions & 1 deletion src/components/PageMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import Head from "next/head"
import { useRouter } from "next/router"
import { useTranslation } from "next-i18next"

import { getOgImage } from "@/lib/utils/metadata"
import { filterRealLocales } from "@/lib/utils/translations"
import { getFullUrl } from "@/lib/utils/url"
import { getOgImage } from "@/lib/utils/metadata"

import { SITE_URL } from "@/lib/constants"

type NameMeta = {
Expand Down
Loading