Files
js/hooks/analytics/useTrack.ts
T
Tair Asimandikethirdweb 821ec55a82 feat: growth plan (#1936)
fixes PROD-172, PROD-389, MON-58, MON-90, MON-88, MON-109, MON-98

* fix link and unauthed

---------

Co-authored-by: ikethirdweb <[email protected]>
2024-01-24 14:10:38 +00:00

41 lines
1.1 KiB
TypeScript

import { flatten } from "flat";
import posthog from "posthog-js-opensource";
import { useCallback } from "react";
type TExtendedTrackParams = {
category: string;
action: string;
label?: string;
[key: string]: any;
};
export function useTrack() {
return useCallback((trackingData: TExtendedTrackParams) => {
const { category, action, label, ...restData } = trackingData;
const catActLab = label
? `${category}.${action}.${label}`
: `${category}.${action}`;
if (process.env.NODE_ENV !== "production") {
console.debug(`[PH.capture]:${catActLab}`, restData);
}
const restDataSafe = Object.fromEntries(
Object.entries(restData).map(([key, value]) => {
if (value instanceof Error) {
return [
key,
{ message: value.message, stack: value.stack?.toString() },
];
}
return [key, value];
}),
);
try {
posthog.capture(catActLab, flatten(restDataSafe));
} catch (e) {
// ignore - we just don't want to trigger an error in the app if posthog fails
}
}, []);
}