@akson/cortex-landing-hooks
Version:
React hooks for landing pages - device detection, API calls, form submission, analytics, and performance
260 lines (256 loc) • 9.18 kB
TypeScript
interface AnalyticsEvent {
name: string;
properties?: Record<string, any>;
value?: number;
}
interface UserData {
email?: string;
phone?: string;
firstName?: string;
lastName?: string;
userId?: string;
[key: string]: any;
}
interface FormData {
completionPercentage: number;
fieldsCompleted: number;
totalFields: number;
filledFields: string[];
emptyFields: string[];
}
interface AnalyticsConfig {
/**
* Google Tag Manager ID
*/
gtmId?: string;
/**
* PostHog project key
*/
posthogKey?: string;
/**
* Facebook Pixel ID
*/
facebookPixelId?: string;
/**
* Custom event tracking function
*/
customTracker?: (event: AnalyticsEvent) => void;
/**
* Debug mode
*/
debug?: boolean;
}
interface AnalyticsHookResult {
track: (eventName: string, properties?: Record<string, any>) => void;
trackPageView: (path?: string, properties?: Record<string, any>) => void;
trackInteraction: (element: string, action: string, properties?: Record<string, any>) => void;
trackConversion: (type: string, value?: number, properties?: Record<string, any>) => void;
identify: (userData: UserData) => void;
collectFormData: () => FormData | null;
trackFormStart: (formName: string, properties?: Record<string, any>) => void;
trackFormComplete: (formName: string, properties?: Record<string, any>) => void;
trackFormAbandonment: (formName: string, step?: string, properties?: Record<string, any>) => void;
getSessionId: () => string | null;
isReady: boolean;
debug: (message: string, data?: any) => void;
}
/**
* Generic analytics hook that supports multiple platforms
*/
declare function useAnalytics$1(config?: AnalyticsConfig): AnalyticsHookResult;
/**
* Hook for e-commerce tracking
*/
declare function useEcommerceAnalytics(config?: AnalyticsConfig): {
trackPurchase: (transactionId: string, items: any[], value: number, currency?: string) => void;
trackAddToCart: (item: any, value?: number, currency?: string) => void;
trackViewItem: (item: any, value?: number, currency?: string) => void;
trackBeginCheckout: (items: any[], value: number, currency?: string) => void;
track: (eventName: string, properties?: Record<string, any>) => void;
trackPageView: (path?: string, properties?: Record<string, any>) => void;
trackInteraction: (element: string, action: string, properties?: Record<string, any>) => void;
trackConversion: (type: string, value?: number, properties?: Record<string, any>) => void;
identify: (userData: UserData) => void;
collectFormData: () => FormData | null;
trackFormStart: (formName: string, properties?: Record<string, any>) => void;
trackFormComplete: (formName: string, properties?: Record<string, any>) => void;
trackFormAbandonment: (formName: string, step?: string, properties?: Record<string, any>) => void;
getSessionId: () => string | null;
isReady: boolean;
debug: (message: string, data?: any) => void;
};
/**
* Analytics Compatibility Hook
* Bridges @akson/cortex-analytics-react with existing landing code
* Migrated from /app/hooks/use-analytics-compat.ts
*/
type FunnelStage = string;
interface FunnelEventData {
stage: FunnelStage;
source?: string;
[key: string]: any;
}
/**
* Compatibility hook that maintains the existing API
* Currently uses mock implementations, will integrate with @akson/cortex-analytics-react when available
*/
declare function useAnalyticsCompat(): {
trackStage: (stage: FunnelStage, data?: Partial<FunnelEventData>) => void;
trackAbandonment: (lastStage: FunnelStage, reason?: string, additionalData?: Record<string, any>) => void;
trackConversion: (type: "order" | "whatsapp" | "phone" | "lead", value?: number, additionalData?: Record<string, any>) => void;
sendUserData: (userData: any) => void;
track: (event: string, properties?: any) => void;
trackPageView: (properties?: any) => void;
trackInteraction: (properties?: any) => void;
trackEcommerce: (properties?: any) => void;
trackExperiment: (properties?: any) => void;
trackFeatureFlag: (properties?: any) => void;
identify: (userId: string, properties?: any) => void;
getSessionId: () => string;
getFunnelPath: () => never[];
getTimeInFunnel: () => number;
clearFunnelState: () => void;
collectFormData: () => {
completionPercentage: number;
fieldsCompleted: number;
totalFields: number;
filledFields: string[];
emptyFields: string[];
} | null;
isGTMReady: () => boolean;
getPostHog: () => any;
};
/**
* Alias for backward compatibility
*/
declare const useAnalytics: typeof useAnalyticsCompat;
/**
* GTM Tracking Hook
* Simplified GTM tracking for landing pages
* Migrated from /app/hooks/use-gtm-tracking.ts
*/
interface DataLayerEvent {
event: string;
component?: string;
action?: string;
value?: number;
metadata?: Record<string, any>;
[key: string]: any;
}
declare global {
interface Window {
dataLayer: any[];
}
}
interface GTMConfig {
enabled: boolean;
debug: boolean;
logEvents: boolean;
containerId?: string;
}
declare const GTM_EVENTS: {
readonly INTERACTION: "interaction";
readonly CONVERSION: "conversion";
readonly PAGEVIEW: "page_view";
readonly EXPERIMENT: "experiment";
};
declare const GTM_ACTIONS: {
readonly CLICK: "click";
readonly VIEW: "view";
readonly SUBMIT: "submit";
readonly HOVER: "hover";
readonly FOCUS: "focus";
readonly BLUR: "blur";
readonly START: "start";
readonly STEP: "step";
readonly COMPLETE: "complete";
readonly ABANDON: "abandon";
readonly ADD_TO_CART: "add_to_cart";
readonly CONVERT: "convert";
readonly SCROLL: "scroll";
};
declare const GTM_COMPONENTS: {
readonly HERO: "hero";
readonly CTA: "cta";
readonly FORM: "form";
readonly WHATSAPP: "whatsapp";
readonly PRODUCT: "product";
readonly NAVIGATION: "navigation";
readonly PAGE: "page";
};
type GTMAction = typeof GTM_ACTIONS[keyof typeof GTM_ACTIONS];
type GTMComponent = typeof GTM_COMPONENTS[keyof typeof GTM_COMPONENTS];
/**
* Hook for GTM tracking with landing page optimizations
*/
declare function useGTMTracking(config?: Partial<GTMConfig>): {
track: (event: DataLayerEvent) => void;
trackInteraction: (component: GTMComponent, action: GTMAction, metadata?: Record<string, any>) => void;
trackConversion: (component: GTMComponent, value?: number, metadata?: Record<string, any>) => void;
trackPageView: (metadata?: Record<string, any>) => void;
trackExperiment: (experimentName: string, variant: string, action?: GTMAction, metadata?: Record<string, any>) => void;
trackWhatsApp: (action?: GTMAction, metadata?: {
location?: string;
urgency_level?: string;
variant?: string;
message_type?: string;
[key: string]: any;
}) => void;
trackForm: (action: GTMAction, formType: string, metadata?: {
step?: number;
step_name?: string;
field_name?: string;
error_type?: string;
[key: string]: any;
}) => void;
trackCTA: (action?: GTMAction, metadata?: {
variant?: string;
location?: string;
cta_type?: string;
[key: string]: any;
}) => void;
trackHero: (action?: GTMAction, metadata?: {
variant?: string;
cta_type?: string;
[key: string]: any;
}) => void;
trackProduct: (action: GTMAction, productId: string, productName: string, metadata?: {
price?: number;
category?: string;
quantity?: number;
[key: string]: any;
}) => void;
identify: (userId: string, traits?: Record<string, any>) => void;
reset: () => void;
ACTIONS: {
readonly CLICK: "click";
readonly VIEW: "view";
readonly SUBMIT: "submit";
readonly HOVER: "hover";
readonly FOCUS: "focus";
readonly BLUR: "blur";
readonly START: "start";
readonly STEP: "step";
readonly COMPLETE: "complete";
readonly ABANDON: "abandon";
readonly ADD_TO_CART: "add_to_cart";
readonly CONVERT: "convert";
readonly SCROLL: "scroll";
};
COMPONENTS: {
readonly HERO: "hero";
readonly CTA: "cta";
readonly FORM: "form";
readonly WHATSAPP: "whatsapp";
readonly PRODUCT: "product";
readonly NAVIGATION: "navigation";
readonly PAGE: "page";
};
EVENTS: {
readonly INTERACTION: "interaction";
readonly CONVERSION: "conversion";
readonly PAGEVIEW: "page_view";
readonly EXPERIMENT: "experiment";
};
};
export { type AnalyticsConfig, type AnalyticsEvent, type AnalyticsHookResult, type FormData, type FunnelStage, type GTMAction, type GTMComponent, GTM_ACTIONS, GTM_COMPONENTS, GTM_EVENTS, type UserData, useAnalytics$1 as useAnalytics, useAnalyticsCompat, useAnalytics as useAnalyticsCompatAlias, useEcommerceAnalytics, useGTMTracking };