feat: centralize PostHog analytics with proper error handling

- Create lib/analytics.ts with centralized tracking functions
- Use arrow functions and camelCase naming per code standards
- Add error handling to prevent analytics failures from crashing app
- Update auth forms and provider workflows to use new analytics lib
- Export TypeScript interfaces for type-safe analytics payloads

Addresses PR review comments:
- Convert to arrow functions (requested by @paabloLC)
- Use camelCase for event and property names
- Centralize analytics logic for better maintainability
This commit is contained in:
Amit Sharma committed 2025-07-29 19:49:12 -07:00
1 parent 255df2dbc1
commit bc34c9df4a
4 files changed
+161 -29

No files matched your search

+7 -20
View File
@@ -4,7 +4,6 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { Icon } from "@iconify/react";
import { Button, Checkbox, Divider, Link, Tooltip } from "@nextui-org/react";
import { useRouter, useSearchParams } from "next/navigation";
import posthog from "posthog-js";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
@@ -22,6 +21,7 @@ import {
FormField,
FormMessage,
} from "@/components/ui/form";
import { initializeSession, trackUserLogin, trackUserRegistration } from "@/lib/analytics";
import { ApiError, authFormSchema } from "@/types";
export const AuthForm = ({
@@ -82,7 +82,7 @@ export const AuthForm = ({
const onSubmit = async (data: z.infer<typeof formSchema>) => {
//getting an new posthog init
posthog.reset(true);
initializeSession();
if (type === "sign-in") {
if (data.isSamlMode) {
const email = data.email.toLowerCase();
@@ -110,11 +110,7 @@ export const AuthForm = ({
password: data.password,
});
if (result?.message === "Success") {
posthog.identify(data.email.toLowerCase());
posthog.capture("user_login", {
email: data.email.toLocaleLowerCase(),
timestamp: Date.now(),
});
trackUserLogin({ email: data.email });
router.push("/");
} else if (result?.errors && "credentials" in result.errors) {
form.setError("email", {
@@ -136,19 +132,10 @@ export const AuthForm = ({
const newUser = await createNewUser(data);
if (!newUser.errors) {
let firstName = "";
let lastName = "";
if (data.name) {
const nameParts = data.name.trim().split(" ");
firstName = nameParts[0] || "";
lastName = nameParts.slice(1).join(" ") || "";
}
posthog.capture("user_registered", {
email: data.email.toLocaleLowerCase(),
firstname: firstName,
lastname: lastName,
company: data.company || "",
timestamp: Date.now(),
trackUserRegistration({
email: data.email,
fullName: data.name,
company: data.company,
});
toast({
title: "Success!",