Sam
Sam4w ago

convex-posthog: PostHog Analytics Component for Convex

Hey everyone! 👋 I just published convex-posthog - a Convex component that makes it super easy to track user events with PostHog analytics directly from your mutations! npm: @samhoque/convex-posthog GitHub: https://github.com/samhoque/convex-posthog ✨ What it does Track user events server-side from your Convex mutations without blocking your app logic. Events are scheduled as background jobs, so analytics failures won't affect your core functionality. 🚀 Quick Example
import { components } from "./_generated/api";
import { PostHog } from "@samhoque/convex-posthog";

const posthog = new PostHog(components.posthog, {
apiKey: process.env.POSTHOG_API_KEY,
});

export const signupUser = mutation({
args: { userId: v.string(), email: v.string() },
handler: async (ctx, args) => {
// Your business logic here
await ctx.db.insert("users", args);

// Track the event
await posthog.trackUserEvent(ctx, {
userId: args.userId,
event: "user_signed_up",
properties: { email: args.email }
});

return { success: true };
},
});
import { components } from "./_generated/api";
import { PostHog } from "@samhoque/convex-posthog";

const posthog = new PostHog(components.posthog, {
apiKey: process.env.POSTHOG_API_KEY,
});

export const signupUser = mutation({
args: { userId: v.string(), email: v.string() },
handler: async (ctx, args) => {
// Your business logic here
await ctx.db.insert("users", args);

// Track the event
await posthog.trackUserEvent(ctx, {
userId: args.userId,
event: "user_signed_up",
properties: { email: args.email }
});

return { success: true };
},
});
🎁 Features - 🔥 Non-blocking event tracking (uses ctx.scheduler.runAfter) - 🛡️ Type-safe API with full TypeScript support - ⚡ Zero-config when using environment variables - 📦 Proper Convex component with all the best practices 📦 Installation
npm install @samhoque/convex-posthog
npm install @samhoque/convex-posthog
Then install the component in your Convex app:
npx convex@latest component add samhoque/convex-posthog
npx convex@latest component add samhoque/convex-posthog
Would love to hear feedback or ideas for improvements! This is my first Convex component, so let me know if you find it useful or have suggestions. 🙌
GitHub
GitHub - SamHoque/convex-posthog: PostHog analytics component for C...
PostHog analytics component for Convex - track user events server-side - SamHoque/convex-posthog
4 Replies
Hmza
Hmza3w ago
this is great!! i'll add that to https://sticky.today you got any users yet? are you tracking something? does it work well?
Ideas that Stick, literally
Organize thoughts, collaborate in real-time, and access your ideas from anywhere. Try it free today!
Sam
SamOP3w ago
No users yet, but I am building a SaaS, and have been using it for a while, so I just extracted it into a component. Feel free to leave feedback, I'll resolve them asap.
bryan_3mavericks
awesome, i was actually looking to setup posthog to my convex app, will def test it out
Chief Chungus
Chief Chungus7d ago
This looks really interesting, I was looking into using one dollar stats which is made by drizzle but wasn't having any look so decided I might use posthog. Think I will use this instead as seems a nice easy way to integrate posthog into convex. @bryan_3mavericks did you end up testing it out?

Did you find this page helpful?