Skip to content

Commit e931234

Browse files
authored
docs: fix posthog capture for md event (#15023)
1 parent 91998f2 commit e931234

File tree

13 files changed

+99
-90
lines changed

13 files changed

+99
-90
lines changed

www/apps/bloom/app/md-content/[[...slug]]/route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { unstable_cache } from "next/cache"
44
import { notFound } from "next/navigation"
55
import { NextRequest, NextResponse } from "next/server"
66
import path from "path"
7-
import { posthog } from "posthog-js"
7+
import { PostHog } from "posthog-node"
88
import {
99
addUrlToRelativeLink,
1010
crossProjectLinksPlugin,
@@ -71,24 +71,20 @@ export async function GET(req: NextRequest, { params }: Params) {
7171
acceptHeader.includes("text/plain") ||
7272
acceptHeader.includes("text/markdown")
7373
) {
74-
if (!posthog.__loaded) {
75-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
76-
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
77-
person_profiles: "always",
78-
defaults: "2025-05-24",
79-
})
80-
}
74+
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
75+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
76+
})
8177

82-
posthog.capture(
83-
"md_content_requested_agents",
84-
{
78+
client.capture({
79+
distinctId: "anonymous",
80+
event: "md_content_requested_agents",
81+
properties: {
8582
$current_url: req.url,
8683
$raw_user_agent: req.headers.get("user-agent") || undefined,
8784
},
88-
{
89-
send_instantly: true,
90-
}
91-
)
85+
})
86+
87+
await client.shutdown()
9288
}
9389

9490
return new NextResponse(cleanMdContent + PLAINTEXT_DOC_MESSAGE, {

www/apps/bloom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"docs-ui": "*",
2424
"next": "15.3.9",
2525
"posthog-js": "^1.298.1",
26+
"posthog-node": "^5.29.0",
2627
"react": "19.2.4",
2728
"react-dom": "19.2.4",
2829
"rehype-mdx-code-props": "^2.0.0",

www/apps/book/app/md-content/[[...slug]]/route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { unstable_cache } from "next/cache"
44
import { notFound } from "next/navigation"
55
import { NextRequest, NextResponse } from "next/server"
66
import path from "path"
7-
import { posthog } from "posthog-js"
7+
import { PostHog } from "posthog-node"
88
import {
99
addUrlToRelativeLink,
1010
crossProjectLinksPlugin,
@@ -78,24 +78,20 @@ export async function GET(req: NextRequest, { params }: Params) {
7878
acceptHeader.includes("text/plain") ||
7979
acceptHeader.includes("text/markdown")
8080
) {
81-
if (!posthog.__loaded) {
82-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
83-
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
84-
person_profiles: "always",
85-
defaults: "2025-05-24",
86-
})
87-
}
81+
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
82+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
83+
})
8884

89-
posthog.capture(
90-
"md_content_requested_agents",
91-
{
85+
client.capture({
86+
distinctId: "anonymous",
87+
event: "md_content_requested_agents",
88+
properties: {
9289
$current_url: req.url,
9390
$raw_user_agent: req.headers.get("user-agent") || undefined,
9491
},
95-
{
96-
send_instantly: true,
97-
}
98-
)
92+
})
93+
94+
await client.shutdown()
9995
}
10096

10197
return new NextResponse(cleanMdContent + PLAINTEXT_DOC_MESSAGE, {

www/apps/book/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"loops": "^6.0.1",
2626
"next": "15.3.9",
2727
"posthog-js": "^1.298.1",
28+
"posthog-node": "^5.29.0",
2829
"react": "19.2.4",
2930
"react-dom": "19.2.4",
3031
"rehype-mdx-code-props": "^2.0.0",

www/apps/cloud/app/md-content/[[...slug]]/route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { unstable_cache } from "next/cache"
44
import { notFound } from "next/navigation"
55
import { NextRequest, NextResponse } from "next/server"
66
import path from "path"
7-
import posthog from "posthog-js"
7+
import { PostHog } from "posthog-node"
88
import {
99
addUrlToRelativeLink,
1010
crossProjectLinksPlugin,
@@ -68,24 +68,20 @@ export async function GET(req: NextRequest, { params }: Params) {
6868
acceptHeader.includes("text/plain") ||
6969
acceptHeader.includes("text/markdown")
7070
) {
71-
if (!posthog.__loaded) {
72-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
73-
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
74-
person_profiles: "always",
75-
defaults: "2025-05-24",
76-
})
77-
}
71+
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
72+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
73+
})
7874

79-
posthog.capture(
80-
"md_content_requested_agents",
81-
{
75+
client.capture({
76+
distinctId: "anonymous",
77+
event: "md_content_requested_agents",
78+
properties: {
8279
$current_url: req.url,
8380
$raw_user_agent: req.headers.get("user-agent") || undefined,
8481
},
85-
{
86-
send_instantly: true,
87-
}
88-
)
82+
})
83+
84+
await client.shutdown()
8985
}
9086

9187
return new NextResponse(cleanMdContent + PLAINTEXT_DOC_MESSAGE, {

www/apps/cloud/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"docs-ui": "*",
2525
"next": "15.3.9",
2626
"posthog-js": "^1.298.1",
27+
"posthog-node": "^5.29.0",
2728
"react": "19.2.4",
2829
"react-dom": "19.2.4",
2930
"rehype-mdx-code-props": "^2.0.0",

www/apps/resources/app/md-content/[[...slug]]/route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import type { Plugin } from "unified"
1313
import { filesMap } from "../../../generated/files-map.mjs"
1414
import { slugChanges } from "../../../generated/slug-changes.mjs"
15-
import { posthog } from "posthog-js"
15+
import { PostHog } from "posthog-node"
1616

1717
type Params = {
1818
params: Promise<{ slug: string[] }>
@@ -76,24 +76,20 @@ export async function GET(req: NextRequest, { params }: Params) {
7676
acceptHeader.includes("text/plain") ||
7777
acceptHeader.includes("text/markdown")
7878
) {
79-
if (!posthog.__loaded) {
80-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
81-
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
82-
person_profiles: "always",
83-
defaults: "2025-05-24",
84-
})
85-
}
79+
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
80+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
81+
})
8682

87-
posthog.capture(
88-
"md_content_requested_agents",
89-
{
83+
client.capture({
84+
distinctId: "anonymous",
85+
event: "md_content_requested_agents",
86+
properties: {
9087
$current_url: req.url,
9188
$raw_user_agent: req.headers.get("user-agent") || undefined,
9289
},
93-
{
94-
send_instantly: true,
95-
}
96-
)
90+
})
91+
92+
await client.shutdown()
9793
}
9894

9995
return new NextResponse(cleanMdContent + PLAINTEXT_DOC_MESSAGE, {

www/apps/resources/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"next": "15.3.9",
2525
"next-mdx-remote-client": "2",
2626
"posthog-js": "^1.298.1",
27+
"posthog-node": "^5.29.0",
2728
"react": "19.2.4",
2829
"react-dom": "19.2.4",
2930
"rehype-mdx-code-props": "^2.0.0",

www/apps/ui/app/md-content/[[...slug]]/route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Plugin } from "unified"
99
import * as Icons from "@medusajs/icons"
1010
import * as HookValues from "@/specs/hook-values"
1111
import { colors as allColors } from "@/config/colors"
12-
import { posthog } from "posthog-js"
12+
import { PostHog } from "posthog-node"
1313

1414
type Params = {
1515
params: Promise<{ slug: string[] }>
@@ -43,24 +43,20 @@ export async function GET(req: NextRequest, { params }: Params) {
4343
acceptHeader.includes("text/plain") ||
4444
acceptHeader.includes("text/markdown")
4545
) {
46-
if (!posthog.__loaded) {
47-
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
48-
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
49-
person_profiles: "always",
50-
defaults: "2025-05-24",
51-
})
52-
}
46+
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
47+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
48+
})
5349

54-
posthog.capture(
55-
"md_content_requested_agents",
56-
{
50+
client.capture({
51+
distinctId: "anonymous",
52+
event: "md_content_requested_agents",
53+
properties: {
5754
$current_url: req.url,
5855
$raw_user_agent: req.headers.get("user-agent") || undefined,
5956
},
60-
{
61-
send_instantly: true,
62-
}
63-
)
57+
})
58+
59+
await client.shutdown()
6460
}
6561

6662
return new NextResponse(cleanMdContent + PLAINTEXT_DOC_MESSAGE, {

www/apps/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"docs-ui": "*",
2626
"next": "15.3.9",
2727
"posthog-js": "^1.298.1",
28+
"posthog-node": "^5.29.0",
2829
"react": "19.2.4",
2930
"react-dom": "19.2.4",
3031
"rehype-mdx-code-props": "^2.0.0",

0 commit comments

Comments
 (0)