Skip to content

Commit

Permalink
Add telemetry for page views & render times
Browse files Browse the repository at this point in the history
  • Loading branch information
rianadon committed Jun 17, 2023
1 parent d7371e3 commit 4fde2a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import Instructions from './lib/Instructions.svelte';
import FilamentChart from './lib/FilamentChart.svelte';
import { serialize, deserialize } from './lib/serialize';
import { trackPageView, trackEvent } from './lib/telemetry';
import presetOrigOrig from './assets/presets/original.original.json'
import presetLight from './assets/presets/original.lightcycle.json'
Expand Down Expand Up @@ -48,8 +49,11 @@
let instructionsOpen = !(localStorage['instructions'] === 'false')
let showSupports = false
let renderBegin: number
$: localStorage['instructions'] = JSON.stringify(instructionsOpen)
trackPageView()
exampleGeometry().then(g => {
// Load the example gemoetry if nothing has been rendered yet
if (!keyboardGeometry) keyboardGeometry = g
Expand Down Expand Up @@ -92,6 +96,7 @@
function downloadSTL() {
generatingSTL = true;
stlDialogOpen = true;
renderBegin = window.performance.now()
myWorker.postMessage({type: "stl", data: state });
}
Expand Down Expand Up @@ -127,6 +132,7 @@
// STL generation finished. Download it!
generatingSTL = false;
generatingSCADSTL = false;
trackEvent('dactyl-render', { time: window.performance.now() - renderBegin })
const blob = new Blob([e.data.data], { type: "application/octet-stream" })
download(blob, "model.stl")
} else if (e.data.type == 'csg') {
Expand Down
24 changes: 24 additions & 0 deletions src/lib/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
async function post(url: string, body: any) {
await (await fetch('https://analytics.ryanis.cool' + url, {
method: 'POST',
body: JSON.stringify(body)
})).text()
}

export function trackPageView() {
return post('/', {
url: window.location.href,
language: window.navigator.language,
referrer: document.referrer
})
}

export function trackEvent(event: string, keys: Record<string, string|number>) {
const entries = Object.entries(keys)
return post('/', {
url: window.location.href,
event,
strings: Object.fromEntries(entries.filter(([_,v]) => typeof v == 'string')),
numbers: Object.fromEntries(entries.filter(([_,v]) => typeof v == 'number')),
})
}

0 comments on commit 4fde2a0

Please sign in to comment.