mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 13:01:56 +00:00
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:
@@ -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!",
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Icon } from "@iconify/react";
|
||||
import { Checkbox } from "@nextui-org/react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import posthog from "posthog-js";
|
||||
import { useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
@@ -18,6 +17,7 @@ import { scanOnDemand, scheduleDaily } from "@/actions/scans";
|
||||
import { getTask } from "@/actions/task/tasks";
|
||||
import { CheckIcon, RocketIcon } from "@/components/icons";
|
||||
import { useToast } from "@/components/ui";
|
||||
import { trackCloudConnectionSuccess } from "@/lib/analytics";
|
||||
import { CustomButton } from "@/components/ui/custom";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { checkTaskStatus } from "@/lib/helper";
|
||||
@@ -146,12 +146,11 @@ export const TestConnectionForm = ({
|
||||
});
|
||||
} else {
|
||||
setIsRedirecting(true);
|
||||
// Capture PostHog event for successful cloud connection
|
||||
posthog.capture("cloud_connection_success", {
|
||||
provider_type: providerType,
|
||||
provider_alias: providerData.data.attributes.alias,
|
||||
scan_type: form.watch("runOnce") ? "single" : "scheduled",
|
||||
timestamp: Date.now(),
|
||||
// Track cloud connection success event
|
||||
trackCloudConnectionSuccess({
|
||||
providerType: providerType,
|
||||
providerAlias: providerData.data.attributes.alias,
|
||||
scanType: form.watch("runOnce") ? "single" : "scheduled",
|
||||
});
|
||||
router.push("/scans");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user