Skip to content

Commit 6ac93c7

Browse files
committed
fix next template
1 parent 6169b9e commit 6ac93c7

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

templates/repo/packages/analytics/src/server.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@ export { getFeatureFlags } from "./vendor/posthog"
77

88
export type { Flags } from "./types"
99

10-
const posthog = new PostHog(
11-
process.env.POSTHOG_API_KEY || "",
12-
{
13-
host: process.env.POSTHOG_HOST || "https://app.posthog.com"
10+
function client() {
11+
const apiKey = process.env.POSTHOG_API_KEY
12+
13+
if (!apiKey) {
14+
return null
1415
}
15-
)
16+
17+
return new PostHog(apiKey, {
18+
host: process.env.POSTHOG_HOST || "https://app.posthog.com"
19+
})
20+
}
1621

1722
/**
1823
* Tracks one or more analytics events server-side using PostHog.
1924
*
2025
* @param eventData - An AnalyticsEvent object or an array of AnalyticsEvent objects to track.
2126
*/
2227
export async function track(eventData: AnalyticsEvent | AnalyticsEvent[]) {
28+
const posthog = client()
29+
30+
if (!posthog) {
31+
return
32+
}
33+
2334
const events = Array.isArray(eventData) ? eventData : [eventData]
2435

2536
events.forEach(({ event, ...rest }) => {
@@ -32,6 +43,5 @@ export async function track(eventData: AnalyticsEvent | AnalyticsEvent[]) {
3243
})
3344
})
3445

35-
// Flush to ensure events are sent
3646
await posthog.flush()
3747
}

templates/repo/packages/analytics/src/vendor/posthog.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,19 @@ export type FlagName = keyof Flags
1515
* Fetches feature flags for the specified user from the PostHog service.
1616
*
1717
* Returns an object mapping feature flag names to their values for the given user.
18+
* If PostHog is not configured (missing API key or host), returns an empty object.
1819
*
1920
* @param distinctUserId - The unique identifier for the user whose feature flags are being retrieved.
2021
* @returns An object containing feature flag values for the user. Boolean values indicate A/B test flags; string values indicate multivariate flags.
2122
*
22-
* @throws {Error} If required environment variables are missing or if the request to the PostHog service fails.
23+
* @throws {Error} If the request to the PostHog service fails.
2324
*/
2425
export async function getFeatureFlags(distinctUserId: string): Promise<Flags> {
25-
if (!POSTHOG_HOST) {
26-
console.warn(
27-
"No PostHog environment variables found, skipping feature flag lookup."
28-
)
29-
console.warn(
30-
"Set POSTHOG_API_KEY and POSTHOG_HOST in your environment variables."
31-
)
26+
if (!POSTHOG_HOST || !POSTHOG_API_KEY) {
3227
return {}
3328
}
34-
if (!POSTHOG_API_KEY) {
35-
throw new Error("The environment variable POSTHOG_API_KEY is missing.")
36-
}
3729

3830
if (!distinctUserId) {
39-
// throw new Error(`distinctUserId is required and it can't be empty`);
4031
return {} as Flags
4132
}
4233

0 commit comments

Comments
 (0)