Skip to content

Commit f1d3304

Browse files
committed
fix(www): skip PostHog when api key is missing
1 parent 6993c0a commit f1d3304

5 files changed

Lines changed: 84 additions & 28 deletions

File tree

apps/www/components/posthog-provider.tsx

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,40 @@ import { usePathname, useSearchParams } from "next/navigation"
55
import posthog from "posthog-js"
66
import { PostHogProvider } from "posthog-js/react"
77

8-
if (typeof window !== "undefined") {
9-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY!, {
10-
api_host: "https://app.posthog.com",
11-
capture_pageview: true,
12-
session_recording: {
13-
maskAllInputs: false,
14-
},
15-
// Enable debug mode in development
16-
loaded: (posthog) => {
17-
if (process.env.NODE_ENV === "development") posthog.debug()
18-
},
19-
})
20-
}
8+
import {
9+
capturePostHogEvent,
10+
initPostHog,
11+
isPostHogEnabled,
12+
} from "@/lib/posthog"
13+
14+
initPostHog()
2115

2216
export function PostHogPageview(): React.ReactNode {
2317
const pathname = usePathname()
2418
const searchParams = useSearchParams()
2519

2620
useEffect(() => {
27-
if (pathname) {
28-
let url = window.origin + pathname
29-
if (searchParams && searchParams.toString()) {
30-
url = url + `?${searchParams.toString()}`
31-
}
32-
posthog.capture("$pageview", {
33-
$current_url: url,
34-
})
21+
if (!isPostHogEnabled || !pathname) {
22+
return
23+
}
24+
25+
let url = window.origin + pathname
26+
if (searchParams && searchParams.toString()) {
27+
url = url + `?${searchParams.toString()}`
3528
}
29+
30+
capturePostHogEvent("$pageview", {
31+
$current_url: url,
32+
})
3633
}, [pathname, searchParams])
3734

3835
return <></>
3936
}
4037

4138
export function PHProvider({ children }: { children: React.ReactNode }) {
39+
if (!isPostHogEnabled) {
40+
return children
41+
}
42+
4243
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
4344
}

apps/www/components/sidebar-cta.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import Link from "next/link"
44
import { CheckIcon, ChevronRight } from "lucide-react"
5-
import posthog from "posthog-js"
65

76
import { trackEvent } from "@/lib/events"
87
import { Button } from "@/components/ui/button"
@@ -116,7 +115,8 @@ export function ProductHuntCTA() {
116115
<Link
117116
href="https://www.producthunt.com/posts/magic-ui-2?utm_source=sidebar-cta&utm_medium=sidebar-cta&utm_campaign=product-hunt-sidebar-cta"
118117
target="_blank"
119-
onClick={() => posthog.capture("product_hunt_sidebar_cta_clicked")}
118+
rel="noopener noreferrer"
119+
onClick={() => trackEvent({ name: "product_hunt_sidebar_cta_clicked" })}
120120
className="group my-20 flex w-full flex-col items-center justify-center gap-2 rounded-xl bg-[#ff6154] p-4 text-center text-lg leading-tight font-medium text-white"
121121
>
122122
<TextAnimate animate="blurInUp" by="word" className="text-2xl">

apps/www/components/site-banner.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import Link from "next/link"
44
import { ChevronRight } from "lucide-react"
5-
import posthog from "posthog-js"
5+
6+
import { trackEvent } from "@/lib/events"
67

78
export function ProBanner() {
89
return (
910
<div className="group relative top-0 bg-indigo-600 py-3 text-white transition-all duration-300 md:py-0">
1011
<div className="container flex flex-col items-center justify-center gap-4 md:h-12 md:flex-row">
1112
<Link
1213
href="https://pro.magicui.design"
13-
onClick={() => posthog.capture("banner_cta_clicked")}
14+
onClick={() => trackEvent({ name: "banner_cta_clicked" })}
1415
target="_blank"
16+
rel="noopener noreferrer"
1517
className="inline-flex text-xs leading-normal md:text-sm"
1618
>
1719
{" "}
@@ -34,8 +36,9 @@ export function ProductHuntBanner() {
3436
<div className="container flex flex-col items-center justify-center gap-4 md:h-12 md:flex-row">
3537
<Link
3638
href="https://www.producthunt.com/posts/magic-ui-2?utm_source=site-banner&utm_medium=banner&utm_campaign=product-hunt-banner"
37-
onClick={() => posthog.capture("product_hunt_banner_clicked")}
39+
onClick={() => trackEvent({ name: "product_hunt_banner_clicked" })}
3840
target="_blank"
41+
rel="noopener noreferrer"
3942
className="inline-flex text-xs leading-normal md:text-sm"
4043
>
4144
{" "}

apps/www/lib/events.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import posthog from "posthog-js"
21
import { z } from "zod"
32

3+
import { capturePostHogEvent } from "@/lib/posthog"
4+
45
const eventSchema = z.object({
56
name: z.enum([
67
"copy_npm_command",
@@ -19,6 +20,9 @@ const eventSchema = z.object({
1920

2021
"sidebar_cta_clicked",
2122
"header_cta_clicked",
23+
"banner_cta_clicked",
24+
"product_hunt_banner_clicked",
25+
"product_hunt_sidebar_cta_clicked",
2226
]),
2327
properties: z
2428
.record(
@@ -33,6 +37,6 @@ export type Event = z.infer<typeof eventSchema>
3337
export function trackEvent(input: Event): void {
3438
const event = eventSchema.parse(input)
3539
if (event) {
36-
posthog.capture(event.name, event.properties)
40+
capturePostHogEvent(event.name, event.properties)
3741
}
3842
}

apps/www/lib/posthog.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import posthog from "posthog-js"
2+
3+
type PostHogPropertyValue = string | number | boolean | null
4+
5+
export type PostHogEventProperties = Record<string, PostHogPropertyValue>
6+
7+
const posthogApiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY?.trim() ?? ""
8+
9+
export const isPostHogEnabled = posthogApiKey.length > 0
10+
11+
let hasInitializedPostHog = false
12+
13+
export function initPostHog(): void {
14+
if (
15+
typeof window === "undefined" ||
16+
!isPostHogEnabled ||
17+
hasInitializedPostHog
18+
) {
19+
return
20+
}
21+
22+
hasInitializedPostHog = true
23+
24+
posthog.init(posthogApiKey, {
25+
api_host: "https://app.posthog.com",
26+
capture_pageview: true,
27+
session_recording: {
28+
maskAllInputs: false,
29+
},
30+
loaded: (client) => {
31+
if (process.env.NODE_ENV === "development") {
32+
client.debug()
33+
}
34+
},
35+
})
36+
}
37+
38+
export function capturePostHogEvent(
39+
name: string,
40+
properties?: PostHogEventProperties
41+
): void {
42+
if (!isPostHogEnabled) {
43+
return
44+
}
45+
46+
initPostHog()
47+
posthog.capture(name, properties)
48+
}

0 commit comments

Comments
 (0)