-
Notifications
You must be signed in to change notification settings - Fork 1
DC-4894 Move PostHog to worker #48
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4c71392
fix: error handling test
aidankmcalister ad78e65
chore: trigger CI
aidankmcalister 912e877
chore: rerun checks
aidankmcalister 960a5fa
chore: rerun checks
aidankmcalister e7dee3a
feat: analytics moved from `create-db` CLI to `create-db-worker`
aidankmcalister 0cd5938
chore: trigger CI
aidankmcalister 2150f28
fix: posthog debugging
aidankmcalister 68963fe
fix: keep_vars: true
aidankmcalister d317564
chore: trigger CI
aidankmcalister f710775
chore: test preview url
aidankmcalister 6c05408
Merge remote-tracking branch 'origin/main' into DC-4894-posthog-fix
aidankmcalister 7660493
fix: coderabbit changes
aidankmcalister 30b9e88
fix: error handling
aidankmcalister ca76f42
fix: coderabbit updates
aidankmcalister 8ca7ecd
fix: coderabbit changes
aidankmcalister 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,60 @@ | ||
class EventCaptureError extends Error { | ||
constructor( | ||
public readonly event: string, | ||
public readonly statusCode: number, | ||
public readonly statusText: string, | ||
) { | ||
super(`Failed to submit PostHog event '${event}': ${statusCode} ${statusText}`); | ||
} | ||
} | ||
|
||
interface AnalyticsProperties { | ||
[key: string]: any; | ||
} | ||
|
||
class PosthogEventCapture { | ||
constructor(private env: { POSTHOG_API_HOST?: string; POSTHOG_API_KEY?: string }) {} | ||
|
||
async capture(eventName: string, properties: AnalyticsProperties = {}) { | ||
const host = this.env.POSTHOG_API_HOST?.replace(/\/+$/, ''); | ||
const key = this.env.POSTHOG_API_KEY; | ||
|
||
if (!host || !key) { | ||
return; | ||
} | ||
|
||
const POSTHOG_CAPTURE_URL = `${host}/capture`; | ||
const POSTHOG_KEY = key; | ||
|
||
const payload = { | ||
api_key: POSTHOG_KEY, | ||
event: eventName, | ||
distinct_id: crypto.randomUUID(), | ||
properties: { | ||
$process_person_profile: false, | ||
...properties, | ||
}, | ||
}; | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try { | ||
const response = await fetch(POSTHOG_CAPTURE_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(payload), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new EventCaptureError(eventName, response.statusText); | ||
} | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
console.log(`${eventName}: Success`); | ||
} catch (error) { | ||
console.error(`${eventName}: Failed - ${error instanceof Error ? error.message : String(error)}`); | ||
throw error; | ||
} | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
export { PosthogEventCapture, EventCaptureError }; |
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.
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
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.