-
Notifications
You must be signed in to change notification settings - Fork 310
feat: migrate analytics from Cloudflare Worker to SvelteKit hooks #2809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
936184d
feat: migrate analytics from Cloudflare Worker to SvelteKit hooks
TorstenDittmann 391230b
Add log batching for Profound analytics
TorstenDittmann 219664c
Format indentation in hooks.server.ts
TorstenDittmann cbd4953
Merge branch 'main' into feat/migrate-analytics-to-sveltekit
HarshMN2345 2529e05
Add Profound API credentials to env and CI
TorstenDittmann b0d42b4
Merge branch 'feat/migrate-analytics-to-sveltekit' of https://github.…
TorstenDittmann e45c31b
Update scripts, analytics, and generated data
TorstenDittmann 2b2781e
Update .prettierignore
TorstenDittmann b83b870
Return non-HTML responses directly
TorstenDittmann 1f2b498
Merge branch 'main' into feat/migrate-analytics-to-sveltekit
TorstenDittmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ node_modules | |
| .env.* | ||
| !.env.example | ||
| deploy | ||
| src/lib/generated | ||
|
|
||
| # Ignore files for Bun | ||
| bun.lock | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "stars": 55124, | ||
| "fetchedAt": "2026-03-10T14:12:30.207Z" | ||
| } | ||
| "stars": 55136, | ||
| "fetchedAt": "2026-03-18T12:04:09.106Z" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import type { Handle } from '@sveltejs/kit'; | ||
| import { env } from '$env/dynamic/private'; | ||
| import { building } from '$app/environment'; | ||
|
|
||
| const { PROFOUND_API_URL, PROFOUND_API_KEY } = env; | ||
|
|
||
| interface LogEntry { | ||
| timestamp: number; | ||
| host: string; | ||
| method: string; | ||
| pathname: string; | ||
| query_params: Record<string, string>; | ||
| ip: string | null; | ||
| userAgent: string | null; | ||
| referer: string | null; | ||
| bytes: number; | ||
| status: number; | ||
| } | ||
|
|
||
| class LogBatcher { | ||
| private queue: LogEntry[] = []; | ||
| private readonly MAX_SIZE = 10000; | ||
| private readonly FLUSH_INTERVAL_MS = 10000; | ||
| private flushTimer: ReturnType<typeof setInterval> | null = null; | ||
| private isRunning = false; | ||
|
|
||
| constructor() { | ||
| this.start(); | ||
| } | ||
|
|
||
| start(): void { | ||
| if (this.isRunning) return; | ||
|
|
||
| this.isRunning = true; | ||
| this.flushTimer = setInterval(() => { | ||
| void this.flush(); | ||
| }, this.FLUSH_INTERVAL_MS); | ||
| } | ||
|
|
||
| stop(): void { | ||
| if (this.flushTimer) { | ||
| clearInterval(this.flushTimer); | ||
| this.flushTimer = null; | ||
| } | ||
| this.isRunning = false; | ||
| } | ||
|
|
||
| add(log: LogEntry): void { | ||
| // Evict oldest entries if at capacity (FIFO) | ||
| while (this.queue.length >= this.MAX_SIZE) { | ||
| this.queue.shift(); | ||
| } | ||
| this.queue.push(log); | ||
| } | ||
|
|
||
| async flush(): Promise<void> { | ||
| if (this.queue.length === 0) return; | ||
| if (!PROFOUND_API_URL || !PROFOUND_API_KEY) return; | ||
|
|
||
| // Take all logs from queue | ||
| const batch = this.queue.splice(0); | ||
|
|
||
| try { | ||
| const response = await fetch(PROFOUND_API_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-API-Key': PROFOUND_API_KEY | ||
| }, | ||
| body: JSON.stringify(batch) | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.error(`Analytics flush failed: ${response.status} ${response.statusText}`); | ||
| } | ||
| } catch (error) { | ||
| console.error('Analytics flush failed:', error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Singleton instance | ||
| const logBatcher = new LogBatcher(); | ||
|
|
||
| export const profoundAnalytics: Handle = async ({ event, resolve }) => { | ||
| // Short circuit if analytics is not configured or during pre-render | ||
| if (building || !PROFOUND_API_URL || !PROFOUND_API_KEY) { | ||
| return resolve(event); | ||
| } | ||
|
|
||
| const response = await resolve(event); | ||
|
|
||
| // Only track HTML pages, not API responses or static assets | ||
| const contentType = response.headers.get('content-type'); | ||
| if (!contentType?.includes('text/html')) { | ||
| return response; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Calculate header size | ||
| const headerSize = Array.from(response.headers.entries()).reduce( | ||
| (total, [key, value]) => total + key.length + value.length + 4, | ||
| 0 | ||
| ); | ||
|
|
||
| // Get body size | ||
| const contentLength = response.headers.get('content-length'); | ||
| const bodySize = contentLength ? Number.parseInt(contentLength, 10) || 0 : 0; | ||
| const totalBytesSent = headerSize + bodySize; | ||
|
|
||
| // Build log data | ||
| const logData: LogEntry = { | ||
| timestamp: Date.now(), | ||
| host: event.url.hostname, | ||
| method: event.request.method, | ||
| pathname: event.url.pathname, | ||
| query_params: Object.fromEntries(event.url.searchParams), | ||
| ip: event.getClientAddress(), | ||
TorstenDittmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| userAgent: event.request.headers.get('user-agent'), | ||
| referer: event.request.headers.get('referer'), | ||
| bytes: totalBytesSent, | ||
| status: response.status | ||
| }; | ||
|
|
||
| // Non-blocking: add to queue and return immediately | ||
| logBatcher.add(logData); | ||
|
|
||
| return response; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.