-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #747 Closes: AENG-30 ### Overview This PR integrates dApp with PostHog service to track users activities and analyze it. The PostHog project was created. All dApp activity is now accessible through [the dashboard](https://us.posthog.com/project/104474). The API key and endpoint are available in the project settings. ### Features - dApp integration using [PostHog's official React SDK](https://www.npmjs.com/package/posthog-js) - Autocapture: all user interactions are tracked, any button click, any form fill - Manually triggered `$pageview` capture event: autocapture for page view event was disabled to prevent false positive captures. It is triggered manually as user visits the dApp. - User identification via wallet address - Added custom events with additional payload information: - `deposit_success` - payload: `{ transactionHash }` - `deposit_failure` - payload: `{ cause }` - `withdrawal_success` - wasn't able to easily get any reasonable payload here, can be done later - `withdrawal_failure` - payload: `{ cause }` - `points_claim_success` - payload: `{ claimedAmount, totalAmount }` - `points_claim_failure` - payload: `{ cause }` - Defined feature flag to toggle PostHog integration ### Changes - Added new env variables to define API key and endpoint and a feature flag - Defined preconfigured `PostHogProvider` to inject the service into application - Defined hooks: `usePostHogCapture`, `usePostHogIdentity`, `usePostHogPageViewCapture` - Integrated key actions to capture PostHog custom events (see above) ### Caveats To ensure the `$pageview` event is captured properly it's needed to call it as a side effect as the `location` value changes. To access this value (via `useLocation` hook) the router's context needs to be in scope of PostHog's context. The only way to achieve it was to do it in the `Layout` component where both contexts are available. ### Note It's impossible to exclude captures for development environment. To keep data logs clean and ensure it contains only user's data I suggest to **keep the feature flag disabled** while using development environment.
- Loading branch information
Showing
19 changed files
with
237 additions
and
11 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
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
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
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
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
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,3 @@ | ||
export * from "./usePostHogIdentity" | ||
export * from "./usePostHogCapture" | ||
export * from "./usePostHogPageViewCapture" |
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,40 @@ | ||
import { PostHogEvent } from "#/posthog/events" | ||
import { PostHog, usePostHog } from "posthog-js/react" | ||
import { useCallback } from "react" | ||
|
||
type CaptureArgs = [ | ||
eventName: PostHogEvent, | ||
...rest: Parameters<PostHog["capture"]> extends [unknown, ...infer R] | ||
? R | ||
: never, | ||
] | ||
|
||
export const usePostHogCapture = () => { | ||
const posthog = usePostHog() | ||
|
||
const handleCapture = useCallback( | ||
(...captureArgs: CaptureArgs) => { | ||
posthog.capture(...captureArgs) | ||
}, | ||
[posthog], | ||
) | ||
|
||
const handleCaptureWithCause = useCallback( | ||
(error: unknown, ...captureArgs: CaptureArgs) => { | ||
const [eventName, parameters, ...rest] = captureArgs | ||
|
||
const captureParameters = | ||
error instanceof Error | ||
? { | ||
...parameters, | ||
cause: error.message, | ||
} | ||
: undefined | ||
|
||
handleCapture(eventName, captureParameters, ...rest) | ||
}, | ||
[handleCapture], | ||
) | ||
|
||
return { handleCapture, handleCaptureWithCause } | ||
} |
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,27 @@ | ||
import { PostHog, usePostHog } from "posthog-js/react" | ||
import { useCallback } from "react" | ||
import { sha256, toUtf8Bytes } from "ethers" | ||
|
||
type IdentifyArgs = Parameters<PostHog["identify"]> | ||
|
||
export const usePostHogIdentity = () => { | ||
const posthog = usePostHog() | ||
|
||
const handleIdentification = useCallback( | ||
(...identifyArgs: IdentifyArgs) => { | ||
const [id, ...rest] = identifyArgs | ||
if (!id) return | ||
|
||
const hashedId = sha256(toUtf8Bytes(id.toLowerCase())).slice(2, 12) | ||
|
||
posthog.identify(hashedId, ...rest) | ||
}, | ||
[posthog], | ||
) | ||
|
||
const resetIdentity = useCallback(() => { | ||
posthog.reset() | ||
}, [posthog]) | ||
|
||
return { handleIdentification, resetIdentity } | ||
} |
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,15 @@ | ||
import { PostHogEvent } from "#/posthog/events" | ||
import { useEffect } from "react" | ||
import { useLocation } from "react-router-dom" | ||
import { usePostHogCapture } from "./usePostHogCapture" | ||
|
||
export const usePostHogPageViewCapture = () => { | ||
const { handleCapture } = usePostHogCapture() | ||
const location = useLocation() | ||
|
||
useEffect(() => { | ||
handleCapture(PostHogEvent.PageView) | ||
}, [location, handleCapture]) | ||
|
||
return handleCapture | ||
} |
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
Oops, something went wrong.