diff --git a/src/app/analytics.ts b/src/app/analytics.ts new file mode 100644 index 0000000..e7385ac --- /dev/null +++ b/src/app/analytics.ts @@ -0,0 +1,17 @@ +import {page} from "$app/stores" + +const w = window as any + +w.plausible = + w.plausible || + function () { + ;(w.plausible.q = w.plausible.q || []).push(arguments) + } + +export const setupAnalytics = () => { + page.subscribe($page => { + if ($page.route) { + w.plausible("pageview", {u: $page.route.id}) + } + }) +} diff --git a/src/app/tracking.ts b/src/app/tracking.ts new file mode 100644 index 0000000..dfd829a --- /dev/null +++ b/src/app/tracking.ts @@ -0,0 +1,32 @@ +import Bugsnag from "@bugsnag/js" + +export const setupTracking = () => { + if (!import.meta.env.VITE_BUGSNAG_API_KEY) return + + // Initialize + Bugsnag.start({ + apiKey: import.meta.env.VITE_BUGSNAG_API_KEY, + collectUserIp: false, + }) + + // Redact long strings, especially hex and bech32 keys which are 64 and 63 + // characters long, respectively. Put the threshold a little lower in case + // someone accidentally enters a key with the last few digits missing + const redactErrorInfo = (info: any) => + JSON.parse( + JSON.stringify(info || null) + .replace(/\d+:{60}\w+:\w+/g, "[REDACTED]") + .replace(/\w{60}\w+/g, "[REDACTED]"), + ) + + Bugsnag.addOnError((event: any) => { + // Redact individual properties since the event needs to be + // mutated, and we don't want to lose the prototype + event.context = redactErrorInfo(event.context) + event.request = redactErrorInfo(event.request) + event.exceptions = redactErrorInfo(event.exceptions) + event.breadcrumbs = redactErrorInfo(event.breadcrumbs) + + return true + }) +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 4f5569e..32ef2c5 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,10 +1,8 @@