Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Analytics event: SELECT_EXTERNAL_SOURCE (#1158)
Olga Bulat665b2aad1f66
diff --git a/frontend/package.json b/frontend/package.jsonindex b7e0260c9..326d2f03d 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -120,7 +120,7 @@"@playwright/test": "1.30.0","@testing-library/dom": "^8.13.0","@testing-library/jest-dom": "^5.16.1",- "@testing-library/user-event": "^13.5.0",+ "@testing-library/user-event": "^14.0.0","@testing-library/vue": "^5.8.2","@types/express-useragent": "^1.0.2","@types/jest": "^26.0.22",diff --git a/frontend/src/components/VErrorSection/VNoResults.vue b/frontend/src/components/VErrorSection/VNoResults.vueindex d64a59ef4..99d7dc421 100644--- a/frontend/src/components/VErrorSection/VNoResults.vue+++ b/frontend/src/components/VErrorSection/VNoResults.vue@@ -14,10 +14,12 @@as="VLink":href="source.url"variant="secondary-bordered"- class="label-bold justify-between text-dark-charcoal md:justify-start md:gap-x-2"+ class="label-bold !w-full justify-between text-dark-charcoal md:!w-auto md:justify-start md:gap-x-2"+ show-external-icon+ :external-icon-size="4"+ @mousedown="handleClick(source.name)">{{ source.name }}- <VIcon name="external-link" :size="4" rtl-flip /></VButton></div></div>@@ -26,23 +28,54 @@<script lang="ts">import { defineComponent, PropType } from "vue"+import { useAnalytics } from "~/composables/use-analytics"+import type { ExternalSource } from "~/types/external-source"+import type { MediaType } from "~/constants/media"+import VButton from "~/components/VButton.vue"-import VIcon from "~/components/VIcon/VIcon.vue"export default defineComponent({name: "VNoResults",- components: { VIcon, VButton },+ components: { VButton },props: {+ /**+ * The list of external sources information: their name and url.+ */externalSources: {type: Array as PropType<ExternalSource[]>,required: true,},+ /**+ * The media type to use as the criteria for filtering additional sources+ */+ mediaType: {+ type: String as PropType<MediaType>,+ required: true,+ },+ /**+ * The search term for which the external sources links are generated.+ */searchTerm: {type: String,required: true,},},+ setup(props) {+ const { sendCustomEvent } = useAnalytics()+ const handleClick = (sourceName: string) => {+ sendCustomEvent("SELECT_EXTERNAL_SOURCE", {+ name: sourceName,+ mediaType: props.mediaType,+ query: props.searchTerm,+ component: "VNoResults",+ })+ }++ return {+ handleClick,+ }+ },})</script>diff --git a/frontend/src/components/VExternalSearch/VExternalSearchForm.vue b/frontend/src/components/VExternalSearch/VExternalSearchForm.vueindex 5e4eb6670..8414fd454 100644--- a/frontend/src/components/VExternalSearch/VExternalSearchForm.vue+++ b/frontend/src/components/VExternalSearch/VExternalSearchForm.vue@@ -52,35 +52,24 @@/></VButton><template v-if="triggerElement">- <VPopoverContent- v-if="isMd"- id="external-sources-popover"- aria-labelledby="external-sources-button"+ <Component+ :is="isMd ? 'VPopoverContent' : 'VModalContent'"+ :id="isMd ? 'external-sources-popover' : 'external-sources-modal'"+ :aria-labelledby="'external-sources-button'":hide="closeDialog":trigger-element="triggerElement":visible="isVisible"- z-index="popover"+ :z-index="isMd ? 'popover' : 'modal'"+ :variant="isMd ? undefined : 'centered'"><VExternalSourceListclass="flex flex-col":external-sources="externalSources"- @close="closeDialog"- /></VPopoverContent>- <VModalContent- v-else- id="external-sources-modal"- aria-labelledby="external-sources-button"- :trigger-element="triggerElement"- :hide="closeDialog"- :visible="isVisible"- variant="centered"- >- <VExternalSourceList- class="flex-col justify-center"- :external-sources="externalSources"+ :media-type="type"+ :search-term="searchTerm"@close="closeDialog"/>- </VModalContent>+ </Component></template></section></template>diff --git a/frontend/src/components/VExternalSearch/VExternalSourceList.vue b/frontend/src/components/VExternalSearch/VExternalSourceList.vueindex cf226fae1..eb71b185b 100644--- a/frontend/src/components/VExternalSearch/VExternalSourceList.vue+++ b/frontend/src/components/VExternalSearch/VExternalSourceList.vue@@ -17,11 +17,13 @@as="VLink"variant="plain"size="disabled"- class="caption-bold w-full justify-between px-4 py-3 text-dark-charcoal hover:bg-dark-charcoal-10"+ class="caption-bold !w-full justify-between px-4 py-3 text-dark-charcoal hover:bg-dark-charcoal-10"+ show-external-icon+ :external-icon-size="4":href="source.url"+ @mousedown="handleClick(source.name)">{{ source.name }}- <VIcon name="external-link" :size="4" :rtl-flip="true" class="ms-2" /></VButton></div></template>@@ -29,11 +31,13 @@<script lang="ts">import { defineComponent, PropType } from "vue"+import { useAnalytics } from "~/composables/use-analytics"import type { ExternalSource } from "~/types/external-source"+import type { MediaType } from "~/constants/media"+import VButton from "~/components/VButton.vue"import VCloseButton from "~/components/VCloseButton.vue"-import VIcon from "~/components/VIcon/VIcon.vue"/*** This component renders a list of pre-populated links to additional sources@@ -41,15 +45,44 @@ import VIcon from "~/components/VIcon/VIcon.vue"*/export default defineComponent({name: "VExternalSourceList",- components: { VCloseButton, VButton, VIcon },+ components: { VCloseButton, VButton },props: {/**- * the media type to use as the criteria for filtering additional sources+ * The media type to use as the criteria for filtering additional sources+ */+ mediaType: {+ type: String as PropType<MediaType>,+ required: true,+ },+ /**+ * The search term for which the external sources links are generated.+ */+ searchTerm: {+ type: String,+ required: true,+ },+ /**+ * The list of external sources information: their name and url.*/externalSources: {type: Array as PropType<ExternalSource[]>,required: true,},},+ setup(props) {+ const { sendCustomEvent } = useAnalytics()+ const handleClick = (sourceName: string) => {+ sendCustomEvent("SELECT_EXTERNAL_SOURCE", {+ name: sourceName,+ mediaType: props.mediaType,+ query: props.searchTerm,+ component: "VExternalSourceList",+ })+ }++ return {+ handleClick,+ }+ },})</script>diff --git a/frontend/src/components/VSearchGrid.vue b/frontend/src/components/VSearchGrid.vueindex 7f8e65e84..907f05c6f 100644--- a/frontend/src/components/VSearchGrid.vue+++ b/frontend/src/components/VSearchGrid.vue@@ -24,14 +24,17 @@:external-sources="externalSources":search-term="searchTerm":is-supported="supported"- @tab="$emit('tab', $event)"/></section><VErrorSection v-else class="w-full py-10"><template #image><VErrorImage error-code="NO_RESULT" /></template>- <VNoResults :external-sources="externalSources" :search-term="searchTerm" />+ <VNoResults+ :external-sources="externalSources"+ :search-term="searchTerm"+ :media-type="externalSourcesType"+ /></VErrorSection></template>diff --git a/frontend/src/types/analytics.ts b/frontend/src/types/analytics.tsindex 8c6f0f407..ff7f4d633 100644--- a/frontend/src/types/analytics.ts+++ b/frontend/src/types/analytics.ts@@ -85,6 +85,22 @@ export type Events = {/** the reason for the report */reason: ReportReason}+ /**+ * Description: When the user chooses an external source from the dropdown of external sources+ * Questions:+ * - Which external sources are most popular? This could drive inclusion in Openverse.+ * - Are certain media types more popular externally?+ */+ SELECT_EXTERNAL_SOURCE: {+ /** The name of the external source */+ name: string+ /** The media type being searched */+ mediaType: MediaType+ /** The search term */+ query: string+ /** The component that triggered the event */+ component: "VNoResults" | "VExternalSourceList"+ }}/**diff --git a/frontend/test/playwright/e2e/external-sources.spec.ts b/frontend/test/playwright/e2e/external-sources.spec.tsnew file mode 100644index 000000000..9592a7969--- /dev/null+++ b/frontend/test/playwright/e2e/external-sources.spec.ts@@ -0,0 +1,38 @@+import { test, expect } from "@playwright/test"++import { goToSearchTerm, setCookies } from "~~/test/playwright/utils/navigation"++test("sends analytics event on external source click", async ({ page }) => {+ let eventData: {+ name: string+ mediaType: string+ query: string+ } = { name: "", mediaType: "", query: "" }+ page.on("request", (req) => {+ if (req.method() === "POST") {+ const requestData = req.postDataJSON()+ if (requestData?.n == "SELECT_EXTERNAL_SOURCE") {+ eventData = JSON.parse(requestData?.p)+ }+ }+ })+ // Start waiting for new page before clicking.+ const pagePromise = page.context().waitForEvent("page")++ const mediaType = "image"+ const name = "Centre For Ageing Better"+ const query = "cat"++ await setCookies(page.context(), { analytics: "true" })+ await goToSearchTerm(page, "cat", { mode: "SSR", query: "ff_analytics=on" })++ await page.getByRole("button", { name: "Source list" }).click()+ await page.getByRole("link", { name: "Centre for Ageing Better" }).click()++ const newPage = await pagePromise+ await newPage.close()++ expect(eventData.name).toEqual(name)+ expect(eventData.mediaType).toEqual(mediaType)+ expect(eventData.query).toEqual(query)+})diff --git a/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js b/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.jsnew file mode 100644index 000000000..264185c0a--- /dev/null+++ b/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js@@ -0,0 +1,69 @@+import VueI18n from "vue-i18n"++import { createLocalVue } from "@vue/test-utils"+import { render, screen } from "@testing-library/vue"+import userEvent from "@testing-library/user-event"++import { PiniaVuePlugin, createPinia } from "~~/test/unit/test-utils/pinia"+import i18n from "~~/test/unit/test-utils/i18n"++import { useAnalytics } from "~/composables/use-analytics"+import { IMAGE } from "~/constants/media"++import VExternalSourceList from "~/components/VExternalSearch/VExternalSourceList.vue"++jest.mock("~/composables/use-analytics", () => ({+ useAnalytics: jest.fn(),+}))++describe("VExternalSourceList", () => {+ let propsData+ let localVue+ let sendCustomEventMock++ beforeEach(() => {+ localVue = createLocalVue()+ localVue.use(PiniaVuePlugin)+ localVue.use(VueI18n)+ sendCustomEventMock = jest.fn()+ useAnalytics.mockImplementation(() => ({+ sendCustomEvent: sendCustomEventMock,+ }))+ propsData = {+ searchTerm: "cat",+ mediaType: IMAGE,+ externalSources: [+ { name: "source1", url: "https://source1.com/?q=cat" },+ { name: "source2", url: "https://source2.com/?q=cat" },+ ],+ }+ render(VExternalSourceList, {+ localVue,+ pinia: createPinia(),+ i18n,+ props: propsData,+ })+ })++ it("should render external sources links", () => {+ expect(screen.queryAllByRole("link")).toHaveLength(+ propsData.externalSources.length+ )+ })++ it("should send SELECT_EXTERNAL_SOURCE analytics event on CTA button click", async () => {+ const source1Link = screen.getByRole("link", {+ name: propsData.externalSources[0].name,+ })++ const user = userEvent.setup()+ await user.click(source1Link)++ expect(sendCustomEventMock).toHaveBeenCalledWith("SELECT_EXTERNAL_SOURCE", {+ mediaType: IMAGE,+ name: propsData.externalSources[0].name,+ query: propsData.searchTerm,+ component: "VExternalSourceList",+ })+ })+})diff --git a/frontend/test/unit/specs/components/ImageDetails/v-copy-license.spec.js b/frontend/test/unit/specs/components/ImageDetails/v-copy-license.spec.jsindex d7cffb1cb..b07c91447 100644--- a/frontend/test/unit/specs/components/ImageDetails/v-copy-license.spec.js+++ b/frontend/test/unit/specs/components/ImageDetails/v-copy-license.spec.js@@ -9,7 +9,7 @@ describe("VCopyLicense", () => {beforeEach(() => {props = {media: {- id: 0,+ id: "0",title: "foo",provider: "flickr",url: "foo.bar",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 8b2b351ea..a611ddc4a 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -51,7 +51,7 @@ importers:'@tailwindcss/typography': ^0.5.9'@testing-library/dom': ^8.13.0'@testing-library/jest-dom': ^5.16.1- '@testing-library/user-event': ^13.5.0+ '@testing-library/user-event': ^14.0.0'@testing-library/vue': ^5.8.2'@types/express-useragent': ^1.0.2'@types/jest': ^26.0.22@@ -192,7 +192,7 @@ importers:'@playwright/test': 1.30.0'@testing-library/dom': 8.13.0'@testing-library/jest-dom': 5.16.1- '@testing-library/user-event': 13.5.0_tlwynutqiyp5mns3woioasuxnq+ '@testing-library/user-event': 14.4.3_tlwynutqiyp5mns3woioasuxnq'@testing-library/vue': 5.8.2_rhqkolmkwunxzlyyxxsuwaiuri'@types/express-useragent': 1.0.2'@types/jest': 26.0.24@@ -6149,13 +6149,12 @@ packages:redent: 3.0.0dev: true- /@testing-library/user-event/13.5.0_tlwynutqiyp5mns3woioasuxnq:- resolution: {integrity: sha512-5Kwtbo3Y/NowpkbRuSepbyMFkZmHgD+vPzYB/RJ4oxt5Gj/avFFBYjhw27cqSVPVw/3a67NK1PbiIr9k4Gwmdg==}- engines: {node: '>=10', npm: '>=6'}+ /@testing-library/user-event/14.4.3_tlwynutqiyp5mns3woioasuxnq:+ resolution: {integrity: sha512-kCUc5MEwaEMakkO5x7aoD+DLi02ehmEM2QCGWvNqAS1dV/fAvORWEjnjsEIvml59M7Y5kCkWN6fCCyPOe8OL6Q==}+ engines: {node: '>=12', npm: '>=6'}peerDependencies:'@testing-library/dom': '>=7.21.4'dependencies:- '@babel/runtime': 7.18.9'@testing-library/dom': 8.13.0dev: true