-
Notifications
You must be signed in to change notification settings - Fork 3
Adds PostHog back into the NextJS build #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds PostHog back into the NextJS build #1644
Conversation
- Now pull the API key and host from `process.env` - Now pull API host - we weren't doing this before; you probably don't want to specify this, but if you want to self-host Pos tHog (it is an option!) you'd need to be able to specify this. - Roll feature flag processing into the new Webpack Config - Minor update to the LearningResourceDrawer to make the custom `lrd_view` hook there work Going forward, things that use PostHog through the usePostHog hook (or one of their components) should work as expected. (The lrd_view one did some checking to make sure PostHog was turned on, hence it needed a bit more work.)
|
pre-commit.ci run |
ChristopherChudzicki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works great. Two small requests.
frontends/main/next.config.js
Outdated
| new webpack.IgnorePlugin({ | ||
| resourceRegExp: /mockAxios\.ts/, | ||
| }), | ||
| new webpack.EnvironmentPlugin(processFeatureFlags()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid adding to the webpack config unless we really need to, and NextJS has an env key for this... Could processFeatureFlags return the bootstrapFeatureFlags, and then do:
const nextConfig = {
webpack: { ... } // leave as is... I'm pretty sure we can get rid of it entirely, but no need to futz with it here.
env: {
FEATURE_FLAGS: JSON.stringify(bootstrapFeatureFlags)
}
...
}
frontends/main/src/app/providers.tsx
Outdated
| return apiKey.length > 0 ? ( | ||
| <PostHogProvider apiKey={apiKey} options={postHogOptions}> | ||
| {interiorElements} | ||
| </PostHogProvider> | ||
| ) : ( | ||
| interiorElements | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you think of doing something like
const ConfiguredPHProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const apiKey = process.env.NEXT_PUBLIC_POSTHOG_PROJECT_API_KEY || ""
const apiHost =
process.env.NEXT_PUBLIC_POSTHOG_API_HOST || "https://us.i.posthog.com"
const featureFlags = JSON.parse(process.env.FEATURE_FLAGS || "")
const postHogOptions = {
api_host: apiHost,
bootstrap: {
featureFlags: featureFlags,
},
}
if (!apiKey) return children
return (
<PostHogProvider apiKey={apiKey} options={postHogOptions}>
{children}
</PostHogProvider>
)
}?
Then ConfiguredPHProvider can handle the conditional itself + configuration.
Partly suggesting this to encapsulate it, but partly because we could run into the same situation in future with other providers, and as-structured I think it would get awkward with more than 1.
| const apiKey = process.env.NEXT_PUBLIC_POSTHOG_PROJECT_API_KEY | ||
|
|
||
| useEffect(() => { | ||
| if (!apiKey || apiKey.length < 1) return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"" is falsy, so you can just check !apiKey
frontends/main/validateEnv.js
Outdated
| .oneOf(["true", "false"]), | ||
| NEXT_PUBLIC_CSRF_COOKIE_NAME: yup.string().required(), | ||
| NEXT_PUBLIC_POSTHOG_PROJECT_ID: yup.string(), | ||
| NEXT_PUBLIC_POSTHOG_PERSONAL_API_KEY: yup.string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is NEXT_PUBLIC_POSTHOG_PERSONAL_API_KEY used for anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, and actually that shouldn't be in the build since it's the actually secret one. I pulled it (and the project ID; we don't use that either, PostHog figures it out just from the project key).
|
90ad486 addresses the comments. This does pull the PostHog provider stuff into its own component, which makes the |
ChristopherChudzicki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
What are the relevant tickets?
Closes https://github.com/mitodl/hq/issues/5411
Description (What does it do?)
The PostHog integration was not implemented in the NextJS build of Learn. This adds it back:
.envif we want to do that, and customizing the prefix for those flagslrd_vieweventsAny future work that gets pulled in and uses the PostHog hook or components should now "just work" too. (Unless it's like the
lrd_viewcode, which checks to see if PostHog is there or not.)Screenshots (if appropriate):
Components when not enabled:

Components and settings when enabled (this is with a test flag added too):

How can this be tested?
Automated tests should pass. This includes a couple for LearningResourceDrawer that were marked as
skipbecause there wasn't PostHog support yet.Set the required environment variables:
NEXT_PUBLIC_POSTHOG_PROJECT_API_KEY- the PostHog project API keySet the optional environment variables:
NEXT_PUBLIC_POSTHOG_API_HOST- the PostHog API host; defaults tohttps://us.i.posthog.com. Don't change this unless you have your own PostHog instance running.NEXT_PUBLIC_POSTHOG_FEATURE_PREFIX- the prefix to use for feature flags, defaults toFEATURE_NEXT_PUBLIC_FEATURE_some_flag_name- customize the lower case bit, and changeFEATURE_if you set the prefix above; set this to something you'll recognizeWith at least the required variable set, you should see the PostHogProvider show up in the React components list as in the screenshots. Additionally, you should start to see events flowing into PostHog as you start doing things, including
lrd_viewevents (which fire when you open a learning resource drawer).If you've bootstrapped some feature flags, you should see those get populated in the PostHogProvider options. We don't have any configured flags right now - you can manually test this by adding something that's flagged out to an existing page, but it's probably sufficient to see the options passed into the provider correctly.
If you want to test the API host setting, you can maybe point it to something like catch-webhooks to make sure it's trying to hit the right place.