Skip to content

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

Add analytics events for external links (#2247)

Olga Bulat49bafdae0d4c
diff --git a/frontend/src/components/VAudioTrack/layouts/VFullLayout.vue b/frontend/src/components/VAudioTrack/layouts/VFullLayout.vue
index 07419feec..87155146c 100644
--- a/frontend/src/components/VAudioTrack/layouts/VFullLayout.vue
+++ b/frontend/src/components/VAudioTrack/layouts/VFullLayout.vue
@@ -34,6 +34,7 @@
<VLink
class="rounded-sm p-px focus:outline-none focus:ring focus:ring-pink"
:href="audio.creator_url"
+ :send-external-link-click-event="false"
>
{{ audio.creator }}
</VLink>
@@ -59,6 +60,7 @@
show-external-icon
:external-icon-size="6"
class="description-bold order-1 my-1 ms-auto flex-shrink-0 lg:order-2"
+ :send-external-link-click-event="false"
@click="sendGetMediaEvent"
>
{{ $t("audio-details.weblink") }}
diff --git a/frontend/src/components/VContentLink/VContentLink.vue b/frontend/src/components/VContentLink/VContentLink.vue
index 7c009eaa6..47ca0af2d 100644
--- a/frontend/src/components/VContentLink/VContentLink.vue
+++ b/frontend/src/components/VContentLink/VContentLink.vue
@@ -6,6 +6,7 @@
variant="bordered-gray"
size="disabled"
class="h-auto w-full flex-col !items-start !justify-start gap-1 overflow-hidden p-4 sm:h-18 sm:flex-row sm:!items-center sm:gap-2 sm:px-6"
+ :send-external-link-click-event="false"
@keydown.native.shift.tab.exact="$emit('shift-tab', $event)"
@mousedown="handleClick"
>
diff --git a/frontend/src/components/VContentReport/VContentReportForm.vue b/frontend/src/components/VContentReport/VContentReportForm.vue
index 358a145f9..ae6e4fbd1 100644
--- a/frontend/src/components/VContentReport/VContentReportForm.vue
+++ b/frontend/src/components/VContentReport/VContentReportForm.vue
@@ -98,6 +98,7 @@
show-external-icon
:external-icon-size="6"
:href="DMCA_FORM_URL"
+ :send-external-link-click-event="false"
@click="handleDmcaSubmit"
>
{{ $t("media-details.content-report.form.dmca.open") }}
diff --git a/frontend/src/components/VLink.vue b/frontend/src/components/VLink.vue
index a7e7b7968..2edd8d116 100644
--- a/frontend/src/components/VLink.vue
+++ b/frontend/src/components/VLink.vue
@@ -21,6 +21,7 @@
:role="href ? undefined : 'link'"
:aria-disabled="!href"
:class="{ 'inline-flex w-max items-center gap-x-2': showExternalIcon }"
+ @click="handleExternalClick"
v-on="$listeners"
>
<slot /><VIcon
@@ -46,6 +47,8 @@
import { computed, defineComponent } from "vue"
import { useContext } from "@nuxtjs/composition-api"
+import { useAnalytics } from "~/composables/use-analytics"
+
import VIcon from "~/components/VIcon/VIcon.vue"
export default defineComponent({
@@ -73,6 +76,14 @@ export default defineComponent({
type: Number,
default: 4,
},
+ /**
+ * Whether the generic EXTERNAL_LINK_CLICK event should be sent on click.
+ * Set to `false` if the link click is tracked by another analytics event.
+ */
+ sendExternalLinkClickEvent: {
+ type: Boolean,
+ default: true,
+ },
},
setup(props) {
const { app } = useContext()
@@ -80,6 +91,7 @@ export default defineComponent({
href: string
showExternalIcon: boolean
externalIconSize: number
+ sendExternalLinkClickEvent: boolean
} {
return typeof p.href === "string" && !["", "#"].includes(p.href)
}
@@ -96,10 +108,21 @@ export default defineComponent({
: null
)
+ const { sendCustomEvent } = useAnalytics()
+
+ const handleExternalClick = () => {
+ if (!checkHref(props) || !props.sendExternalLinkClickEvent) return
+ sendCustomEvent("EXTERNAL_LINK_CLICK", {
+ url: props.href,
+ })
+ }
+
return {
linkTo,
isNuxtLink,
isInternal,
+
+ handleExternalClick,
}
},
})
diff --git a/frontend/src/components/VMediaInfo/VCopyLicense.vue b/frontend/src/components/VMediaInfo/VCopyLicense.vue
index 8f3b4a3df..5becd9878 100644
--- a/frontend/src/components/VMediaInfo/VCopyLicense.vue
+++ b/frontend/src/components/VMediaInfo/VCopyLicense.vue
@@ -17,7 +17,10 @@
>
<!-- Disable reason: We control the attribution HTML generation so this is safe and will not lead to XSS attacks -->
<!-- eslint-disable vue/no-v-html -->
- <div v-html="getAttributionMarkup({ includeIcons: false })" />
+ <div
+ ref="richRef"
+ v-html="getAttributionMarkup({ includeIcons: false })"
+ />
<!-- eslint-enable vue/no-v-html -->
</VLicenseTabPanel>
<VLicenseTabPanel
@@ -41,11 +44,12 @@
</template>
<script lang="ts">
-import { defineComponent, PropType } from "vue"
+import { defineComponent, onBeforeUnmount, onMounted, PropType, ref } from "vue"
import { AttributionOptions, getAttribution } from "~/utils/attribution-html"
import type { Media } from "~/types/media"
import { useI18n } from "~/composables/use-i18n"
+import { useAnalytics } from "~/composables/use-analytics"
import VTabs from "~/components/VTabs/VTabs.vue"
import VTab from "~/components/VTabs/VTab.vue"
@@ -63,10 +67,36 @@ export default defineComponent({
},
},
setup(props) {
+ const richRef = ref<HTMLElement | null>(null)
+
const i18n = useI18n()
const getAttributionMarkup = (options?: AttributionOptions) =>
getAttribution(props.media, i18n, options)
+
+ const { sendCustomEvent } = useAnalytics()
+
+ const sendAnalyticsEvent = (event: MouseEvent) => {
+ if (!event.currentTarget) return
+
+ const url = (event.currentTarget as HTMLAnchorElement).href
+ sendCustomEvent("EXTERNAL_LINK_CLICK", { url })
+ }
+
+ onMounted(() => {
+ richRef.value?.querySelectorAll("a").forEach((link) => {
+ link.addEventListener("click", sendAnalyticsEvent)
+ })
+ })
+
+ onBeforeUnmount(() => {
+ richRef.value?.querySelectorAll("a").forEach((link) => {
+ link.removeEventListener("click", sendAnalyticsEvent)
+ })
+ })
+
return {
+ richRef,
+
tabs,
getAttributionMarkup,
diff --git a/frontend/src/components/VMediaInfo/VMediaLicense.vue b/frontend/src/components/VMediaInfo/VMediaLicense.vue
index d452407be..2f07618a9 100644
--- a/frontend/src/components/VMediaInfo/VMediaLicense.vue
+++ b/frontend/src/components/VMediaInfo/VMediaLicense.vue
@@ -11,7 +11,11 @@
class="mb-2 block text-sm md:mb-4 md:text-base"
>
<template #link>
- <VLink :href="licenseUrl">
+ <VLink
+ :href="licenseUrl"
+ :send-external-link-click-event="false"
+ @click="sendVisitLicensePage"
+ >
{{ fullLicenseName }}
</VLink>
</template>
@@ -30,6 +34,8 @@
<VLink
:aria-label="$t('media-details.aria.attribution.tool')"
:href="licenseUrl"
+ :send-external-link-click-event="false"
+ @click="sendVisitLicensePage"
>{{ $t("media-details.reuse.tool.link") }}</VLink
>
</template>
@@ -43,6 +49,7 @@ import { computed, defineComponent, PropType } from "vue"
import { getFullLicenseName, isLicense as isLicenseFn } from "~/utils/license"
import { useI18n } from "~/composables/use-i18n"
+import { useAnalytics } from "~/composables/use-analytics"
import type { License, LicenseVersion } from "~/constants/license"
@@ -68,6 +75,8 @@ export default defineComponent({
},
setup(props) {
const i18n = useI18n()
+ const { sendCustomEvent } = useAnalytics()
+
const isLicense = computed(() => isLicenseFn(props.license))
const headerText = computed(() => {
const licenseOrTool = isLicense.value ? "license" : "tool"
@@ -76,10 +85,19 @@ export default defineComponent({
const fullLicenseName = computed(() =>
getFullLicenseName(props.license, props.licenseVersion, i18n)
)
+
+ const sendVisitLicensePage = () => {
+ sendCustomEvent("VISIT_LICENSE_PAGE", {
+ license: props.license,
+ })
+ }
+
return {
isLicense,
headerText,
fullLicenseName,
+
+ sendVisitLicensePage,
}
},
})
diff --git a/frontend/src/pages/image/_id/index.vue b/frontend/src/pages/image/_id/index.vue
index 325c1c6e6..ceb56928e 100644
--- a/frontend/src/pages/image/_id/index.vue
+++ b/frontend/src/pages/image/_id/index.vue
@@ -38,6 +38,7 @@
:external-icon-size="6"
has-icon-end
size="large"
+ :send-external-link-click-event="false"
@click="sendGetMediaEvent"
>
{{ $t("image-details.weblink") }}
@@ -56,6 +57,8 @@
})
"
:href="image.creator_url"
+ :send-external-link-click-event="false"
+ @click="sendVisitCreatorLinkEvent"
>{{ image.creator }}</VLink
>
<span v-else>{{ image.creator }}</span>
@@ -222,6 +225,16 @@ export default defineComponent({
})
}
+ const sendVisitCreatorLinkEvent = () => {
+ if (!image.value) {
+ return
+ }
+ sendCustomEvent("VISIT_CREATOR_LINK", {
+ id: image.value.id,
+ url: image.value.creator_url,
+ })
+ }
+
return {
image,
hasRelatedMedia,
@@ -239,6 +252,7 @@ export default defineComponent({
backToSearchPath,
sendGetMediaEvent,
+ sendVisitCreatorLinkEvent,
}
},
async asyncData({ app, error, route, $pinia }) {
diff --git a/frontend/src/types/analytics.ts b/frontend/src/types/analytics.ts
index 8a846497d..782b2a3d1 100644
--- a/frontend/src/types/analytics.ts
+++ b/frontend/src/types/analytics.ts
@@ -139,6 +139,38 @@ export type Events = {
/** The name of the Vue component used to switch content types. */
component: string
}
+ /**
+ * Description: The user clicks to a link outside of Openverse.
+ * Questions:
+ * - What types of external content do users seek?
+ * - Are there external resources we should make more visible?
+ * - Is there content we might want to add to Openverse directly?
+ */
+ EXTERNAL_LINK_CLICK: {
+ /** The url of the external link */
+ url: string
+ }
+ /**
+ * Description: The user visits a creator's link in the single result UI
+ * Questions:
+ * - Are creator links clicked much? Does Openverse increase visibility
+ * of included creator's profiles?
+ */
+ VISIT_CREATOR_LINK: {
+ /** The unique ID of the media */
+ id: string
+ /** The permalink to the creator's profile */
+ url: string
+ }
+ /**
+ * Description: The user visits a CC license description page on CC.org
+ * Questions:
+ * - How often are external licenses viewed?
+ */
+ VISIT_LICENSE_PAGE: {
+ /** The slug of the license the user clicked on */
+ license: 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 cae7c2622..846a29b0e 100644
--- a/frontend/test/unit/specs/components/v-content-link.spec.js
+++ b/frontend/test/unit/specs/components/v-content-link.spec.js
@@ -12,6 +12,11 @@ jest.mock("~/composables/use-analytics", () => ({
describe("VContentLink", () => {
let options = {}
+ const sendCustomEventMock = jest.fn()
+
+ useAnalytics.mockImplementation(() => ({
+ sendCustomEvent: sendCustomEventMock,
+ }))
beforeEach(() => {
options = {
@@ -33,11 +38,6 @@ describe("VContentLink", () => {
})
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")
diff --git a/frontend/test/unit/specs/components/v-link.spec.js b/frontend/test/unit/specs/components/v-link.spec.js
index 843aff1ef..c3d554e26 100644
--- a/frontend/test/unit/specs/components/v-link.spec.js
+++ b/frontend/test/unit/specs/components/v-link.spec.js
@@ -3,9 +3,18 @@ import Vue from "vue"
import { render } from "~~/test/unit/test-utils/render"
+import { useAnalytics } from "~/composables/use-analytics"
+
import VLink from "~/components/VLink.vue"
+jest.mock("~/composables/use-analytics", () => ({
+ useAnalytics: jest.fn(),
+}))
+
describe("VLink", () => {
+ useAnalytics.mockImplementation(() => ({
+ sendCustomEvent: jest.fn(),
+ }))
it.each`
href | target | rel
${"/about"} | ${null} | ${null}
@@ -42,7 +51,10 @@ describe("VLink", () => {
this.text = "Code is Poetry"
},
},
- template: `<div><VLink href="${href}" @click="handleClick">{{ text }}</VLink></div>`,
+ template: `
+ <div>
+ <VLink href="${href}" @click="handleClick">{{ text }}</VLink>
+ </div>`,
})
const WrapperComponent = createVLinkWrapper(href)
render(WrapperComponent, {}, (localVue) => {
@@ -56,3 +68,39 @@ describe("VLink", () => {
expect(linkAfter.tagName).toBe("A")
})
})
+
+describe("VLink custom events", () => {
+ const sendCustomEventMock = jest.fn()
+ const href = "https://example.com"
+ useAnalytics.mockImplementation(() => ({
+ sendCustomEvent: sendCustomEventMock,
+ }))
+
+ afterEach(() => {
+ sendCustomEventMock.mockReset()
+ })
+
+ it("External link sends a custom event", async () => {
+ render(VLink, {
+ props: { href },
+ slots: { default: "Code is Poetry" },
+ })
+ const link = screen.getByRole("link")
+ await fireEvent.click(link)
+
+ expect(sendCustomEventMock).toHaveBeenCalledWith("EXTERNAL_LINK_CLICK", {
+ url: href,
+ })
+ })
+
+ it("External link does not send a custom event if prop is false", async () => {
+ render(VLink, {
+ props: { href: "https://example.com", sendExternalLinkClickEvent: false },
+ slots: { default: "Code is Poetry" },
+ })
+ const link = screen.getByRole("link")
+ await fireEvent.click(link)
+
+ expect(sendCustomEventMock).not.toHaveBeenCalled()
+ })
+})