diff --git a/middleware/analytics.js b/middleware/analytics.js index fee859f87..24456bc94 100644 --- a/middleware/analytics.js +++ b/middleware/analytics.js @@ -1,5 +1,9 @@ export default function (context) { - if (process.client && context.from.path === context.route.path) { + if (!process.client) { + return + } + + if (context.from.path === context.route.path) { return } @@ -9,19 +13,9 @@ export default function (context) { setTimeout(() => { context.$axios - .post( - `${context.$config.analytics.base_url}view`, - { - url: process.env.domain + context.route.fullPath, - }, - context.$config.analytics.admin_key - ? { - headers: { - 'Modrinth-Admin': context.$config.analytics.admin_key, - }, - } - : {} - ) + .post(`${context.$config.analytics.base_url}view`, { + url: process.env.domain + context.route.fullPath, + }) .then(() => {}) .catch((e) => { console.error('An error occurred while registering the visit: ', e) diff --git a/nuxt.config.js b/nuxt.config.js index f88ff29f8..1ca3a6c44 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -1,4 +1,5 @@ import { sortRoutes } from '@nuxt/utils' +import axios from 'axios' export default { /* @@ -305,10 +306,35 @@ export default { }, }, }, - analytics: { - base_url: - process.env.ARIADNE_URL || 'https://staging-ariadne.modrinth.com/v1/', - admin_key: process.env.ARIADNE_ADMIN_KEY, + }, + hooks: { + render: { + routeDone(url) { + setTimeout(() => { + axios + .post( + `${ + process.env.ARIADNE_URL || + 'https://staging-ariadne.modrinth.com/v1/' + }view`, + { + url: process.env.domain + url, + }, + { + headers: { + 'Modrinth-Admin': process.env.ARIADNE_ADMIN_KEY || 'feedbeef', + }, + } + ) + .then(() => {}) + .catch((e) => { + console.error( + 'An error occurred while registering the visit: ', + e + ) + }) + }) + }, }, }, }