1- import { getAssetHostFromHost } from './utils' ;
1+ import { getAssetHostFromHost , getUiHostFromHost } from './utils' ;
22
33export const getNextjsAppRouterDocs = ( {
44 host,
@@ -9,26 +9,28 @@ export const getNextjsAppRouterDocs = ({
99} ) => {
1010 return `
1111==============================
12- FILE: app/components/PostHogProvider.${ language === 'typescript' ? 'tsx' : 'jsx'
13- }
12+ FILE: PostHogProvider.${
13+ language === 'typescript' ? 'tsx' : 'jsx'
14+ } (put it somewhere where client files are, like the components folder)
15+ LOCATION: Wherever other providers are, or the components folder
1416==============================
1517Changes:
1618- Create a PostHogProvider component that will be imported into the layout file.
1719
1820Example:
1921--------------------------------------------------
20- ' use client'
22+ " use client"
2123
22- import posthog from ' posthog-js'
23- import { PostHogProvider as PHProvider, usePostHog } from ' posthog-js/react'
24- import { Suspense, useEffect } from ' react'
24+ import posthog from " posthog-js"
25+ import { PostHogProvider as PHProvider, usePostHog } from " posthog-js/react"
26+ import { Suspense, useEffect } from " react"
2527import { usePathname, useSearchParams } from "next/navigation"
2628
2729export function PostHogProvider({ children }: { children: React.ReactNode }) {
2830 useEffect(() => {
2931 posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
3032 api_host: "/ingest",
31- ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST ,
33+ ui_host: " ${ getUiHostFromHost ( host ) } " ,
3234 capture_pageview: false, // We capture pageviews manually
3335 capture_pageleave: true, // Enable pageleave capture
3436 })
@@ -55,7 +57,7 @@ function PostHogPageView() {
5557 if (search) {
5658 url += "?" + search
5759 }
58- posthog.capture(' $pageview' , { ' $current_url' : url })
60+ posthog.capture(" $pageview" , { " $current_url" : url })
5961 }
6062 }, [pathname, searchParams, posthog])
6163
@@ -72,15 +74,16 @@ function SuspendedPostHogPageView() {
7274--------------------------------------------------
7375
7476==============================
75- FILE: app/layout.${ language === 'typescript' ? 'tsx' : 'jsx' }
77+ FILE: layout.${ language === 'typescript' ? 'tsx' : 'jsx' }
78+ LOCATION: Wherever the root layout is
7679==============================
7780Changes:
7881- Import the PostHogProvider from the providers file and wrap the app in it.
7982
8083Example:
8184--------------------------------------------------
8285// other imports
83- import { PostHogProvider } from './components/providers'
86+ import { PostHogProvider } from "LOCATION_OF_POSTHOG_PROVIDER"
8487
8588export default function RootLayout({ children }) {
8689 return (
@@ -98,18 +101,19 @@ export default function RootLayout({ children }) {
98101--------------------------------------------------
99102
100103==============================
101- FILE: app/lib/server/posthog.${ language === 'typescript' ? 'ts' : 'js' }
104+ FILE: posthog.${ language === 'typescript' ? 'ts' : 'js' }
105+ LOCATION: Wherever works best given the project structure
102106==============================
103107Changes:
104108- Initialize the PostHog Node.js client
105109
106110Example:
107111--------------------------------------------------
108- import { PostHog } from ' posthog-node'
112+ import { PostHog } from " posthog-node"
109113
110114export default function PostHogClient() {
111115 const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
112- host: process.env.NEXT_PUBLIC_POSTHOG_HOST ,
116+ host: " ${ host } " ,
113117 flushAt: 1,
114118 flushInterval: 0,
115119 })
@@ -119,6 +123,7 @@ export default function PostHogClient() {
119123
120124==============================
121125FILE: next.config.{js,ts,mjs,cjs}
126+ LOCATION: Wherever the root next config is
122127==============================
123128Changes:
124129- Add rewrites to the Next.js config to support PostHog, if there are existing rewrites, add the PostHog rewrites to them.
@@ -160,7 +165,10 @@ export const getNextjsPagesRouterDocs = ({
160165} ) => {
161166 return `
162167==============================
163- FILE: pages/_app.${ language === 'typescript' ? 'tsx' : 'jsx' }
168+ FILE: _app.${ language === 'typescript' ? 'tsx' : 'jsx' }
169+ LOCATION: Wherever the root _app.${
170+ language === 'typescript' ? 'tsx' : 'jsx'
171+ } file is
164172==============================
165173Changes:
166174- Initialize PostHog in _app.js.
@@ -169,25 +177,26 @@ Changes:
169177
170178Example:
171179--------------------------------------------------
172- import { useEffect } from ' react'
173- import { Router } from ' next/router'
174- import posthog from ' posthog-js'
175- import { PostHogProvider } from ' posthog-js/react'
180+ import { useEffect } from " react"
181+ import { Router } from " next/router"
182+ import posthog from " posthog-js"
183+ import { PostHogProvider } from " posthog-js/react"
176184
177185export default function App({ Component, pageProps }) {
178186 useEffect(() => {
179187 posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
180- api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
188+ api_host: "/ingest",
189+ ui_host: "${ getUiHostFromHost ( host ) } ",
181190 loaded: (posthog) => {
182- if (process.env.NODE_ENV === ' development' ) posthog.debug()
191+ if (process.env.NODE_ENV === " development" ) posthog.debug()
183192 },
184193 })
185194
186- const handleRouteChange = () => posthog?.capture(' $pageview' )
187- Router.events.on(' routeChangeComplete' , handleRouteChange)
195+ const handleRouteChange = () => posthog?.capture(" $pageview" )
196+ Router.events.on(" routeChangeComplete" , handleRouteChange)
188197
189198 return () => {
190- Router.events.off(' routeChangeComplete' , handleRouteChange)
199+ Router.events.off(" routeChangeComplete" , handleRouteChange)
191200 }
192201 }, [])
193202
@@ -200,18 +209,19 @@ export default function App({ Component, pageProps }) {
200209--------------------------------------------------
201210
202211==============================
203- FILE: lib/server/posthog.${ language === 'typescript' ? 'ts' : 'js' }
212+ FILE: posthog.${ language === 'typescript' ? 'ts' : 'js' }
213+ LOCATION: Wherever works best given the project structure
204214==============================
205215Changes:
206216- Initialize the PostHog Node.js client for server-side tracking.
207217
208218Example:
209219--------------------------------------------------
210- import { PostHog } from ' posthog-node'
220+ import { PostHog } from " posthog-node"
211221
212222export default function PostHogClient() {
213223 return new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
214- host: process.env.NEXT_PUBLIC_POSTHOG_HOST ,
224+ host: " ${ host } " ,
215225 flushAt: 1,
216226 flushInterval: 0,
217227 })
@@ -220,6 +230,7 @@ export default function PostHogClient() {
220230
221231==============================
222232FILE: next.config.{js,ts,mjs,cjs}
233+ LOCATION: Wherever the root next config is
223234==============================
224235Changes:
225236- Add rewrites to support PostHog tracking.
@@ -231,8 +242,8 @@ const nextConfig = {
231242 async rewrites() {
232243 return [
233244 { source: "/ingest/static/:path*", destination: "${ getAssetHostFromHost (
234- host ,
235- ) } /static/:path*" },
245+ host ,
246+ ) } /static/:path*" },
236247 { source: "/ingest/:path*", destination: "${ host } /:path*" },
237248 { source: "/ingest/decide", destination: "${ host } /decide" },
238249 ]
0 commit comments