SamS
Convex Communityβ€’3mo agoβ€’
6 replies
Sam

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 };
  },
});


🎁 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


Then install the component in your Convex app:

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
PostHog analytics component for Convex - track user events server-side - SamHoque/convex-posthog
GitHub - SamHoque/convex-posthog: PostHog analytics component for C...
Was this page helpful?