Skip to content

Commit

Permalink
feature: add posthog
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos committed Sep 19, 2024
1 parent 8d31450 commit f1a1965
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 42 deletions.
2 changes: 2 additions & 0 deletions apps/infra/Pulumi.core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ config:
secure: v1:/+DMPM/zWsNsNHDg:JIlxC1v0ocfdfX1TsDXXTZClL8Uza74CMq5YSst7Ymu+CPulRNgUejrSQEFVW3F6BkXMA1QU3s4=
infra:MAILER_API_KEY:
secure: v1:Br02wGwX0UOAZiNp:wIzQFfWCzWygBRCVJNDs/ZP65EXEAIFhduPOOniN6EOeXPyJ4UHjOefPyO2bhOBkR5E3CWLmQBsVjIGrGwAHZYZCQg==
infra:NEXT_PUBLIC_POSTHOG_KEY:
secure: v1:cI6gi7pKYG9cQJDk:FYV6+v87vWFw+ikVdYQNqr3vY4vWpsB4D1YG5u1GowcTW7yrxOSa12HXPsWRkw8IIFEhqpXiaG00AF/G2pYm
infra:SENTRY_DSN:
secure: v1:TFAocHKQe31DxpAX:8+iplSLckN3i91KshS+8mab3s158nFkNyWD8JBZM8Ojtu2zstpna5N50ugkXuH9hdcuUv4db088pI9Dkvuc/84OJDEm47z/0bJgD803f4ZWqVtA7Yt8Z2pyTSGqGzsj9RErstG0N
infra:SENTRY_ORG:
Expand Down
5 changes: 5 additions & 0 deletions apps/infra/src/core/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const defaultProviderApiKey = createSecretWithVersion(
'DEFAULT_PROVIDER_API_KEY',
'Default provider API key',
)
const postHogApiKey = createSecretWithVersion(
'NEXT_PUBLIC_POSTHOG_KEY',
'Posthog API Key for product analytics',
)

export const mailerApiKeyArn = mailerApiKey.arn
export const sentryDsnArn = sentryDsn.arn
Expand All @@ -78,3 +82,4 @@ export const websocketsSecretRefreshTokenArn = websocketsSecretRefreshToken.arn
export const workersWebsocketsSecretTokenArn = workersWebsocketsSecretToken.arn
export const defaultProjectIdArn = defaultProjectId.arn
export const defaultProviderApiKeyArn = defaultProviderApiKey.arn
export const postHogApiKeyArn = postHogApiKey.arn
5 changes: 5 additions & 0 deletions apps/infra/src/deployments/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const defaultProjectIdArn = coreStack.requireOutput('defaultProjectIdArn')
const defaultProviderApiKeyArn = coreStack.requireOutput(
'defaultProviderApiKeyArn',
)
const postHogApiKeyArn = coreStack.requireOutput('postHogApiKeyArn')

const getSecretString = (arn: pulumi.Output<any>) => {
return arn.apply((secretId) =>
Expand All @@ -52,6 +53,7 @@ const workersWebsocketsSecretToken = getSecretString(
workersWebsocketsSecretTokenArn,
)
const defaultProjectId = getSecretString(defaultProjectIdArn)
const postHogApiKey = getSecretString(postHogApiKeyArn)

export const sentryDsn = getSecretString(sentryDsnArn)
export const sentryOrg = getSecretString(sentryOrgArn)
Expand All @@ -72,6 +74,7 @@ export const environment = pulumi
sentryProject,
defaultProjectId,
defaultProviderApiKey,
postHogApiKey,
])
.apply(() => {
return [
Expand Down Expand Up @@ -110,5 +113,7 @@ export const environment = pulumi
{ name: 'AWS_ACCESS_SECRET', value: awsAccessSecret },
{ name: 'DEFAULT_PROJECT_ID', value: defaultProjectId },
{ name: 'DEFAULT_PROVIDER_API_KEY', value: defaultProviderApiKey },
{ name: 'NEXT_PUBLIC_POSTHOG_KEY', value: postHogApiKey },
{ name: 'NEXT_PUBLIC_POSTHOG_HOST', value: 'https://eu.i.posthog.com' },
]
})
2 changes: 0 additions & 2 deletions apps/web/env.example

This file was deleted.

11 changes: 6 additions & 5 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"test:watch": "vitest"
},
"dependencies": {
"yaml": "^2.4.5",
"@latitude-data/compiler": "workspace:^",
"@latitude-data/core": "workspace:*",
"@latitude-data/env": "workspace:^",
Expand Down Expand Up @@ -43,11 +42,13 @@
"next-themes": "^0.3.0",
"nextjs-toploader": "^1.6.12",
"nprogress": "^0.2.0",
"posthog-js": "^1.161.6",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"socket.io-react-hook": "^2.4.5",
"swr": "^2.2.5",
"use-debounce": "^10.0.1",
"yaml": "^2.4.5",
"zod": "^3.23.8",
"zsa": "^0.5.1",
"zsa-react": "^0.2.2"
Expand All @@ -57,6 +58,9 @@
"@latitude-data/eslint-config": "workspace:*",
"@latitude-data/typescript-config": "workspace:*",
"@next/eslint-plugin-next": "^14.2.4",
"@testing-library/dom": "^10.3.2",
"@testing-library/react": "^16.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/js-cookie": "^3.0.6",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.14.10",
Expand All @@ -68,10 +72,7 @@
"node-mocks-http": "^1.15.0",
"postcss": "^8.4.39",
"tailwindcss": "^3.4.4",
"vitest": "^2.0.3",
"@testing-library/dom": "^10.3.2",
"@testing-library/react": "^16.0.0",
"@testing-library/react-hooks": "^8.0.1"
"vitest": "^2.0.3"
},
"optionalDependencies": {
"svelte": "^4.2.19",
Expand Down
18 changes: 11 additions & 7 deletions apps/web/src/app/(private)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { getSession } from '$/services/auth/getSession'
import { ROUTES } from '$/services/routes'
import { redirect } from 'next/navigation'

import { CSPostHogProvider } from '../providers'

export default async function PrivateLayout({
children,
}: Readonly<{
Expand All @@ -22,12 +24,14 @@ export default async function PrivateLayout({
const { workspace, user } = await getCurrentUser()

return (
<SocketIOProvider>
<SessionProvider currentUser={user} workspace={workspace}>
<LatitudeWebsocketsProvider socketServer={env.WEBSOCKETS_SERVER}>
{children}
</LatitudeWebsocketsProvider>
</SessionProvider>
</SocketIOProvider>
<CSPostHogProvider>
<SocketIOProvider>
<SessionProvider currentUser={user} workspace={workspace}>
<LatitudeWebsocketsProvider socketServer={env.WEBSOCKETS_SERVER}>
{children}
</LatitudeWebsocketsProvider>
</SessionProvider>
</SocketIOProvider>
</CSPostHogProvider>
)
}
17 changes: 17 additions & 0 deletions apps/web/src/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client'

import { ReactNode } from 'react'

import { envClient } from '$/envClient'
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'

if (typeof window !== 'undefined') {
posthog.init(envClient.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: envClient.NEXT_PUBLIC_POSTHOG_HOST,
person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well
})
}
export function CSPostHogProvider({ children }: { children: ReactNode }) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
}
2 changes: 1 addition & 1 deletion apps/web/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default createEnv({
NODE_ENV: z.string(),
DATABASE_URL: z.string(),
GATEWAY_HOSTNAME: z.string(),
GATEWAY_PORT: z.coerce.number().optional(),
WEBSOCKETS_SERVER: z.string(),
GATEWAY_PORT: z.coerce.number().optional(),
GATEWAY_SSL: z
.enum(['true', 'false'])
.transform((value) => value === 'true')
Expand Down
13 changes: 13 additions & 0 deletions apps/web/src/envClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createEnv } from '@t3-oss/env-nextjs'
import { z } from 'zod'

export const envClient = createEnv({
client: {
NEXT_PUBLIC_POSTHOG_KEY: z.string(),
NEXT_PUBLIC_POSTHOG_HOST: z.string(),
},
runtimeEnv: {
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST,
},
})
2 changes: 1 addition & 1 deletion packages/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const env = createEnv({
WEBSOCKET_REFRESH_SECRET_TOKEN_KEY: z.string(),
WEBSOCKET_SECRET_TOKEN_KEY: z.string(),
WORKERS_WEBSOCKET_SECRET_TOKEN: z.string(),
DEFAULT_PROVIDER_ID: z.string(),
AWS_ACCESS_KEY: z.string().optional(),
AWS_ACCESS_SECRET: z.string().optional(),
AWS_REGION: z.string().optional(),
Expand All @@ -79,7 +80,6 @@ export const env = createEnv({
SENTRY_DSN: z.string().optional(),
SENTRY_ORG: z.string().optional(),
SENTRY_PROJECT: z.string().optional(),
DEFAULT_PROVIDER_ID: z.string(),
},
runtimeEnv: {
...process.env,
Expand Down
Loading

0 comments on commit f1a1965

Please sign in to comment.