adds 2 more events

This commit is contained in:
pandeymangg
2026-04-03 10:45:50 +05:30
parent e1d681fbdb
commit d0e423af57
5 changed files with 34 additions and 17 deletions

View File

@@ -53,7 +53,6 @@ export const createOrganizationAction = authenticatedActionClient
capturePostHogEvent(ctx.user.id, "organization_created", {
organization_id: newOrganization.id,
is_first_org: hasNoOrganizations,
user_id: ctx.user.id,
});
return newOrganization;

View File

@@ -7,6 +7,7 @@ const POSTHOG_HOST = "https://eu.i.posthog.com";
const globalForPostHog = globalThis as unknown as {
posthogServerClient: PostHog | undefined;
posthogHandlersRegistered: boolean | undefined;
};
function createPostHogClient(): PostHog | null {
@@ -26,7 +27,11 @@ if (process.env.NODE_ENV !== "production" && posthogServerClient) {
globalForPostHog.posthogServerClient = posthogServerClient;
}
if (process.env.NEXT_RUNTIME === "nodejs" && posthogServerClient) {
if (
process.env.NEXT_RUNTIME === "nodejs" &&
posthogServerClient &&
!globalForPostHog.posthogHandlersRegistered
) {
const shutdownPostHog = () => {
posthogServerClient?.shutdown().catch((err) => {
logger.error(err, "Error shutting down PostHog server client");
@@ -34,4 +39,5 @@ if (process.env.NEXT_RUNTIME === "nodejs" && posthogServerClient) {
};
process.on("SIGTERM", shutdownPostHog);
process.on("SIGINT", shutdownPostHog);
globalForPostHog.posthogHandlersRegistered = true;
}

View File

@@ -343,18 +343,22 @@ export const authOptions: NextAuthOptions = {
const captureSignIn = async (provider: string) => {
if (!POSTHOG_KEY) return;
const [membershipCount, userData] = await Promise.all([
prisma.membership.count({ where: { userId } }),
prisma.user.findUnique({ where: { id: userId }, select: { lastLoginAt: true } }),
]);
const isFirstLoginToday =
!userData?.lastLoginAt || userData.lastLoginAt.toDateString() !== new Date().toDateString();
try {
const [membershipCount, userData] = await Promise.all([
prisma.membership.count({ where: { userId } }),
prisma.user.findUnique({ where: { id: userId }, select: { lastLoginAt: true } }),
]);
const isFirstLoginToday =
userData?.lastLoginAt?.toISOString().slice(0, 10) !== new Date().toISOString().slice(0, 10);
capturePostHogEvent(userId, "user_signed_in", {
auth_provider: provider,
organization_count: membershipCount,
is_first_login_today: isFirstLoginToday,
});
capturePostHogEvent(userId, "user_signed_in", {
auth_provider: provider,
organization_count: membershipCount,
is_first_login_today: isFirstLoginToday,
});
} catch (error) {
logger.warn({ error }, "Failed to capture PostHog sign-in event");
}
};
if (account?.provider === "credentials" || account?.provider === "token") {
@@ -363,7 +367,7 @@ export const authOptions: NextAuthOptions = {
logger.error("Email Verification is Pending");
throw new Error("Email Verification is Pending");
}
await captureSignIn(account.provider);
void captureSignIn(account.provider);
await updateUserLastLoginAt(userEmail);
return true;
}
@@ -375,12 +379,12 @@ export const authOptions: NextAuthOptions = {
});
if (result) {
await captureSignIn(account.provider);
void captureSignIn(account.provider);
await updateUserLastLoginAt(userEmail);
}
return result;
}
await captureSignIn(account?.provider ?? "unknown");
void captureSignIn(account?.provider ?? "unknown");
await updateUserLastLoginAt(userEmail);
return true;
},

View File

@@ -3,6 +3,7 @@
import { CheckIcon, GiftIcon } from "lucide-react";
import Image from "next/image";
import { useRouter } from "next/navigation";
import posthog from "posthog-js";
import { useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
@@ -49,6 +50,9 @@ export const SelectPlanCard = ({ nextUrl, organizationId }: SelectPlanCardProps)
const handleStartTrial = async () => {
setIsStartingTrial(true);
if (posthog.__loaded) {
posthog.capture("reverse_trial_started", { organization_id: organizationId });
}
try {
const result = await startProTrialAction({ organizationId });
if (result?.data) {
@@ -68,6 +72,9 @@ export const SelectPlanCard = ({ nextUrl, organizationId }: SelectPlanCardProps)
const handleContinueHobby = async () => {
setIsStartingHobby(true);
if (posthog.__loaded) {
posthog.capture("stayed_on_hobby_plan", { organization_id: organizationId });
}
try {
const result = await startHobbyAction({ organizationId });
if (result?.data) {

View File

@@ -22,6 +22,7 @@ import { checkExternalUrlsPermission } from "@/modules/survey/editor/lib/check-e
import { updateSurvey, updateSurveyDraft } from "@/modules/survey/editor/lib/survey";
import { ZSurveyDraft } from "@/modules/survey/editor/types/survey";
import { getSurveyFollowUpsPermission } from "@/modules/survey/follow-ups/lib/utils";
import { getElementsFromBlocks } from "@/modules/survey/lib/client-utils";
import { checkSpamProtectionPermission } from "@/modules/survey/lib/permission";
import { getOrganizationBilling, getSurvey } from "@/modules/survey/lib/survey";
import { getProject } from "./lib/project";
@@ -150,7 +151,7 @@ export const updateSurveyAction = authenticatedActionClient.inputSchema(ZSurvey)
capturePostHogEvent(ctx.user.id, "survey_published", {
survey_id: result.id,
survey_type: result.type,
question_count: result.questions?.length ?? 0,
question_count: getElementsFromBlocks(result.blocks).length,
organization_id: organizationId,
has_targeting: result.segment ? !result.segment.isPrivate : false,
language_count: result.languages?.length ?? 0,