-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(analytics): add Profound web traffic tracking #3835
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
+142
−11
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f6bcd1
feat(analytics): add Profound web traffic tracking
waleedlatif1 627b61e
fix(analytics): address PR review — add endpoint check and document t…
waleedlatif1 a902d60
chore(analytics): remove implementation comments
waleedlatif1 e407ddc
fix(analytics): guard sendToProfound with try-catch and align check w…
waleedlatif1 86a85a3
fix(analytics): strip sensitive query params and remove redundant guard
waleedlatif1 acfbf94
chore(analytics): remove unnecessary query param filtering
waleedlatif1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * Profound Analytics - Custom log integration | ||
| * | ||
| * Buffers HTTP request logs in memory and flushes them in batches to Profound's API. | ||
| * Runs in Node.js (proxy.ts on ECS), so module-level state persists across requests. | ||
| * @see https://docs.tryprofound.com/agent-analytics/custom | ||
| */ | ||
| import { createLogger } from '@sim/logger' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { isHosted } from '@/lib/core/config/feature-flags' | ||
|
|
||
| const logger = createLogger('ProfoundAnalytics') | ||
|
|
||
| const FLUSH_INTERVAL_MS = 10_000 | ||
| const MAX_BATCH_SIZE = 500 | ||
|
|
||
| interface ProfoundLogEntry { | ||
| timestamp: string | ||
| method: string | ||
| host: string | ||
| path: string | ||
| status_code: number | ||
| ip: string | ||
| user_agent: string | ||
| query_params?: Record<string, string> | ||
| referer?: string | ||
| } | ||
|
|
||
| let buffer: ProfoundLogEntry[] = [] | ||
| let flushTimer: NodeJS.Timeout | null = null | ||
|
|
||
| /** | ||
| * Returns true if Profound analytics is configured. | ||
| */ | ||
| export function isProfoundEnabled(): boolean { | ||
| return isHosted && Boolean(env.PROFOUND_API_KEY) && Boolean(env.PROFOUND_ENDPOINT) | ||
| } | ||
|
|
||
| /** | ||
| * Flushes buffered log entries to Profound's API. | ||
| */ | ||
| async function flush(): Promise<void> { | ||
| if (buffer.length === 0) return | ||
|
|
||
| const apiKey = env.PROFOUND_API_KEY | ||
| if (!apiKey) { | ||
| buffer = [] | ||
| return | ||
| } | ||
|
|
||
| const endpoint = env.PROFOUND_ENDPOINT | ||
| if (!endpoint) { | ||
| buffer = [] | ||
| return | ||
| } | ||
| const entries = buffer.splice(0, MAX_BATCH_SIZE) | ||
|
|
||
| try { | ||
| const response = await fetch(endpoint, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'x-api-key': apiKey, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(entries), | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| logger.error(`Profound API returned ${response.status}`) | ||
| } | ||
| } catch (error) { | ||
| logger.error('Failed to flush logs to Profound', error) | ||
| } | ||
waleedlatif1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function ensureFlushTimer(): void { | ||
| if (flushTimer) return | ||
| flushTimer = setInterval(() => { | ||
| flush().catch(() => {}) | ||
| }, FLUSH_INTERVAL_MS) | ||
| flushTimer.unref() | ||
| } | ||
|
|
||
| /** | ||
| * Queues a request log entry for the next batch flush to Profound. | ||
| */ | ||
| export function sendToProfound(request: Request, statusCode: number): void { | ||
| if (!isProfoundEnabled()) return | ||
|
|
||
| try { | ||
| const url = new URL(request.url) | ||
| const queryParams: Record<string, string> = {} | ||
| url.searchParams.forEach((value, key) => { | ||
| queryParams[key] = value | ||
| }) | ||
|
|
||
| buffer.push({ | ||
| timestamp: new Date().toISOString(), | ||
| method: request.method, | ||
| host: url.hostname, | ||
| path: url.pathname, | ||
| status_code: statusCode, | ||
| ip: | ||
| request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || | ||
| request.headers.get('x-real-ip') || | ||
| '0.0.0.0', | ||
| user_agent: request.headers.get('user-agent') || '', | ||
| ...(Object.keys(queryParams).length > 0 && { query_params: queryParams }), | ||
| ...(request.headers.get('referer') && { referer: request.headers.get('referer')! }), | ||
waleedlatif1 marked this conversation as resolved.
Show resolved
Hide resolved
waleedlatif1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| ensureFlushTimer() | ||
|
|
||
| if (buffer.length >= MAX_BATCH_SIZE) { | ||
| flush().catch(() => {}) | ||
| } | ||
| } catch (error) { | ||
| logger.error('Failed to enqueue log entry', error) | ||
| } | ||
| } | ||
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
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.