Skip to content

Commit

Permalink
🪟 🔧 Add PostHog A/B Testing tool
Browse files Browse the repository at this point in the history
Posthog is an analytics tool that will allow us to run A/B tests in
product in a simple manner.

The advantage vs LaunchDarkly is that they provide features for
experimenting, setting goals, create funnels, etc. Thanks to their UI, we can define experiments and
monitor which variant is the winner. Posthog will do all the maths for
us.
More info: https://posthog.com/product/experimentation-suite

This addition needs to be approved by @timroes after a security audit
has been performed.

This PR sets all the code needed in case the audit is succesful
  • Loading branch information
letiescanciano committed Sep 30, 2022
1 parent 7ab162a commit 5cf55ae
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 10 deletions.
1 change: 1 addition & 0 deletions airbyte-webapp/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ REACT_APP_FULL_STORY_ORG=13AXQ4
REACT_APP_SENTRY_DSN=
REACT_APP_INTERCOM_APP_ID=nj1oam7s
REACT_APP_OSANO=16A0CTTE7vE8m1Qif/cff33c53-d6e6-45af-8a66-76be4c5ac568
REACT_APP_POSTHOG=phc_6nrn7nfNbiHszdF5zntXhNwgshvD7s3lvIxWAmhQvtg
56 changes: 56 additions & 0 deletions airbyte-webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions airbyte-webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"launchdarkly-js-client-sdk": "^2.22.1",
"lodash": "^4.17.21",
"mdast": "^3.0.0",
"posthog-js": "^1.32.0",
"query-string": "^6.13.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
1 change: 1 addition & 0 deletions airbyte-webapp/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface Config {
version?: string;
integrationUrl: string;
launchDarkly?: string;
postHog?: string;
}

export type DeepPartial<T> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import posthog, { PostHog } from "posthog-js";
import { useContext, useEffect } from "react";
import React from "react";

import { useConfig } from "config";

interface Context {
posthog: PostHog;
featureFlagsVariants?: Record<string, boolean | string>;
}
const postHogServiceContext = React.createContext<Context | null>(null);
const PostHogServiceProvider = postHogServiceContext.Provider;

export const PostHogProvider = ({ children }: { children: React.ReactNode }) => {
const { postHog: posthogKey } = useConfig();

useEffect(() => {
if (posthogKey) {
posthog.init(posthogKey, { api_host: "https://app.posthog.com" });
}
}, []);

if (posthogKey) {
return <PostHogServiceProvider value={{ posthog }}>{children}</PostHogServiceProvider>;
}

return <>{children}</>;
};

export const usePostHog = () => {
const posthogContext = useContext(postHogServiceContext);

if (!posthogContext) {
throw new Error("posthogContext must be used within a PostHogProvider.");
}

return posthogContext;
};
23 changes: 13 additions & 10 deletions airbyte-webapp/src/packages/cloud/cloudRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useAnalyticsIdentifyUser, useAnalyticsRegisterValues } from "hooks/serv
import { FeatureItem, FeatureSet, useFeatureService } from "hooks/services/Feature";
import { useApiHealthPoll } from "hooks/services/Health";
import { OnboardingServiceProvider } from "hooks/services/Onboarding";
import { PostHogProvider } from "hooks/services/PostHogExperiment/PostHogExperimentService";
import { useQuery } from "hooks/useQuery";
import { useAuthService } from "packages/cloud/services/auth/AuthService";
import { useIntercom } from "packages/cloud/services/thirdParty/intercom/useIntercom";
Expand Down Expand Up @@ -163,16 +164,18 @@ export const Routing: React.FC = () => {
return (
<WorkspaceServiceProvider>
<LDExperimentServiceProvider>
<Suspense fallback={<LoadingPage />}>
{/* Allow email verification no matter whether the user is logged in or not */}
<Routes>
<Route path={CloudRoutes.FirebaseAction} element={<VerifyEmailAction />} />
</Routes>
{/* Show the login screen if the user is not logged in */}
{!user && <Auth />}
{/* Allow all regular routes if the user is logged in */}
{user && <MainViewRoutes />}
</Suspense>
<PostHogProvider>
<Suspense fallback={<LoadingPage />}>
{/* Allow email verification no matter whether the user is logged in or not */}
<Routes>
<Route path={CloudRoutes.FirebaseAction} element={<VerifyEmailAction />} />
</Routes>
{/* Show the login screen if the user is not logged in */}
{!user && <Auth />}
{/* Allow all regular routes if the user is logged in */}
{user && <MainViewRoutes />}
</Suspense>
</PostHogProvider>
</LDExperimentServiceProvider>
</WorkspaceServiceProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const cloudEnvConfigProvider: ConfigProvider<CloudConfig> = async () => {
appId: process.env.REACT_APP_INTERCOM_APP_ID,
},
launchDarkly: process.env.REACT_APP_LAUNCHDARKLY_KEY,
postHog: process.env.REACT_APP_POSTHOG,
};
};

Expand Down

0 comments on commit 5cf55ae

Please sign in to comment.