Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Analytics event: COPY_ATTRIBUTION (#1147)
Olga Bulatcf2564897a40
diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.tsindex 06a5cf534..8f92b614c 100644--- a/frontend/nuxt.config.ts+++ b/frontend/nuxt.config.ts@@ -104,6 +104,7 @@ const openverseLocales = [].filter((l) => Boolean(l.iso)) as LocaleObject[]const port = process.env.PORT || 8443+const isProdNotPlaywright = isProd && !(process.env.PW === "true")const config: NuxtConfig = {// eslint-disable-next-line no-undef@@ -292,21 +293,23 @@ const config: NuxtConfig = {proxy: {// The key is appended to the address in the value."/api/event":- process.env.PLAUSIBLE_ORIGIN ?? isProd- ? "https://plausible.io"- : "http://localhost:50288",+ process.env.PLAUSIBLE_ORIGIN ??+ (isProdNotPlaywright ? "https://plausible.io" : "http://localhost:50288"),},plausible: {- trackLocalhost: !isProd,+ trackLocalhost: !isProdNotPlaywright,},publicRuntimeConfig: {plausible: {// This is the current domain of the site.- domain: process.env.SITE_DOMAIN ?? isProd ? "openverse.org" : "localhost",+ domain:+ process.env.SITE_DOMAIN ??+ (isProdNotPlaywright ? "openverse.org" : "localhost"),apiHost:- process.env.SITE_DOMAIN ?? isProd+ process.env.SITE_DOMAIN ??+ (isProdNotPlaywright? "https://openverse.org"- : `http://localhost:${port}`,+ : `http://localhost:${port}`),},},}diff --git a/frontend/src/components/VCopyButton.vue b/frontend/src/components/VCopyButton.vueindex 7dc302d3a..c6547a93c 100644--- a/frontend/src/components/VCopyButton.vue+++ b/frontend/src/components/VCopyButton.vue@@ -50,7 +50,7 @@ export default defineComponent({const onCopySuccess = (e: Clipboard.Event) => {success.value = true- emit("copied", { content: e.text })+ emit("copied")setTimeout(() => {success.value = falsediff --git a/frontend/src/components/VMediaInfo/VCopyLicense.vue b/frontend/src/components/VMediaInfo/VCopyLicense.vueindex 4c06e6efc..8f3b4a3df 100644--- a/frontend/src/components/VMediaInfo/VCopyLicense.vue+++ b/frontend/src/components/VMediaInfo/VCopyLicense.vue@@ -10,18 +10,30 @@{{ $t(`media-details.reuse.copy-license.${tab}`) }}</VTab></template>- <VLicenseTabPanel :tab="tabs[0]">+ <VLicenseTabPanel+ :tab="tabs[0]"+ :media-id="media.id"+ :media-type="media.frontendMediaType"+ ><!-- 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 })" /><!-- eslint-enable vue/no-v-html --></VLicenseTabPanel>- <VLicenseTabPanel :tab="tabs[1]">+ <VLicenseTabPanel+ :tab="tabs[1]"+ :media-id="media.id"+ :media-type="media.frontendMediaType"+ ><p id="attribution-html" class="break-all font-mono" dir="ltr">{{ getAttributionMarkup() }}</p></VLicenseTabPanel>- <VLicenseTabPanel :tab="tabs[2]">+ <VLicenseTabPanel+ :tab="tabs[2]"+ :media-id="media.id"+ :media-type="media.frontendMediaType"+ >{{ getAttributionMarkup({ isPlaintext: true }) }}</VLicenseTabPanel></VTabs>@@ -39,7 +51,7 @@ import VTabs from "~/components/VTabs/VTabs.vue"import VTab from "~/components/VTabs/VTab.vue"import VLicenseTabPanel from "~/components/VMediaInfo/VLicenseTabPanel.vue"-const tabs = ["rich", "html", "plain"]+const tabs = ["rich", "html", "plain"] as constexport default defineComponent({name: "VCopyLicense",diff --git a/frontend/src/components/VMediaInfo/VLicenseTabPanel.vue b/frontend/src/components/VMediaInfo/VLicenseTabPanel.vueindex f7c01f5c8..fd4a9771e 100644--- a/frontend/src/components/VMediaInfo/VLicenseTabPanel.vue+++ b/frontend/src/components/VMediaInfo/VLicenseTabPanel.vue@@ -10,12 +10,17 @@:id="`copyattr-${tab}`":el="`#panel-slot-${tab}`"class="mt-6"+ @copied="handleCopy"/></VTabPanel></template><script lang="ts">-import { defineComponent } from "vue"+import { defineComponent, PropType } from "vue"++import type { MediaType } from "~/constants/media"++import { useAnalytics } from "~/composables/use-analytics"import VCopyButton from "~/components/VCopyButton.vue"import VTabPanel from "~/components/VTabs/VTabPanel.vue"@@ -27,10 +32,42 @@ export default defineComponent({VTabPanel,},props: {+ /**+ * The kind of attribution shown in the tab.+ */tab: {+ type: String as PropType<"rich" | "html" | "plain">,+ required: true,+ },+ /**+ * The ID of the media for which the attribution is generated.+ * Used for analytics.+ */+ mediaId: {type: String,required: true,},+ /**+ * The media type of the media for which the attribution is generated.+ * Used for analytics.+ */+ mediaType: {+ type: String as PropType<MediaType>,+ required: true,+ },+ },+ setup(props) {+ const { sendCustomEvent } = useAnalytics()+ const handleCopy = () => {+ sendCustomEvent("COPY_ATTRIBUTION", {+ id: props.mediaId,+ format: props.tab,+ mediaType: props.mediaType,+ })+ }+ return {+ handleCopy,+ }},})</script>diff --git a/frontend/src/types/analytics.ts b/frontend/src/types/analytics.tsindex 558507569..b10525878 100644--- a/frontend/src/types/analytics.ts+++ b/frontend/src/types/analytics.ts@@ -38,6 +38,20 @@ export type Events = {/** The media type being searched */mediaType: MediaType}+ /**+ * Description: The user clicks one of the buttons to copy the media attribution+ * Questions:+ * - How often do users use our attribution tool?+ * - Which format is the most popular?+ */+ COPY_ATTRIBUTION: {+ /** The unique ID of the media */+ id: string+ /** The format of the copied attribution */+ format: "plain" | "rich" | "html"+ /** The media type being searched */+ mediaType: MediaType+ }}/**diff --git a/frontend/test/playwright/e2e/attribution.spec.ts b/frontend/test/playwright/e2e/attribution.spec.tsindex 60caab613..9ac92abcc 100644--- a/frontend/test/playwright/e2e/attribution.spec.ts+++ b/frontend/test/playwright/e2e/attribution.spec.ts@@ -44,3 +44,29 @@ test("can copy plain text attribution", async ({ page }) => {// Only the plain-text license contains the "To view" bit.expect(clippedText).toContain("To view a copy of this license")})++test("sends analytics event on copy", async ({ page }) => {+ let copyAttributionEventData: {+ id: string+ format: string+ mediaType: string+ } = { id: "", format: "", mediaType: "" }+ page.on("request", (req) => {+ if (req.method() === "POST") {+ const requestData = req.postDataJSON()+ if (requestData?.n == "COPY_ATTRIBUTION") {+ copyAttributionEventData = JSON.parse(requestData?.p)+ }+ }+ })+ const mediaType = "image"+ const id = "e9d97a98-621b-4ec2-bf70-f47a74380452"+ const format = "rich"++ await page.goto(`${mediaType}/${id}?ff_analytics=on`)+ await page.click(`[id="copyattr-${format}"]`)++ expect(copyAttributionEventData.id).toEqual(id)+ expect(copyAttributionEventData.format).toEqual(format)+ expect(copyAttributionEventData.mediaType).toEqual(mediaType)+})diff --git a/frontend/test/playwright/playwright.config.ts b/frontend/test/playwright/playwright.config.tsindex f175582d8..8b0d43770 100644--- a/frontend/test/playwright/playwright.config.ts+++ b/frontend/test/playwright/playwright.config.ts@@ -36,6 +36,7 @@ const config: PlaywrightTestConfig = {env: {API_URL: "http://localhost:49153/",UPDATE_TAPES: UPDATE_TAPES,+ PW: "true",},},use: {