Skip to content

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

Stop blocking on analytics requests (#715)

sarayourfrienddcd1f75a63fa
diff --git a/nuxt.config.js b/nuxt.config.js
index a9a9d2205..b698100fe 100644
--- a/nuxt.config.js
+++ b/nuxt.config.js
@@ -3,6 +3,7 @@ import locales from './src/locales/scripts/valid-locales.json'
import { VIEWPORTS } from './src/constants/screens'
import { dev } from './src/utils/dev'
import { env } from './src/utils/env'
+import { sentry } from './src/utils/sentry-config'
/**
* The default metadata for the site. Can be extended and/or overwritten per page. And even in components!
@@ -195,14 +196,7 @@ export default {
* {@link https://github.com/nuxt-community/redirect-module#usage}
*/
redirect: [{ from: '^/photos/(.*)$', to: '/image/$1', statusCode: 301 }],
- sentry: {
- dsn:
- process.env.SENTRY_DSN ||
- 'https://[email protected]/5799642',
- disabled: process.env.NODE_ENV === 'development',
- environment: process.env.NODE_ENV,
- lazy: true,
- },
+ sentry,
build: {
postcss: {
plugins: {
diff --git a/src/data/usage-data-service.js b/src/data/usage-data-service.js
index e75ee0f48..43b36aba2 100644
--- a/src/data/usage-data-service.js
+++ b/src/data/usage-data-service.js
@@ -8,7 +8,7 @@ const ApiService = createApiService(baseUrl.replace('/v1', ''))
const UsageDataService = {
post(endpoint, params) {
- return ApiService.post(`/analytics/${endpoint}`, params)
+ return ApiService.post(`analytics/${endpoint}`, params)
},
sendSearchQueryEvent({ query, sessionId }) {
diff --git a/src/store/usage-data.js b/src/store/usage-data.js
index 78d6d67a4..a1ba17049 100644
--- a/src/store/usage-data.js
+++ b/src/store/usage-data.js
@@ -6,45 +6,61 @@ import {
} from '~/constants/usage-data-analytics-types'
import stringToBoolean from '~/utils/string-to-boolean'
import UsageDataService from '~/data/usage-data-service'
+import { sentry as sentryConfig } from '~/utils/sentry-config'
const disabled = !stringToBoolean(process.env.enableInternalAnalytics)
/**
* Catch api event failures, mainly to prevent SSR errors on test domains that cannot communicate with our API server.
* Alternatively, just disable internal analytics on those servers with ENABLE_INTERNAL_ANALYTICS=true in the environment.
+ * @param {string} eventName
+ * @param {import('vue').default} context
*/
-const handleUsageEvent = (eventName) => (promise) =>
- promise.catch((error) =>
- console.error({
- message: `Failed to execute api event: ${eventName}.`,
- error,
- })
+const handleUsageEvent = (eventName, context) => (promise) =>
+ promise.catch(
+ (error) =>
+ // $sentryReady won't exist if sentry has been disabled
+ !sentryConfig.disabled &&
+ context.$sentryReady().then((sentry) =>
+ sentry.captureException(error, (scope) => {
+ scope.setTag('event_name', eventName)
+ scope.setTag('request_url', error.config.url)
+ })
+ )
)
+/**
+ *
+ * @param {typeof UsageDataService} usageDataService
+ */
export const createActions = (usageDataService) => ({
- async [SEND_SEARCH_QUERY_EVENT](context, params) {
+ [SEND_SEARCH_QUERY_EVENT](context, params) {
if (disabled) return
- await handleUsageEvent(SEND_SEARCH_QUERY_EVENT)(
- usageDataService.sendSearchQueryEvent(params)
- )
+ handleUsageEvent(
+ SEND_SEARCH_QUERY_EVENT,
+ this
+ )(usageDataService.sendSearchQueryEvent(params))
},
async [SEND_RESULT_CLICKED_EVENT](context, params) {
if (disabled) return
- await handleUsageEvent(SEND_RESULT_CLICKED_EVENT)(
- usageDataService.sendResultClickedEvent(params)
- )
+ handleUsageEvent(
+ SEND_RESULT_CLICKED_EVENT,
+ this
+ )(usageDataService.sendResultClickedEvent(params))
},
- async [SEND_SEARCH_RATING_EVENT](context, params) {
+ [SEND_SEARCH_RATING_EVENT](context, params) {
if (disabled) return
- await handleUsageEvent(SEND_SEARCH_RATING_EVENT)(
- usageDataService.sendSearchRatingEvent(params)
- )
+ handleUsageEvent(
+ SEND_SEARCH_RATING_EVENT,
+ this
+ )(usageDataService.sendSearchRatingEvent(params))
},
- async [SEND_DETAIL_PAGE_EVENT](context, params) {
+ [SEND_DETAIL_PAGE_EVENT](context, params) {
if (disabled) return
- await handleUsageEvent(SEND_DETAIL_PAGE_EVENT)(
- usageDataService.sendDetailPageEvent(params)
- )
+ handleUsageEvent(
+ SEND_DETAIL_PAGE_EVENT,
+ this
+ )(usageDataService.sendDetailPageEvent(params))
},
})
diff --git a/src/utils/sentry-config.js b/src/utils/sentry-config.js
new file mode 100644
index 000000000..2512b7d80
--- /dev/null
+++ b/src/utils/sentry-config.js
@@ -0,0 +1,8 @@
+export const sentry = {
+ dsn:
+ process.env.SENTRY_DSN ||
+ 'https://[email protected]/5799642',
+ disabled: process.env.NODE_ENV === 'development',
+ environment: process.env.NODE_ENV,
+ lazy: true,
+}