Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BACK_TO_TOP event and fire when button is clicked #4953

Merged
merged 15 commits into from
Oct 8, 2024

Conversation

d3jawu
Copy link
Contributor

@d3jawu d3jawu commented Sep 17, 2024

Fixes

Resolves #2276 by @fcoveram.

Fixes #4948 by @d3jawu.

Description

Adds a new analytics event, BACK_TO_TOP, and fires it when the "back to top" button in the bottom-right corner is clicked.

Fixes the behavior of the "back to top" button, which had stopped working on the search page. Verified that the behavior still works on other pages.

Testing Instructions

  1. Run a search.
  2. Click the "back to top" caret button in the bottom-right corner.
  3. See that an analytics event for BACK_TO_TOP is logged.
  4. See that the page scrolls back to the top instead of remaining in place.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (ov just catalog/generate-docs for catalog
    PRs) or the media properties generator (ov just catalog/generate-docs media-props
    for the catalog or ov just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@openverse-bot openverse-bot added 🧱 stack: frontend Related to the Nuxt frontend 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work 🟩 priority: low Low priority and doesn't need to be rushed 🌟 goal: addition Addition of new feature 💻 aspect: code Concerns the software code in the repository labels Sep 17, 2024
@d3jawu d3jawu mentioned this pull request Sep 17, 2024
8 tasks
@d3jawu d3jawu marked this pull request as ready for review September 17, 2024 23:59
@d3jawu d3jawu requested a review from a team as a code owner September 17, 2024 23:59
@obulat obulat removed the 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work label Sep 18, 2024
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start ✨

The scroll button is on all scrollable pages, including such content pages as https://openverse.org/about, so we should not send the event when we are not on a search or a collection page.

To check the page we are on, we can use useRoute, and check if the route.name is one of search__image, search__audio, search, image__collection, audio_collection. Note that the route names are usually Symbols, so you would need to use .toString() on them, and then also remove the language suffix such as __en.

Also, this is not mentioned in the issue, but I think the params should be similar to the once sent by REACH_RESULT_END:

REACH_RESULT_END: {

Please let me know if anything is unclear. If you think this is too much work, we can remove the events from this PR, and just ship the fix to scrolling on the search pages.

frontend/src/components/VScrollButton.vue Outdated Show resolved Hide resolved
@d3jawu
Copy link
Contributor Author

d3jawu commented Sep 19, 2024

I think the params should be similar to the once sent by REACH_RESULT_END:

Hi @obulat what exactly do you mean by this? Should they match them exactly, or include them in addition to the ones listed in the issue?

@obulat
Copy link
Contributor

obulat commented Sep 20, 2024

I think the params should be similar to the once sent by REACH_RESULT_END:

Hi @obulat what exactly do you mean by this? Should they match them exactly, or include them in addition to the ones listed in the issue?

It's the latter, they should contain

  • searchType: SupportedSearchType
  • kind: ResultKind
  • query: string
  • resultPage: number
  • collectionType: Collection | "null"
  • collectionValue: string | "null"

and

  • page
  • scrollPixels
  • maxScroll

Here's the way we get those properties for REACH_RESULT_END event:

const eventPayload = computed(() => {
let kind: ResultKind =
searchStore.strategy === "default" ? "search" : "collection"
const collectionType = (searchStore.collectionValue as Collection) ?? "null"
return {
searchType: props.searchType,
query: props.searchTerm,
resultPage: currentPage.value || 1,
kind,
collectionType,
collectionValue: searchStore.collectionValue ?? "null",
}
})

Plausible does not allow us to send undefined as the value for a parameter. The values here could be non-number. Maybe we should use -1 instead of undefined, @WordPress/openverse-frontend ?

@d3jawu
Copy link
Contributor Author

d3jawu commented Sep 20, 2024

A couple little things:

  • I've used -1 instead of undefined for the numeric values.
  • For unsupported search types (video and model-3d) I've defaulted to all.

Let me know if you'd rather I use other values!

@d3jawu d3jawu requested a review from obulat September 20, 2024 20:53
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for the search pages, but needs to be updated for other pages.
I have several suggestions:

  1. I did not give the correct route names in the previous suggestions. They should be fixed.
  2. The search pages scroll the main page, but all others scroll the body. This means that the code for scrollPixels and maxScroll works correctly only on the search pages.
  3. Since the scroll values are calculated after the page was scrolled to top, they are set to 0 (or -1, because of the || -1 condition).
  4. It's better to get the search type from the mediaStore._searchType, which filters out the unsupported types.

Here's a patch with suggestions on how to fix these problems:

Subject: [PATCH] Fix search type, route names and scroll values
---
Index: frontend/nuxt.config.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.ts
--- a/frontend/nuxt.config.ts	(revision 431ccc43aaa8f58cacf18170c8f5cbddde48cbbd)
+++ b/frontend/nuxt.config.ts	(date 1727112312513)
@@ -42,7 +42,7 @@
       },
       plausible: {
         ignoredHostnames: ["localhost", "staging.openverse.org"],
-        logIgnoredEvents: true,
+        logIgnoredEvents: false,
         apiHost: "http://localhost:50290",
         domain: "localhost",
       },
Index: frontend/src/components/VScrollButton.vue
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/frontend/src/components/VScrollButton.vue b/frontend/src/components/VScrollButton.vue
--- a/frontend/src/components/VScrollButton.vue	(revision 431ccc43aaa8f58cacf18170c8f5cbddde48cbbd)
+++ b/frontend/src/components/VScrollButton.vue	(date 1727116542815)
@@ -24,45 +24,44 @@
 
 defineEmits<{ tab: [KeyboardEvent] }>()
 
-const ANALYTICS_ROUTES = [
-  "search__image",
-  "search__audio",
-  "search",
-  "image__collection",
-  "audio_collection",
-]
+const SEARCH_ROUTES = ["search-image", "search-audio", "search"]
+const ANALYTICS_ROUTES = [...SEARCH_ROUTES, "image-collection", "audio-collection"]
 
 const hClass = computed(() =>
   props.isFilterSidebarVisible ? positionWithSidebar : positionWithoutSidebar
 )
 const scrollToTop = () => {
+  const routeName = route.name?.toString().split("__")[0]
+
+  if (!routeName || !ANALYTICS_ROUTES.includes(routeName)) {
+    window.scrollTo({ top: 0, left: 0, behavior: "smooth" })
+    return
+  }
+
+  const isSearchRoute = routeName && SEARCH_ROUTES.includes(routeName)
+
   const mainPage = document.getElementById("main-page")
-  const element = mainPage || window
+
+  const scrollPixels = isSearchRoute ? mainPage?.scrollTop : window.scrollY
+  const maxScroll = isSearchRoute ? mainPage?.scrollHeight : document.body.scrollHeight
+
+  const element = isSearchRoute ? mainPage || window : window
   element.scrollTo({ top: 0, left: 0, behavior: "smooth" })
 
-  if (ANALYTICS_ROUTES.some((r) => route.name?.toString().startsWith(r))) {
     const kind: ResultKind =
       searchStore.strategy === "default" ? "search" : "collection"
     const collectionType = (searchStore.collectionValue as Collection) ?? "null"
 
     $sendCustomEvent("BACK_TO_TOP", {
-      searchType:
-        searchStore.searchType === "model-3d" ||
-        searchStore.searchType === "video"
-          ? "all"
-          : searchStore.searchType,
+      searchType: mediaStore._searchType,
       kind,
       query: searchStore.searchTerm,
       resultPage: mediaStore.currentPage,
-      scrollPixels: mainPage?.scrollTop || -1,
-      maxScroll:
-        mainPage && "scrollTopMax" in mainPage
-          ? (mainPage.scrollTopMax as number)
-          : -1,
+      scrollPixels: scrollPixels ?? -1,
+      maxScroll: maxScroll ?? -1,
       collectionType,
       collectionValue: searchStore.collectionValue ?? "null",
     })
-  }
 }
 </script>

@openverse-bot
Copy link
Collaborator

Based on the contributor urgency of this PR, the following reviewers are being gently reminded to review this PR:

@dhruvkb
This reminder is being automatically generated due to the urgency configuration.

Excluding weekend1 days, this PR was ready for review 5 day(s) ago. PRs labelled with contributor urgency are expected to be reviewed within 3 weekday(s)2.

@d3jawu, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings.

Footnotes

  1. Specifically, Saturday and Sunday.

  2. For the purpose of these reminders we treat Monday - Friday as weekdays. Please note that the operation that generates these reminders runs at midnight UTC on Monday - Friday. This means that depending on your timezone, you may be pinged outside of the expected range.

@zackkrida zackkrida marked this pull request as draft September 25, 2024 00:14
@obulat obulat marked this pull request as ready for review October 8, 2024 14:50
@obulat obulat self-requested a review October 8, 2024 14:50
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the suggested patch and also extracted the common properties for search-related analytics events.

Thank you for your contribution, @d3jawu !

@obulat obulat merged commit f7a7cce into WordPress:main Oct 8, 2024
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🌟 goal: addition Addition of new feature 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: frontend Related to the Nuxt frontend
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Button to return to top not working in search page Analytics event: BACK_TO_TOP
4 participants