Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add analytics to content safety events (#2941)
Dhruv Bhanushaliec7693057904
diff --git a/frontend/src/components/VSafeBrowsing/VSafeBrowsing.vue b/frontend/src/components/VSafeBrowsing/VSafeBrowsing.vueindex 7d3e1bb5b..18fb5f4ad 100644--- a/frontend/src/components/VSafeBrowsing/VSafeBrowsing.vue+++ b/frontend/src/components/VSafeBrowsing/VSafeBrowsing.vue@@ -46,6 +46,7 @@ import { useContext } from "@nuxtjs/composition-api"import { useFeatureFlagStore } from "~/stores/feature-flag"import { useUiStore } from "~/stores/ui"+import { useAnalytics } from "~/composables/use-analytics"import { ON, OFF } from "~/constants/feature-flag"import VCheckbox from "~/components/VCheckbox/VCheckbox.vue"@@ -65,12 +66,14 @@ export default defineComponent({const sensitivityPath = computed(() => app.localePath("/about")) // TODO: Issue#2550const featureFlagStore = useFeatureFlagStore()+ const { sendCustomEvent } = useAnalytics()let fetchSensitive = computed(() =>featureFlagStore.isOn("fetch_sensitive"))let setFetchSensitive = ({ checked }: { checked: boolean }) => {featureFlagStore.toggleFeature("fetch_sensitive", checked ? ON : OFF)+ sendCustomEvent("TOGGLE_FETCH_SENSITIVE", { checked })if (!checked) {// If sensitive content is not fetched, there is nothing to blur/unblur.@@ -83,6 +86,7 @@ export default defineComponent({let blurSensitive = computed(() => uiStore.shouldBlurSensitive)let setBlurSensitive = ({ checked }: { checked: boolean }) => {uiStore.setShouldBlurSensitive(checked)+ sendCustomEvent("TOGGLE_BLUR_SENSITIVE", { checked })}const toggles = [diff --git a/frontend/src/components/VSafetyWall/VSafetyWall.vue b/frontend/src/components/VSafetyWall/VSafetyWall.vueindex 13911be72..aa25271dc 100644--- a/frontend/src/components/VSafetyWall/VSafetyWall.vue+++ b/frontend/src/components/VSafetyWall/VSafetyWall.vue@@ -36,6 +36,7 @@variant="filled-dark"class="label-bold":href="backToSearchPath || '/'"+ @mousedown="handleBack">{{ $t("singleResult.back") }}</VButton>@@ -44,7 +45,7 @@variant="bordered-gray"class="label-bold"has-icon-end- @click="showMedia"+ @click="handleShow">{{ $t("sensitiveContent.singleResult.show") }}<VIcon name="eye-open" />@@ -59,6 +60,7 @@ import { PropType, computed, defineComponent } from "@nuxtjs/composition-api"import { camel } from "case"import { useSearchStore } from "~/stores/search"+import { useAnalytics } from "~/composables/use-analytics"import type { AudioDetail, ImageDetail } from "~/types/media"import VLink from "~/components/VLink.vue"@@ -78,17 +80,25 @@ export default defineComponent({required: true,},},- setup(_, { emit }) {+ setup(props, { emit }) {const searchStore = useSearchStore()const backToSearchPath = computed(() => searchStore.backToSearchPath)- const showMedia = () => {+ const { sendCustomEvent } = useAnalytics()+ const handleBack = () => {+ sendCustomEvent("GO_BACK_FROM_SENSITIVE_RESULT", {+ id: props.media.id,+ sensitivities: props.media.sensitivity.join(","),+ })+ }+ const handleShow = () => {emit("reveal")}return {backToSearchPath,- showMedia,+ handleBack,+ handleShow,camel,}},diff --git a/frontend/src/composables/use-sensitive-media.ts b/frontend/src/composables/use-sensitive-media.tsindex 7fdc2d12b..13fe876a8 100644--- a/frontend/src/composables/use-sensitive-media.ts+++ b/frontend/src/composables/use-sensitive-media.ts@@ -3,6 +3,7 @@ import { computed } from "vue"import type { Media } from "~/types/media"import { useUiStore } from "~/stores/ui"+import { useAnalytics } from "~/composables/use-analytics"import type { SensitiveMediaVisibility } from "~/constants/content-safety"@@ -12,9 +13,10 @@ import type { SensitiveMediaVisibility } from "~/constants/content-safety"* along with toggles for hiding/revealing the media.*/export function useSensitiveMedia(- media: Pick<Media, "id" | "isSensitive"> | null+ media: Pick<Media, "id" | "sensitivity" | "isSensitive"> | null) {const uiStore = useUiStore()+ const { sendCustomEvent } = useAnalytics()/*** The current state of a single sensitive media item.@@ -40,6 +42,10 @@ export function useSensitiveMedia(function reveal() {if (media && !uiStore.revealedSensitiveResults.includes(media.id)) {uiStore.revealedSensitiveResults.push(media.id)+ sendCustomEvent("UNBLUR_SENSITIVE_RESULT", {+ id: media.id,+ sensitivities: media.sensitivity.join(","),+ })}}@@ -49,6 +55,10 @@ export function useSensitiveMedia(if (index > -1) {uiStore.revealedSensitiveResults.splice(index, 1)}+ sendCustomEvent("REBLUR_SENSITIVE_RESULT", {+ id: media.id,+ sensitivities: media.sensitivity.join(","),+ })}const isHidden = computed(diff --git a/frontend/src/types/analytics.ts b/frontend/src/types/analytics.tsindex 21f48c63e..eb5587ea1 100644--- a/frontend/src/types/analytics.ts+++ b/frontend/src/types/analytics.ts@@ -332,6 +332,79 @@ export type Events = {/** The name of the Vue component used on the interaction, e.g. the global or main player. */component: AudioComponent}++ /* Content safety events */++ /**+ * Description: The user flips the sidebar toggle to fetch sensitive results.+ *+ * Questions:+ * - Are users seeking sensitive content from Openverse?+ */+ TOGGLE_FETCH_SENSITIVE: {+ /** whether the switch was turned on or off */+ checked: boolean+ }+ /**+ * Description: The user flips the sidebar toggle to not blur sensitive+ * content in the search results.+ *+ * Questions:+ * - Are users comfortable seeing sensitive content alongside safe results?+ * - Do users find the blurring useful or just an extra step?+ */+ TOGGLE_BLUR_SENSITIVE: {+ /** whether the switch was turned on or off */+ checked: boolean+ }+ /**+ * Description: The user proceeds to see the sensitive content from the+ * content safety wall.+ *+ * Questions:+ * - Do people choose to see a result based on the explanation for why it was+ * marked sensitive?+ * - What sensitivity values are most likely to make a user want to go ahead?+ */+ UNBLUR_SENSITIVE_RESULT: {+ /** the unique ID of the sensitive result */+ id: string+ /** the reasons for why this result is considered sensitive */+ sensitivities: string+ }+ /**+ * Description: The user opts not to see the sensitive content and to go back+ * to the search results from the content safety wall.+ *+ * Questions:+ * - Do people choose not to see a result based on the explanation for why it+ * was marked sensitive?+ * - What sensitivity values are most likely to make a user go back?+ *+ * This event is similar to the `BACK_TO_SEARCH` event, but is separate+ * because it is only triggered from the content safety wall.+ */+ GO_BACK_FROM_SENSITIVE_RESULT: {+ /** the unique ID of the sensitive result */+ id: string+ /** the reasons for why this result is considered sensitive */+ sensitivities: string+ }+ /**+ * Description: The user opts to re-hide the sensitive content that has been+ * unblurred and presented to them.+ *+ * Questions:+ * - What content on Openverse would be so sensitive that a person would want+ * to hide it after they've seen it?+ * - Do users prefer to briefly see and re-hide most sensitive content?+ */+ REBLUR_SENSITIVE_RESULT: {+ /** the unique ID of the sensitive result */+ id: string+ /** the reasons for why this result is considered sensitive */+ sensitivities: string+ }}/**diff --git a/frontend/test/unit/specs/composables/use-sensitive-media.spec.ts b/frontend/test/unit/specs/composables/use-sensitive-media.spec.tsindex c41091da5..b45ed408d 100644--- a/frontend/test/unit/specs/composables/use-sensitive-media.spec.ts+++ b/frontend/test/unit/specs/composables/use-sensitive-media.spec.ts@@ -1,26 +1,45 @@import { useSensitiveMedia } from "~/composables/use-sensitive-media"+import { useAnalytics } from "~/composables/use-analytics"+import type { Sensitivity } from "~/constants/content-safety"let mockUseUiStore = {shouldBlurSensitive: true,revealedSensitiveResults: [],}+jest.mock("~/composables/use-analytics")+jest.mock("~/stores/ui", () => ({useUiStore: () => mockUseUiStore,}))describe("useSensitiveMedia composable", () => {- let mockMedia: { id: string; isSensitive: boolean }+ const sendCustomEventMock = jest.fn()++ let mockMedia: {+ id: string+ sensitivity: Sensitivity[]+ isSensitive: boolean+ }beforeEach(() => {mockMedia = {id: "mock-id",+ sensitivity: [],isSensitive: false,}mockUseUiStore = {shouldBlurSensitive: true,revealedSensitiveResults: [],}++ sendCustomEventMock.mockClear()+ const mockedUseAnalytics = useAnalytics as jest.Mock<+ ReturnType<typeof useAnalytics>+ >+ mockedUseAnalytics.mockImplementation(() => ({+ sendCustomEvent: sendCustomEventMock,+ }))})it("should return non-sensitive when media is null", () => {@@ -55,6 +74,10 @@ describe("useSensitiveMedia composable", () => {reveal()expect(visibility.value).toBe("sensitive-shown")+ expect(sendCustomEventMock).toHaveBeenCalledWith(+ "UNBLUR_SENSITIVE_RESULT",+ { id: "mock-id", sensitivities: "" }+ )})it("should hide sensitive media", () => {@@ -65,6 +88,10 @@ describe("useSensitiveMedia composable", () => {hide()expect(visibility.value).toBe("sensitive-hidden")+ expect(sendCustomEventMock).toHaveBeenCalledWith(+ "REBLUR_SENSITIVE_RESULT",+ { id: "mock-id", sensitivities: "" }+ )})it("should correctly report if a media is hidden", () => {