Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Analytics event: `CHANGE_CONTENT_TYPE` (#1151)

Zack Krida7cb89302d278
diff --git a/frontend/src/components/VContentLink/VContentLink.vue b/frontend/src/components/VContentLink/VContentLink.vue
index f52e6d512..600bc337e 100644
--- a/frontend/src/components/VContentLink/VContentLink.vue
+++ b/frontend/src/components/VContentLink/VContentLink.vue
@@ -9,6 +9,7 @@
: 'cursor-not-allowed text-dark-charcoal/40'
"
@keydown.native.shift.tab.exact="$emit('shift-tab', $event)"
+ @mousedown="handleClick"
>
<div class="flex flex-col items-start md:flex-row md:items-center">
<VIcon :name="mediaType" />
@@ -26,13 +27,17 @@
<script lang="ts">
import { computed, defineComponent, PropType } from "vue"
+import { useAnalytics } from "~/composables/use-analytics"
+
import { useI18nResultsCount } from "~/composables/use-i18n-utilities"
import type { SupportedMediaType } from "~/constants/media"
import { defineEvent } from "~/types/emits"
-import VLink from "~/components/VLink.vue"
+import useSearchType from "~/composables/use-search-type"
+
import VIcon from "~/components/VIcon/VIcon.vue"
+import VLink from "~/components/VLink.vue"
export default defineComponent({
name: "VContentLink",
@@ -68,9 +73,22 @@ export default defineComponent({
const hasResults = computed(() => props.resultsCount > 0)
const resultsCountLabel = computed(() => getI18nCount(props.resultsCount))
+ const { activeType } = useSearchType()
+ const analytics = useAnalytics()
+
+ const handleClick = () => {
+ analytics.sendCustomEvent("CHANGE_CONTENT_TYPE", {
+ previous: activeType.value,
+ next: props.mediaType,
+ component: "VContentLink",
+ })
+ }
+
return {
resultsCountLabel,
hasResults,
+
+ handleClick,
}
},
})
diff --git a/frontend/src/composables/use-analytics.ts b/frontend/src/composables/use-analytics.ts
index a6c4b4080..8640340a3 100644
--- a/frontend/src/composables/use-analytics.ts
+++ b/frontend/src/composables/use-analytics.ts
@@ -6,6 +6,8 @@ import { useUiStore } from "~/stores/ui"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { useI18n } from "~/composables/use-i18n"
+import { log } from "~/utils/console"
+
/**
* The `ctx` parameter must be supplied if using this composable outside the
* bounds of the composition API.
@@ -66,6 +68,7 @@ export const useAnalytics = () => {
name: T,
payload: Events[T]
) => {
+ log(`Analytics event: ${name}`, payload)
$plausible.trackEvent(name, {
props: {
...isomorphicProps.value,
diff --git a/frontend/src/composables/use-component-name.ts b/frontend/src/composables/use-component-name.ts
new file mode 100644
index 000000000..391b6da67
--- /dev/null
+++ b/frontend/src/composables/use-component-name.ts
@@ -0,0 +1,10 @@
+import { getCurrentInstance } from "vue"
+
+/**
+ * Retrieve the name of the component in which
+ * the composable is being used.
+ */
+export const useComponentName = () => {
+ const vm = getCurrentInstance()
+ return vm?.proxy.$options.name ?? "Unknown"
+}
diff --git a/frontend/src/composables/use-search-type.ts b/frontend/src/composables/use-search-type.ts
index be3ba1e95..a91881d12 100644
--- a/frontend/src/composables/use-search-type.ts
+++ b/frontend/src/composables/use-search-type.ts
@@ -16,6 +16,10 @@ import { useFeatureFlagStore } from "~/stores/feature-flag"
import { useI18n } from "~/composables/use-i18n"
+import { useAnalytics } from "~/composables/use-analytics"
+
+import { useComponentName } from "./use-component-name"
+
const icons = {
[ALL_MEDIA]: "all",
[AUDIO]: "audio",
@@ -34,6 +38,9 @@ const labels = {
export default function useSearchType() {
const i18n = useI18n()
+ const componentName = useComponentName()
+ const analytics = useAnalytics()
+
const activeType = computed(() => useSearchStore().searchType)
const previousSearchType = ref(activeType.value)
@@ -47,6 +54,12 @@ export default function useSearchType() {
const setActiveType = (searchType: SearchType) => {
if (previousSearchType.value === searchType) return
+
+ analytics.sendCustomEvent("CHANGE_CONTENT_TYPE", {
+ previous: previousSearchType.value,
+ next: searchType,
+ component: componentName,
+ })
useSearchStore().setSearchType(searchType)
previousSearchType.value = searchType
}
diff --git a/frontend/src/types/analytics.ts b/frontend/src/types/analytics.ts
index ff7f4d633..5c41adeee 100644
--- a/frontend/src/types/analytics.ts
+++ b/frontend/src/types/analytics.ts
@@ -101,6 +101,21 @@ export type Events = {
/** The component that triggered the event */
component: "VNoResults" | "VExternalSourceList"
}
+ /**
+ * Description: Whenever a user changes the content type
+ * Questions:
+ * - Which content types are most popular?
+ * - Is there interest in the non-supported content types?
+ * - Do users switch content types? Where in their journeys?
+ */
+ CHANGE_CONTENT_TYPE: {
+ /** The previously-set media type */
+ previous: SearchType
+ /** The new media type */
+ next: SearchType
+ /** The name of the Vue component used to switch content types. */
+ component: string
+ }
}
/**
diff --git a/frontend/test/unit/specs/components/v-content-link.spec.js b/frontend/test/unit/specs/components/v-content-link.spec.js
index 56a856d23..42a43e745 100644
--- a/frontend/test/unit/specs/components/v-content-link.spec.js
+++ b/frontend/test/unit/specs/components/v-content-link.spec.js
@@ -1,9 +1,15 @@
-import { screen } from "@testing-library/vue"
+import { fireEvent, screen } from "@testing-library/vue"
import { render } from "~~/test/unit/test-utils/render"
+import { useAnalytics } from "~/composables/use-analytics"
+
import VContentLink from "~/components/VContentLink/VContentLink.vue"
+jest.mock("~/composables/use-analytics", () => ({
+ useAnalytics: jest.fn(),
+}))
+
describe("VContentLink", () => {
let options = {}
@@ -30,4 +36,21 @@ describe("VContentLink", () => {
expect(btn).toHaveAttribute("aria-disabled")
expect(btn.getAttribute("aria-disabled")).toBeTruthy()
})
+
+ it("sends CHANGE_CONTENT_TYPE event when clicked", async () => {
+ const sendCustomEventMock = jest.fn()
+
+ useAnalytics.mockImplementation(() => ({
+ sendCustomEvent: sendCustomEventMock,
+ }))
+ render(VContentLink, options)
+ const btn = screen.getByRole("link")
+
+ await fireEvent.click(btn)
+ expect(sendCustomEventMock).toHaveBeenCalledWith("CHANGE_CONTENT_TYPE", {
+ component: "VContentLink",
+ next: "image",
+ previous: "all",
+ })
+ })
})
diff --git a/frontend/test/unit/specs/composables/use-search-type.spec.js b/frontend/test/unit/specs/composables/use-search-type.spec.js
new file mode 100644
index 000000000..1ef69cabe
--- /dev/null
+++ b/frontend/test/unit/specs/composables/use-search-type.spec.js
@@ -0,0 +1,87 @@
+import { createPinia, setActivePinia } from "~~/test/unit/test-utils/pinia"
+
+import useSearchType from "~/composables/use-search-type"
+import { useAnalytics } from "~/composables/use-analytics"
+
+jest.mock("~/composables/use-analytics")
+
+import { ALL_MEDIA, AUDIO, IMAGE } from "~/constants/media"
+
+jest.mock("~/composables/use-i18n", () => ({
+ useI18n: jest.fn(() => ({
+ t: (key) => key,
+ })),
+}))
+
+describe("useSearchType", () => {
+ const sendCustomEventMock = jest.fn()
+ beforeEach(() => {
+ sendCustomEventMock.mockClear()
+
+ setActivePinia(createPinia())
+ useAnalytics.mockImplementation(() => ({
+ sendCustomEvent: sendCustomEventMock,
+ }))
+ })
+
+ it("should have correct initial values", () => {
+ const {
+ activeType,
+ types: searchTypes,
+ icons,
+ labels,
+ additionalTypes,
+ } = useSearchType()
+ expect(activeType.value).toEqual(ALL_MEDIA)
+ expect(searchTypes).toEqual([ALL_MEDIA, IMAGE, AUDIO])
+ expect(icons).toEqual({
+ all: "all",
+ audio: "audio",
+ image: "image",
+ "model-3d": "model-3d",
+ video: "video",
+ })
+ expect(labels).toEqual({
+ all: "search-type.all",
+ audio: "search-type.audio",
+ image: "search-type.image",
+ "model-3d": "search-type.model-3d",
+ video: "search-type.video",
+ })
+ expect(additionalTypes.value).toEqual([])
+ })
+
+ it("should return correct props for active search type when type is not passed", () => {
+ const { getSearchTypeProps } = useSearchType()
+
+ const { icon, label } = getSearchTypeProps()
+ expect(icon).toEqual(ALL_MEDIA)
+ expect(label).toEqual("search-type.all")
+ })
+
+ it("should return correct props when type is passed", () => {
+ const { getSearchTypeProps } = useSearchType()
+
+ const { icon, label } = getSearchTypeProps(AUDIO)
+ expect(icon).toEqual(AUDIO)
+ expect(label).toEqual("search-type.audio")
+ })
+
+ it("should send the analytics event when setActiveType is called", () => {
+ const { setActiveType } = useSearchType()
+
+ setActiveType(AUDIO)
+ expect(sendCustomEventMock).toHaveBeenCalledWith("CHANGE_CONTENT_TYPE", {
+ component: "Unknown",
+ next: AUDIO,
+ previous: ALL_MEDIA,
+ })
+ })
+
+ it("should not send the analytics event when setActiveType is called with current type", () => {
+ const { setActiveType } = useSearchType()
+
+ setActiveType(ALL_MEDIA)
+ expect(sendCustomEventMock).not.toHaveBeenCalled()
+ })
+})