Skip to content

Commit

Permalink
feat(workers): posthog env vars and additional events capture (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
poolsar42 authored Jul 12, 2023
1 parent d5481c5 commit cbe6ef5
Show file tree
Hide file tree
Showing 31 changed files with 102 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main-address.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
SECRET_ZERODEV_PROJECTID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
NODE_ENV: 'development'
INTERNAL_GOOGLE_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_GOOGLE_OAUTH_CLIENT_ID_DEV }}
Expand All @@ -77,4 +77,4 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_DEV }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_DEV }}
SECRET_ZERODEV_PROJECTID: ${{secrets.SECRET_ZERODEV_PROJECTID_DEV }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_DEV }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_DEV }}
4 changes: 2 additions & 2 deletions .github/workflows/main-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
STRIPE_API_SECRET
STRIPE_WEBHOOK_SECRET
STRIPE_PRO_PLAN_ID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
NODE_ENV: 'development'
# A secret used for session encryption.
Expand All @@ -66,7 +66,7 @@ jobs:
STRIPE_API_SECRET: ${{ secrets.STRIPE_API_SECRET_DEV }}
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET_DEV }}
STRIPE_PRO_PLAN_ID: ${{ secrets.STRIPE_PRO_PLAN_ID_DEV }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_DEV }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_DEV }}

- name: Setup Playwright
working-directory: 'apps/console'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main-passport.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
NODE_ENV: 'development'
SECRET_SESSION_KEY: ${{ secrets.SECRET_SESSION_KEY_DEV }}
Expand All @@ -83,7 +83,7 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_APPLE_OAUTH_CLIENT_SECRET_DEV }}
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_DEV }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_DEV }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_DEV }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_DEV }}

- name: Setup Playwright
run: yarn playwright install --with-deps
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/main-starbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ jobs:
INTERNAL_CLOUDFLARE_ZONE_ID
TOKEN_CLOUDFLARE_API
INTERNAL_DKIM_SELECTOR
SECRET_POSTHOG_API_KEY
env:
NODE_ENV: 'development'
INTERNAL_CLOUDFLARE_ZONE_ID: ${{ secrets.INTERNAL_CLOUDFLARE_ZONE_ID }}
TOKEN_CLOUDFLARE_API: ${{ secrets.TOKEN_CLOUDFLARE_API }}
INTERNAL_DKIM_SELECTOR: ${{ secrets.INTERNAL_DKIM_SELECTOR }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_DEV }}
4 changes: 2 additions & 2 deletions .github/workflows/next-address.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
SECRET_ZERODEV_PROJECTID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
INTERNAL_GOOGLE_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_GOOGLE_OAUTH_CLIENT_ID_TEST }}
SECRET_GOOGLE_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_GOOGLE_OAUTH_CLIENT_SECRET_TEST }}
Expand All @@ -76,4 +76,4 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_TEST }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_TEST }}
SECRET_ZERODEV_PROJECTID: ${{secrets.SECRET_ZERODEV_PROJECTID_TEST }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_TEST }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_TEST }}
4 changes: 2 additions & 2 deletions .github/workflows/next-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ jobs:
STRIPE_API_SECRET
STRIPE_WEBHOOK_SECRET
STRIPE_PRO_PLAN_ID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
# A secret used for session encryption.
SECRET_SESSION_KEY: ${{ secrets.SECRET_SESSION_KEY_TEST }}
SECRET_SESSION_SALT: ${{ secrets.SECRET_SESSION_SALT_TEST }}
STRIPE_API_SECRET: ${{ secrets.STRIPE_API_SECRET_TEST }}
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET_TEST }}
STRIPE_PRO_PLAN_ID: ${{ secrets.STRIPE_PRO_PLAN_ID_TEST }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_TEST }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_TEST }}
4 changes: 2 additions & 2 deletions .github/workflows/next-passport.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
SECRET_SESSION_KEY: ${{ secrets.SECRET_SESSION_KEY_TEST }}
SECRET_SESSION_SALT: ${{ secrets.SECRET_SESSION_SALT_TEST }}
Expand All @@ -80,4 +80,4 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_APPLE_OAUTH_CLIENT_SECRET_TEST }}
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_TEST }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_TEST }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_TEST }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_TEST }}
2 changes: 2 additions & 0 deletions .github/workflows/next-starbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ jobs:
INTERNAL_CLOUDFLARE_ZONE_ID
TOKEN_CLOUDFLARE_API
INTERNAL_DKIM_SELECTOR
SECRET_POSTHOG_API_KEY
env:
INTERNAL_CLOUDFLARE_ZONE_ID: ${{ secrets.INTERNAL_CLOUDFLARE_ZONE_ID }}
TOKEN_CLOUDFLARE_API: ${{ secrets.TOKEN_CLOUDFLARE_API }}
INTERNAL_DKIM_SELECTOR: ${{ secrets.INTERNAL_DKIM_SELECTOR }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_TEST }}
4 changes: 2 additions & 2 deletions .github/workflows/release-address.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
SECRET_ZERODEV_PROJECTID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
INTERNAL_GOOGLE_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_GOOGLE_OAUTH_CLIENT_ID_PROD }}
SECRET_GOOGLE_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_GOOGLE_OAUTH_CLIENT_SECRET_PROD }}
Expand All @@ -75,4 +75,4 @@ jobs:
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_PROD }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_PROD }}
SECRET_ZERODEV_PROJECTID: ${{secrets.SECRET_ZERODEV_PROJECTID_PROD }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_PROD }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_PROD }}
4 changes: 2 additions & 2 deletions .github/workflows/release-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ jobs:
STRIPE_API_SECRET
STRIPE_WEBHOOK_SECRET
STRIPE_PRO_PLAN_ID
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
# A secret used for session encryption.
SECRET_SESSION_KEY: ${{ secrets.SECRET_SESSION_KEY_PROD }}
SECRET_SESSION_SALT: ${{ secrets.SECRET_SESSION_SALT_PROD }}
STRIPE_API_SECRET: ${{ secrets.STRIPE_API_SECRET_PROD }}
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET_PROD }}
STRIPE_PRO_PLAN_ID: ${{ secrets.STRIPE_PRO_PLAN_ID_PROD }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_PROD }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_PROD }}
4 changes: 2 additions & 2 deletions .github/workflows/release-passport.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET
INTERNAL_DISCORD_OAUTH_CLIENT_ID
SECRET_DISCORD_OAUTH_CLIENT_SECRET
POSTHOG_API_KEY
SECRET_POSTHOG_API_KEY
env:
SECRET_SESSION_KEY: ${{ secrets.SECRET_SESSION_KEY_PROD }}
SECRET_SESSION_SALT: ${{ secrets.SECRET_SESSION_SALT_PROD }}
Expand All @@ -76,4 +76,4 @@ jobs:
SECRET_APPLE_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_APPLE_OAUTH_CLIENT_SECRET_PROD }}
INTERNAL_DISCORD_OAUTH_CLIENT_ID: ${{ vars.INTERNAL_DISCORD_OAUTH_CLIENT_ID_PROD }}
SECRET_DISCORD_OAUTH_CLIENT_SECRET: ${{ secrets.SECRET_DISCORD_OAUTH_CLIENT_SECRET_PROD }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY_PROD }}
SECRET_POSTHOG_API_KEY: ${{ secrets.SECRET_POSTHOG_API_KEY_PROD }}
2 changes: 2 additions & 0 deletions .github/workflows/release-starbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ jobs:
INTERNAL_CLOUDFLARE_ZONE_ID
TOKEN_CLOUDFLARE_API
INTERNAL_DKIM_SELECTOR
SECRET_POSTHOG_API_KEY_TEST
env:
NODE_ENV: 'development'
INTERNAL_CLOUDFLARE_ZONE_ID: ${{ secrets.INTERNAL_CLOUDFLARE_ZONE_ID }}
TOKEN_CLOUDFLARE_API: ${{ secrets.TOKEN_CLOUDFLARE_API }}
INTERNAL_DKIM_SELECTOR: ${{ secrets.INTERNAL_DKIM_SELECTOR }}
SECRET_POSTHOG_API_KEY_TEST: ${{ secrets.SECRET_POSTHOG_API_KEY_PROD }}
2 changes: 1 addition & 1 deletion apps/console/.dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ STRIPE_API_SECRET = ""
STRIPE_WEBHOOK_SECRET = ""
STRIPE_PRO_PLAN_ID = ""

POSTHOG_API_KEY=""
SECRET_POSTHOG_API_KEY=""
6 changes: 3 additions & 3 deletions apps/console/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export type LoaderData = {
PASSPORT_URL: string
displayName: string
ENV: {
POSTHOG_API_KEY: string
SECRET_POSTHOG_API_KEY: string
INTERNAL_GOOGLE_ANALYTICS_TAG: string
REMIX_DEV_SERVER_WS_PORT?: number
WALLET_CONNECT_PROJECT_ID: string
Expand Down Expand Up @@ -155,7 +155,7 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
avatarUrl,
PASSPORT_URL,
ENV: {
POSTHOG_API_KEY,
SECRET_POSTHOG_API_KEY,
INTERNAL_GOOGLE_ANALYTICS_TAG,
REMIX_DEV_SERVER_WS_PORT:
process.env.NODE_ENV === 'development'
Expand Down Expand Up @@ -198,7 +198,7 @@ export default function App() {
// https://posthog.com/docs/libraries/react#posthog-provider
if (hydrated) {
try {
posthog.init(loaderData.ENV.POSTHOG_API_KEY, {
posthog.init(loaderData.ENV.SECRET_POSTHOG_API_KEY, {
api_host: POSTHOG_PROXY_HOST,
})

Expand Down
19 changes: 2 additions & 17 deletions apps/console/app/routes/apps/$clientId/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import type { notificationHandlerType } from '~/types'
import { SCOPE_SMART_CONTRACT_WALLETS } from '@proofzero/security/scopes'
import { BadRequestError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { usePostHog } from 'posthog-js/react'
import { type AccountURN } from '@proofzero/urns/account'

/**
* @file app/routes/dashboard/index.tsx
Expand Down Expand Up @@ -236,22 +234,15 @@ export default function AppDetailIndexPage() {
const submit = useSubmit()
const actionData = useActionData()
const outletContextData = useOutletContext<{
accountURN: AccountURN
notificationHandler: notificationHandlerType
appDetails: appDetailsProps
rotationResult: any
paymaster: PaymasterType
appContactAddress?: AddressURN
}>()
const {
appContactAddress,
paymaster,
notificationHandler,
appDetails,
accountURN,
} = outletContextData
const { appContactAddress, paymaster, notificationHandler, appDetails } =
outletContextData
const { scopeMeta }: { scopeMeta: ScopeMeta } = useLoaderData()
const posthog = usePostHog()

const ref = useRef(null)
const [multiselectComponentWidth, setMultiselectComponentWidth] = useState(0)
Expand Down Expand Up @@ -290,12 +281,6 @@ export default function AppDetailIndexPage() {

useEffect(() => {
if (actionData?.updatedApp) Object.assign(appDetails, actionData.updatedapp)
if (actionData?.published) {
posthog?.capture('app_published', {
distinct_id: appDetails.clientId,
account_urn: accountURN,
})
}
}, [actionData])

const errors = actionData?.errors
Expand Down
17 changes: 16 additions & 1 deletion apps/console/app/routes/apps/delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { redirect } from '@remix-run/cloudflare'
import type { ActionFunction } from '@remix-run/cloudflare'
import createStarbaseClient from '@proofzero/platform-clients/starbase'
import { requireJWT } from '~/utilities/session.server'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import {
getAuthzHeaderConditionallyFromToken,
parseJwt,
} from '@proofzero/utils'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import {
JsonError,
getErrorCause,
getRollupReqFunctionErrorWrapper,
} from '@proofzero/utils/errors'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { posthogCall } from '@proofzero/utils/posthog'
import { type AccountURN } from '@proofzero/urns/account'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
Expand All @@ -20,13 +25,23 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
throw new BadRequestError({ message: 'Client ID is required' })

const jwt = await requireJWT(request)
const parsedJwt = parseJwt(jwt as string)
const accountURN = parsedJwt.sub as AccountURN

const starbaseClient = createStarbaseClient(Starbase, {
...getAuthzHeaderConditionallyFromToken(jwt),
...generateTraceContextHeaders(context.traceSpan),
})
try {
await starbaseClient.deleteApp.mutate({ clientId })
await posthogCall({
apiKey: SECRET_POSTHOG_API_KEY,
eventName: 'app_deleted',
distinctId: accountURN,
properties: {
client_id: clientId,
},
})
return redirect('/')
} catch (error) {
const cause = getErrorCause(error)
Expand Down
2 changes: 1 addition & 1 deletion apps/console/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare global {
const STRIPE_WEBHOOK_SECRET: string
const STRIPE_PRO_PLAN_ID: string

const POSTHOG_API_KEY: string
const SECRET_POSTHOG_API_KEY: string

const CONSOLE_URL: string
}
2 changes: 1 addition & 1 deletion apps/passport/.dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ SECRET_APPLE_OAUTH_CLIENT_SECRET=""
INTERNAL_DISCORD_OAUTH_CLIENT_ID=""
SECRET_DISCORD_OAUTH_CLIENT_SECRET=""

POSTHOG_API_KEY=""
SECRET_POSTHOG_API_KEY=""
4 changes: 2 additions & 2 deletions apps/passport/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
appProps,
flashes,
ENV: {
POSTHOG_API_KEY: context.env.POSTHOG_API_KEY,
SECRET_POSTHOG_API_KEY: context.env.SECRET_POSTHOG_API_KEY,
PROFILE_APP_URL: context.env.PROFILE_APP_URL,
INTERNAL_GOOGLE_ANALYTICS_TAG:
context.env.INTERNAL_GOOGLE_ANALYTICS_TAG,
Expand Down Expand Up @@ -224,7 +224,7 @@ export default function App() {
// https://posthog.com/docs/libraries/react#posthog-provider
if (hydrated) {
try {
posthog.init(browserEnv.ENV.POSTHOG_API_KEY, {
posthog.init(browserEnv.ENV.SECRET_POSTHOG_API_KEY, {
api_host: POSTHOG_PROXY_HOST,
})
posthog?.reset()
Expand Down
13 changes: 12 additions & 1 deletion apps/passport/app/routes/settings/accounts/rename.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
getValidatedSessionContext,
} from '~/session.server'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { posthogCall } from '@proofzero/utils/posthog'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
await getValidatedSessionContext(
const { accountUrn } = await getValidatedSessionContext(
request,
getDefaultAuthzParams(request),
context.env,
Expand All @@ -26,6 +27,16 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
nickname: name,
})

await posthogCall({
apiKey: context.env.SECRET_POSTHOG_API_KEY,
distinctId: accountUrn,
eventName: 'address_renamed',
properties: {
addressId: id,
nickname: name,
},
})

return null
}
)
2 changes: 1 addition & 1 deletion apps/passport/app/routes/settings/advanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
}

await posthogCall({
apiKey: context.env.POSTHOG_API_KEY,
apiKey: context.env.SECRET_POSTHOG_API_KEY,
eventName: 'delete_rollup_identity',
distinctId: accountUrn,
})
Expand Down
Loading

0 comments on commit cbe6ef5

Please sign in to comment.