Valentyne Stigloher b7e72cfcb4 (plausible) simplify redaction code and strip hashes
instances for hashes which show up in plausible include #google_vignette (from ads), pronouns after profile or search filters in lists
2024-03-18 14:04:38 +01:00

33 lines
950 B
TypeScript

import * as Sentry from '@sentry/vue';
import type { Plugin } from '@nuxt/types';
const USER_AT = /^\/@.+/;
const USER_SUBPAGE = /^\/(u|card)\/.*/;
export const normalizeUrl = (page: URL): URL => {
if (USER_AT.test(page.pathname)) {
page.pathname = page.pathname.replace(USER_AT, '/@--redacted--');
}
if (USER_SUBPAGE.test(page.pathname)) {
page.pathname = page.pathname.replace(USER_SUBPAGE, '/$1/--redacted--');
}
page.hash = '';
return page;
};
const plugin: Plugin = ({ app }) => {
app.router?.afterEach((to) => {
try {
const url = normalizeUrl(new URL(to.fullPath, window.location.href));
console.debug('[analytics] tracking page view:', url.toString());
app.$plausible.trackPageview({
url: url.toString(),
});
} catch (error) {
Sentry.captureException(error);
}
});
};
export default plugin;