Open jacob-ai-bot[bot] opened 1 day ago
Hello human! 👋
This PR was created by JACoB to address the issue Integrate PostHog Analytics into Next.js Application
Please review the PR carefully. Auto-generated code can and will contain subtle bugs and mistakes.
If you identify code that needs to be changed, please reject the PR with a specific reason. Be as detailed as possible in your comments. JACoB will take these comments, make changes to the code and push up changes. Please note that this process will take a few minutes.
Once the code looks good, approve the PR and merge the code.
Summary:
Description We need to integrate PostHog analytics into our Next.js application to track user interactions and gather valuable insights. This integration will help us understand user behavior, improve user experience, and make data-driven decisions for future development. We currently have PostHog on the server side (node) but we need to add it to the client.
Expected Outcome PostHog is successfully integrated into the Next.js application. User interactions and events are accurately tracked and sent to our PostHog analytics dashboard. The integration is optimized for performance and does not negatively impact the application's load times or responsiveness. User privacy is maintained, with appropriate measures in place to anonymize data and gain necessary consents. The analytics tracking can be easily enabled or disabled through a configuration setting to accommodate different deployment environments (e.g., development, staging, production). The docs are here: https://posthog.com/docs/libraries/next-js @jacob-ai-bot --skip-build
Plan:
Step 1: Edit
/.env.example
Task: Add client-side PostHog environment variables to .env.example
Instructions: In the /.env.example file, add the following environment variables for client-side PostHog integration:
NEXT_PUBLIC_POSTHOG_KEY="your-posthog-api-key"
(provide a sample value or leave it empty)NEXT_PUBLIC_POSTHOG_HOST="https://app.posthog.com"
(optional, include if using a custom PostHog host)NEXT_PUBLIC_ENABLE_ANALYTICS="false"
(set to 'false' by default to allow easy enabling or disabling of analytics)Ensure these variables are clearly documented, indicating their purpose and usage.
Exit Criteria: The .env.example file includes NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_HOST, and NEXT_PUBLIC_ENABLE_ANALYTICS with appropriate sample values or placeholders.
Step 2: Edit
/src/env.js
Task: Validate PostHog environment variables in env.js
Instructions: In the /src/env.js file, add the following environment variables to the client-side schema under the
client
section:NEXT_PUBLIC_POSTHOG_KEY: z.string().optional()
NEXT_PUBLIC_POSTHOG_HOST: z.string().optional().default('https://app.posthog.com')
NEXT_PUBLIC_ENABLE_ANALYTICS: z.enum(['true', 'false']).default('false')
This will ensure that these environment variables are validated and have default values where appropriate. Update the
runtimeEnv
section to include these variables, mapping them fromprocess.env
. Ensure that these variables are accessible in the client-side code.Exit Criteria: The environment variables NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_HOST, and NEXT_PUBLIC_ENABLE_ANALYTICS are validated and accessible in the client-side environment variables.
Step 3: Create
/src/lib/posthog.ts
Task: Create PostHog client initialization in /src/lib/posthog.ts
Instructions: Create a new file at /src/lib/posthog.ts. In this file, perform the following steps:
posthog-js
by addingimport posthog from 'posthog-js'
at the top of the file./src/env.js
.NEXT_PUBLIC_ENABLE_ANALYTICS
is set to'true'
andNEXT_PUBLIC_POSTHOG_KEY
is provided.posthog.init()
with the following options:apiKey
: Use theNEXT_PUBLIC_POSTHOG_KEY
environment variable.api_host
: Use theNEXT_PUBLIC_POSTHOG_HOST
environment variable.This setup ensures that PostHog is conditionally initialized based on configuration settings and that user privacy is considered.
Exit Criteria: A new file /src/lib/posthog.ts is created with PostHog client initialized conditionally based on configuration, respecting user privacy, and exporting the posthog instance.
Step 4: Edit
/src/app/layout.tsx
Task: Integrate PostHogProvider into RootLayout in layout.tsx
Instructions: In the /src/app/layout.tsx file, perform the following modifications:
PostHogProvider
fromposthog-js/react
by addingimport { PostHogProvider } from 'posthog-js/react'
at the top of the file./src/lib/posthog.ts
usingimport { posthog } from '~/lib/posthog'
.<ThemeProvider>
component with the<PostHogProvider>
component, passing theposthog
instance as a prop.posthog
is not initialized). ThePostHogProvider
should handle cases where theposthog
instance isundefined
or not initialized.This integration will enable client-side analytics throughout the application without impacting existing functionality.
Exit Criteria: The RootLayout component uses PostHogProvider to wrap the application, and PostHog is integrated into the Next.js application without impacting functionality.