Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Feature/posthog backend (#243)
Browse files Browse the repository at this point in the history
* Create param validator

* Move discord

* Move user settings getter to app router

* Use standard var

* move getAllPublicUserData to app router

* Better error handling

* Delete getAllData.ts

* Delete discord

* Throw errors

* Handle error throwing

* Move updateSettings to app router

* Update confluence.svg

* Configure jest

* Create settings.test.ts

* Create updateSettings.test.ts

* Add getting test

* Use generalized validator

* Move Stripe to app router

* Move Sendgrid to app router

* Move vscode to airtable Analytics to app router

* Create posthog.ts

* Add types, move to utils

* Capture posthog event

* Change export

* Fix export

* Fix export

* Check dev env
  • Loading branch information
EstebanDalelR authored Aug 14, 2023
1 parent 58c7f6e commit 4e72063
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/api/user/settings/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import getUserSettings from "../../../../utils/db/user/settings";
import validateParams from "../../../../utils/api/validateParams";
import posthog from "../../../../utils/posthog/posthog";

export async function POST(request: Request) {
const req = await request.json();
Expand All @@ -11,7 +12,13 @@ export async function POST(request: Request) {
error: `Missing parameters: ${missingParams.join(", ")}`,
});
}

posthog.capture({
distinctId: req.email,
event: "user_settings_viewed",
properties: {
email: req.email,
},
});
try {
let dbResponse = await getUserSettings({ email: req.email });
return NextResponse.json(dbResponse);
Expand Down
36 changes: 36 additions & 0 deletions utils/posthog/posthog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PostHog } from "posthog-node";
interface PostHogEvent {
event: string;
distinctId?: string;
properties?: Record<string, any>;
groups?: Record<string, any>;
}

function PostHogClient(apiKey: string) {
const posthogClient = new PostHog(apiKey, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
});

return {
capture: ({ event, distinctId, properties, groups }: PostHogEvent) => {
posthogClient.capture({
distinctId: distinctId || "unknown_user",
event,
properties,
groups,
});
console.log("posthog event", event, properties);
if (process.env.NODE_ENV === "development") {
console.log(
`PostHog event: ${event} with properties: ${JSON.stringify(
properties
)}`
);
}
},
};
}

const posthog = PostHogClient(process.env.NEXT_PUBLIC_POSTHOG_KEY!);

export default posthog;

1 comment on commit 4e72063

@vercel
Copy link

@vercel vercel bot commented on 4e72063 Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.