-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from threshold-network/posthog
Posthog This PR adds posthog data tracker. Posthog captures product's usage data to see which users are doing what in your application. We want to run the posthog only in production.
- Loading branch information
Showing
14 changed files
with
165 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { EnvVariable } from "../enums" | ||
import { getEnvVariable } from "../utils/getEnvVariable" | ||
import { ChainID, EnvVariable } from "../enums" | ||
import { getEnvVariable, supportedChainId } from "../utils/getEnvVariable" | ||
|
||
export const TBTC_V2 = | ||
getEnvVariable(EnvVariable.FEATURE_FLAG_TBTC_V2) === "true" | ||
export const MULTI_APP_STAKING = | ||
getEnvVariable(EnvVariable.FEATURE_FLAG_MULTI_APP_STAKING) === "true" | ||
export const POSTHOG = | ||
getEnvVariable(EnvVariable.FEATURE_FLAG_POSTHOG) === "true" && | ||
supportedChainId === ChainID.Ethereum.toString() |
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 @@ | ||
export * from "./usePosthog" |
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,12 @@ | ||
import { useEffect } from "react" | ||
import { useLocation } from "react-router-dom" | ||
import * as posthog from "../../posthog" | ||
import { featureFlags } from "../../constants" | ||
|
||
export const useCapturePageview = () => { | ||
const location = useLocation() | ||
|
||
useEffect(() => { | ||
if (featureFlags.POSTHOG) posthog.capturePageview() | ||
}, [location]) | ||
} |
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,50 @@ | ||
import { useEffect } from "react" | ||
import { useWeb3React } from "@web3-react/core" | ||
import { ConnectorEvent, ConnectorUpdate } from "@web3-react/types" | ||
import * as posthog from "../../posthog" | ||
import { getAddress, isSameETHAddress } from "../../web3/utils" | ||
import { featureFlags } from "../../constants" | ||
|
||
export const useIdentify = () => { | ||
const { connector, account } = useWeb3React() | ||
|
||
useEffect(() => { | ||
if (!featureFlags.POSTHOG) return | ||
|
||
const onLogin = async () => { | ||
const account = await connector?.getAccount() | ||
if (account) { | ||
posthog.identify(getAddress(account)) | ||
} | ||
} | ||
onLogin() | ||
}, [connector]) | ||
|
||
useEffect(() => { | ||
if (!featureFlags.POSTHOG) return | ||
|
||
const updateHandler = (update: ConnectorUpdate) => { | ||
if (!update.account) { | ||
posthog.reset() | ||
} else if ( | ||
update.account && | ||
account && | ||
!isSameETHAddress(update.account, account) | ||
) { | ||
posthog.reset() | ||
posthog.identify(getAddress(update.account)) | ||
} | ||
} | ||
|
||
const deactivateHandler = () => { | ||
posthog.reset() | ||
} | ||
|
||
connector?.on(ConnectorEvent.Update, updateHandler) | ||
connector?.on(ConnectorEvent.Deactivate, deactivateHandler) | ||
return () => { | ||
connector?.removeListener(ConnectorEvent.Update, updateHandler) | ||
connector?.removeListener(ConnectorEvent.Deactivate, deactivateHandler) | ||
} | ||
}, [connector, account]) | ||
} |
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,14 @@ | ||
import { useEffect } from "react" | ||
import { featureFlags } from "../../constants" | ||
import * as posthog from "../../posthog" | ||
import { useCapturePageview } from "./useCapturePageview" | ||
import { useIdentify } from "./useIdentify" | ||
|
||
export const usePosthog = () => { | ||
useEffect(() => { | ||
if (featureFlags.POSTHOG) posthog.init() | ||
}, []) | ||
|
||
useCapturePageview() | ||
useIdentify() | ||
} |
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,37 @@ | ||
import posthog from "posthog-js" | ||
import { EnvVariable } from "../enums" | ||
import { getEnvVariable } from "../utils/getEnvVariable" | ||
|
||
export const init = () => { | ||
const apiKey = getEnvVariable(EnvVariable.POSTHOG_API_KEY) | ||
const apiHost = getEnvVariable(EnvVariable.POSTHOG_HOSTNAME_HTTP) | ||
|
||
if (!apiKey || !apiHost) { | ||
throw new Error("Could not initilize posthog.") | ||
} | ||
|
||
posthog.init(apiKey, { | ||
api_host: apiHost, | ||
// T dapp is a single page app so we need to make a `pageview` call | ||
// manually. It also prevents sending the `pageview` event on postohog | ||
// initialization. | ||
capture_pageview: false, | ||
persistence: "memory", | ||
}) | ||
} | ||
|
||
export const identify = (ethAddress: string) => { | ||
posthog.identify(ethAddress) | ||
} | ||
|
||
// Posthog automatically sends pageview events whenever it gets loaded. For a | ||
// one-page app, this means it will only send a `pageview` once, when the app | ||
// loads. To make sure any navigating a user does within your app gets captured, | ||
// you must make a `pageview` call manually. | ||
export const capturePageview = () => { | ||
posthog.capture("$pageview") | ||
} | ||
|
||
export const reset = () => { | ||
posthog.reset() | ||
} |
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 |
---|---|---|
|
@@ -3836,6 +3836,11 @@ | |
estree-walker "^1.0.1" | ||
picomatch "^2.2.2" | ||
|
||
"@sentry/types@^7.2.0": | ||
version "7.14.0" | ||
resolved "https://registry.npmjs.org/@sentry/types/-/types-7.14.0.tgz#8069e58a0104190e328647963036c7597e2e5fa8" | ||
integrity sha512-9iFZS9Hr5hAoL+M9oUH2dY9burOaQh+CHGH66fortuTp++YDWKdbPEeKcz8hRJaUyBBn53rdxiBmAyHsrlE6KA== | ||
|
||
"@sinclair/typebox@^0.23.3": | ||
version "0.23.5" | ||
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.23.5.tgz#93f7b9f4e3285a7a9ade7557d9a8d36809cbc47d" | ||
|
@@ -12193,6 +12198,11 @@ fetch-retry@^5.0.2: | |
resolved "https://registry.yarnpkg.com/fetch-retry/-/fetch-retry-5.0.2.tgz#4c55663a7c056cb45f182394e479464f0ff8f3e3" | ||
integrity sha512-57Hmu+1kc6pKFUGVIobT7qw3NeAzY/uNN26bSevERLVvf6VGFR/ooDCOFBHMNDgAxBiU2YJq1D0vFzc6U1DcPw== | ||
|
||
fflate@^0.4.1: | ||
version "0.4.8" | ||
resolved "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae" | ||
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA== | ||
|
||
figgy-pudding@^3.5.1: | ||
version "3.5.2" | ||
resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.2.tgz#b4eee8148abb01dcf1d1ac34367d59e12fa61d6e" | ||
|
@@ -18496,6 +18506,15 @@ postcss@^8.1.0: | |
picocolors "^1.0.0" | ||
source-map-js "^1.0.2" | ||
|
||
posthog-js@^1.32.2: | ||
version "1.32.2" | ||
resolved "https://registry.npmjs.org/posthog-js/-/posthog-js-1.32.2.tgz#faf8e88946e448ccfd6f673d1cce4512dd7568d9" | ||
integrity sha512-1jepbvxUCGJW7pxsiPeioct9+JZJKPur6j0M2+pe2WFmNnkBGlV7qbK/evv1k6jjYpG8SxDZqwU0RAseRyh7rA== | ||
dependencies: | ||
"@sentry/types" "^7.2.0" | ||
fflate "^0.4.1" | ||
rrweb-snapshot "^1.1.14" | ||
|
||
[email protected]: | ||
version "10.4.1" | ||
resolved "https://registry.yarnpkg.com/preact/-/preact-10.4.1.tgz#9b3ba020547673a231c6cf16f0fbaef0e8863431" | ||
|
@@ -20004,6 +20023,11 @@ rollup@^1.31.1: | |
"@types/node" "*" | ||
acorn "^7.1.0" | ||
|
||
rrweb-snapshot@^1.1.14: | ||
version "1.1.14" | ||
resolved "https://registry.npmjs.org/rrweb-snapshot/-/rrweb-snapshot-1.1.14.tgz#9d4d9be54a28a893373428ee4393ec7e5bd83fcc" | ||
integrity sha512-eP5pirNjP5+GewQfcOQY4uBiDnpqxNRc65yKPW0eSoU1XamDfc4M8oqpXGMyUyvLyxFDB0q0+DChuxxiU2FXBQ== | ||
|
||
rsvp@^4.8.4: | ||
version "4.8.5" | ||
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734" | ||
|