-
Notifications
You must be signed in to change notification settings - Fork 1
DC-5180-analytics #55
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8be4556
fix: analytics
aidankmcalister 615a60b
fix: api routes fixed
aidankmcalister 77a3c06
fix: page viewed
aidankmcalister 9be1411
fix: build error
aidankmcalister 56d7a2a
fix: env vars set up different
aidankmcalister 41fa7d7
chore: console log removed
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,54 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { getEnv } from "@/lib/env"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const env = getEnv(); | ||
|
||
const rateLimitResult = await env.CLAIM_DB_RATE_LIMITER.limit({ | ||
key: request.url, | ||
}); | ||
if (!rateLimitResult.success) { | ||
return NextResponse.json({ error: "Rate limit exceeded" }, { status: 429 }); | ||
} | ||
|
||
const POSTHOG_API_KEY = env.POSTHOG_API_KEY; | ||
const POSTHOG_PROXY_HOST = env.POSTHOG_API_HOST; | ||
|
||
if (!POSTHOG_API_KEY || !POSTHOG_PROXY_HOST) { | ||
return NextResponse.json({ success: true }); | ||
} | ||
aidankmcalister marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
try { | ||
const { | ||
event, | ||
properties, | ||
}: { event: string; properties: Record<string, any> } = | ||
await request.json(); | ||
|
||
if (!event) { | ||
return NextResponse.json( | ||
{ error: "Event name required" }, | ||
{ status: 400 } | ||
); | ||
} | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await fetch(`${POSTHOG_PROXY_HOST}/e`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${POSTHOG_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
api_key: POSTHOG_API_KEY, | ||
event, | ||
properties: properties || {}, | ||
distinct_id: "web-claim", | ||
}), | ||
aidankmcalister marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
|
||
return NextResponse.json({ success: true }); | ||
aidankmcalister marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} catch (error) { | ||
console.error("Failed to send PostHog event:", error); | ||
return NextResponse.json({ error: "Analytics failed" }, { status: 500 }); | ||
} | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use client"; | ||
|
||
import { Suspense, useEffect } from "react"; | ||
import { usePathname, useSearchParams } from "next/navigation"; | ||
import { sendAnalyticsEvent } from "@/lib/analytics"; | ||
|
||
function PageViewTrackerContent() { | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
useEffect(() => { | ||
if (typeof window === "undefined") return; | ||
|
||
if (pathname) { | ||
const url = window.location.href; | ||
const search = searchParams?.toString(); | ||
const fullPath = search ? `${pathname}?${search}` : pathname; | ||
|
||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sendAnalyticsEvent("create_db:claim_page_viewed", { | ||
path: pathname, | ||
full_path: fullPath, | ||
url: url, | ||
referrer: document.referrer || "", | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
}, [pathname, searchParams]); | ||
|
||
return null; | ||
} | ||
|
||
export function PageViewTracker() { | ||
return ( | ||
<Suspense fallback={null}> | ||
<PageViewTrackerContent /> | ||
</Suspense> | ||
); | ||
} |
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,62 +1,18 @@ | ||
import { getEnv } from "./env"; | ||
"use client"; | ||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export async function sendPosthogEvent( | ||
export const sendAnalyticsEvent = async ( | ||
event: string, | ||
properties: Record<string, any> | ||
) { | ||
const env = getEnv(); | ||
const POSTHOG_API_KEY = env.POSTHOG_API_KEY; | ||
const POSTHOG_PROXY_HOST = env.POSTHOG_API_HOST; | ||
|
||
// Skip analytics if PostHog is not configured | ||
if (!POSTHOG_API_KEY || !POSTHOG_PROXY_HOST) { | ||
return; | ||
} | ||
|
||
try { | ||
await fetch(`${POSTHOG_PROXY_HOST}/e`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${POSTHOG_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
api_key: POSTHOG_API_KEY, | ||
event, | ||
properties, | ||
distinct_id: "web-claim", | ||
}), | ||
}); | ||
} catch (error) { | ||
console.error("Failed to send PostHog event:", error); | ||
} | ||
} | ||
|
||
export async function trackClaimSuccess(projectID: string) { | ||
const env = getEnv(); | ||
|
||
try { | ||
env.CREATE_DB_DATASET.writeDataPoint({ | ||
blobs: ["database_claimed"], | ||
indexes: ["claim_db"], | ||
}); | ||
} catch (error) { | ||
console.error("Failed to write analytics data point:", error); | ||
} | ||
|
||
await sendPosthogEvent("create_db:claim_successful", { | ||
"project-id": projectID, | ||
) => { | ||
const response = await fetch(`${window.location.origin}/api/analytics`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ event, properties }), | ||
}); | ||
} | ||
|
||
export async function trackClaimFailure( | ||
projectID: string, | ||
status: number, | ||
error: string | ||
) { | ||
await sendPosthogEvent("create_db:claim_failed", { | ||
"project-id": projectID, | ||
status, | ||
error, | ||
}); | ||
} | ||
if (!response.ok) { | ||
console.error("Failed to send analytics event:", response); | ||
} | ||
}; |
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.