-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
identify users in posthog via user id (#1799)
* identify users in posthog via user id * add posthog to frontend.env default file
- Loading branch information
1 parent
87b36b2
commit bcf2f10
Showing
6 changed files
with
129 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
frontends/main/src/components/ConfiguredPostHogProvider/ConfiguredPostHogProvider.tsx
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
...nds/main/src/page-components/ConfiguredPostHogProvider/ConfiguredPostHogProvider.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from "react" | ||
|
||
import { render, waitFor } from "@testing-library/react" | ||
import { PosthogIdenifier } from "./ConfiguredPostHogProvider" | ||
import { QueryClientProvider, QueryClient } from "@tanstack/react-query" | ||
|
||
// mock stuff | ||
import { setMockResponse, urls } from "api/test-utils" | ||
import type { User } from "api/hooks/user" | ||
import { makeUserSettings } from "@/test-utils/factories" | ||
import { usePostHog } from "posthog-js/react" | ||
import type { PostHog } from "posthog-js" | ||
|
||
jest.mock("posthog-js/react", () => { | ||
return { | ||
__esModule: true, | ||
usePostHog: jest.fn(), | ||
} | ||
}) | ||
const mockUsePostHog = jest.mocked(usePostHog) | ||
const posthog: Pick<PostHog, "identify" | "reset" | "get_property"> = { | ||
identify: jest.fn(), | ||
reset: jest.fn(), | ||
get_property: jest.fn(), | ||
} | ||
mockUsePostHog.mockReturnValue(posthog as PostHog) | ||
const mockPosthog = jest.mocked(posthog) | ||
|
||
describe("PosthogIdenifier", () => { | ||
const setup = (user: Partial<User>) => { | ||
const queryClient = new QueryClient() | ||
const userData = makeUserSettings(user) | ||
|
||
setMockResponse.get(urls.userMe.get(), userData) | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<PosthogIdenifier /> | ||
</QueryClientProvider>, | ||
) | ||
return userData | ||
} | ||
test.each([ | ||
{ posthogUserState: "anonymous", resetCalls: 0 }, | ||
{ posthogUserState: "anything_else", resetCalls: 1 }, | ||
])( | ||
"If user is NOT authenticated, calls `reset` if and only if not already anonymous", | ||
async ({ posthogUserState, resetCalls }) => { | ||
setup({ is_authenticated: false }) | ||
mockPosthog.get_property.mockReturnValue(posthogUserState) | ||
await waitFor(() => { | ||
expect(mockPosthog.get_property).toHaveBeenCalledWith("$user_state") | ||
}) | ||
expect(mockPosthog.reset).toHaveBeenCalledTimes(resetCalls) | ||
expect(mockPosthog.identify).not.toHaveBeenCalled() | ||
}, | ||
) | ||
|
||
test("If authenticated, calls `identify` with user id and username", async () => { | ||
const user = setup({ is_authenticated: true }) | ||
await waitFor(() => { | ||
expect(mockPosthog.identify).toHaveBeenCalledWith(String(user.id)) | ||
}) | ||
expect(mockPosthog.reset).not.toHaveBeenCalled() | ||
}) | ||
}) |
57 changes: 57 additions & 0 deletions
57
frontends/main/src/page-components/ConfiguredPostHogProvider/ConfiguredPostHogProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { useEffect } from "react" | ||
import { PostHogProvider, usePostHog } from "posthog-js/react" | ||
import { useUserMe } from "api/hooks/user" | ||
import type { PostHogConfig } from "posthog-js" | ||
|
||
const PosthogIdenifier = () => { | ||
const { data: user } = useUserMe() | ||
const posthog = usePostHog() | ||
/** | ||
* Posthog docs recommend calling `posthog.identify` on signin and | ||
* `posthog.reset` on signout. But signin and signout generally occur on the | ||
* SSO server, which users could get to via other means. | ||
* | ||
* So instead, when page first loads: | ||
* 1. Identify user (noop if user already identified) | ||
* 2. If user is not authenticated AND posthog thinks they are not anonymous, | ||
* then reset their posthog state. | ||
*/ | ||
useEffect(() => { | ||
if (!user) return | ||
const anonymous = posthog.get_property("$user_state") === "anonymous" | ||
if (user.is_authenticated && user.id) { | ||
posthog.identify(String(user.id)) | ||
} else if (!anonymous) { | ||
posthog.reset() | ||
} | ||
}, [user, posthog]) | ||
return null | ||
} | ||
|
||
const ConfiguredPostHogProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const apiKey = process.env.NEXT_PUBLIC_POSTHOG_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: Partial<PostHogConfig> = { | ||
api_host: apiHost, | ||
bootstrap: { | ||
featureFlags: featureFlags, | ||
}, | ||
} | ||
|
||
return apiKey ? ( | ||
<PostHogProvider apiKey={apiKey} options={postHogOptions}> | ||
<PosthogIdenifier /> | ||
{children} | ||
</PostHogProvider> | ||
) : ( | ||
children | ||
) | ||
} | ||
|
||
export default ConfiguredPostHogProvider | ||
export { PosthogIdenifier } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters