Add plausible analytics

This commit is contained in:
Jon Staab
2024-10-25 14:15:35 -07:00
parent 9debd15026
commit a22b9e40e0
3 changed files with 34 additions and 23 deletions

View File

@@ -26,5 +26,6 @@
</head> </head>
<body data-sveltekit-preload-data="hover"> <body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div> <div style="display: contents">%sveltekit.body%</div>
<script defer data-domain="flotilla.social" src="https://plausible.coracle.social/js/script.manual.js"></script>
</body> </body>
</html> </html>

View File

@@ -4,6 +4,7 @@
import {onMount} from "svelte" import {onMount} from "svelte"
import {get} from "svelte/store" import {get} from "svelte/store"
import {dev} from "$app/environment" import {dev} from "$app/environment"
import {page} from "$app/stores"
import {sleep, take, sortBy, ago, now, HOUR} from "@welshman/lib" import {sleep, take, sortBy, ago, now, HOUR} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util" import type {TrustedEvent} from "@welshman/util"
import { import {
@@ -44,7 +45,15 @@
import {loadUserData} from "@app/commands" import {loadUserData} from "@app/commands"
import * as state from "@app/state" import * as state from "@app/state"
const setupBugsnag = () => { const setupErrorTracking = () => {
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 // Redact long strings, especially hex and bech32 keys which are 64 and 63
// characters long, respectively. Put the threshold a little lower in case // characters long, respectively. Put the threshold a little lower in case
// someone accidentally enters a key with the last few digits missing // someone accidentally enters a key with the last few digits missing
@@ -55,18 +64,25 @@
.replace(/\w{60}\w+/g, "[REDACTED]"), .replace(/\w{60}\w+/g, "[REDACTED]"),
) )
// Wait for bugsnag to be started in main Bugsnag.addOnError((event: any) => {
setTimeout(() => { // Redact individual properties since the event needs to be
Bugsnag.addOnError((event: any) => { // mutated, and we don't want to lose the prototype
// Redact individual properties since the event needs to be event.context = redactErrorInfo(event.context)
// mutated, and we don't want to lose the prototype event.request = redactErrorInfo(event.request)
event.context = redactErrorInfo(event.context) event.exceptions = redactErrorInfo(event.exceptions)
event.request = redactErrorInfo(event.request) event.breadcrumbs = redactErrorInfo(event.breadcrumbs)
event.exceptions = redactErrorInfo(event.exceptions)
event.breadcrumbs = redactErrorInfo(event.breadcrumbs)
return true return true
}) })
}
const setupAnalytics = () => {
(window as any).plausible.q = []
page.subscribe($page => {
if ($page.route) {
(window as any).plausible.q.push(["pageview", {u: $page.route.id}])
}
}) })
} }
@@ -80,15 +96,6 @@
onMount(() => { onMount(() => {
Object.assign(window, {get, ...lib, ...util, ...app, ...state}) Object.assign(window, {get, ...lib, ...util, ...app, ...state})
if (import.meta.env.VITE_BUGSNAG_API_KEY) {
Bugsnag.start({
apiKey: import.meta.env.VITE_BUGSNAG_API_KEY,
collectUserIp: false,
})
}
setupBugsnag()
const getScoreEvent = () => { const getScoreEvent = () => {
const ALWAYS_KEEP = Infinity const ALWAYS_KEEP = Infinity
const NEVER_KEEP = 0 const NEVER_KEEP = 0
@@ -146,6 +153,9 @@
} }
if (!db) { if (!db) {
setupErrorTracking()
setupAnalytics()
ready = initStorage("flotilla", 4, { ready = initStorage("flotilla", 4, {
events: storageAdapters.fromRepository(repository, {throttle: 300, migrate: migrateEvents}), events: storageAdapters.fromRepository(repository, {throttle: 300, migrate: migrateEvents}),
relays: {keyPath: "url", store: throttled(1000, relays)}, relays: {keyPath: "url", store: throttled(1000, relays)},

View File

@@ -16,10 +16,10 @@ export default {
}, },
csp: { csp: {
directives: { directives: {
"script-src": ["self", "plausible.io"], "script-src": ["self", "plausible.coracle.social"],
"worker-src": ["self", "blob:"], "worker-src": ["self", "blob:"],
"style-src": ["self", "unsafe-inline"], "style-src": ["self", "unsafe-inline"],
"frame-src": ["open.spotify.com", "embed.tidal.com"], "frame-src": ["none"],
"child-src": ["none"], "child-src": ["none"],
"form-action": ["none"], "form-action": ["none"],
}, },