UNPKG

@scaleway/use-analytics

Version:

A small hook to handle events analytics

46 lines (45 loc) 2.03 kB
import type { LoadOptions } from '@rudderstack/analytics-js'; import { RudderAnalytics } from '@rudderstack/analytics-js'; import type { JSX, ReactNode } from 'react'; import type { CategoryKind } from '../types'; type Analytics = RudderAnalytics; export type { Analytics }; export type OnEventError = (error: Error) => Promise<void> | void; type EventFunction = (...args: never[]) => Promise<void> | void; type Events = Record<string, (analytics?: Analytics, onEventError?: OnEventError) => EventFunction>; type AnalyticsContextInterface<T extends Events = Events> = { analytics: Analytics | undefined; events: { [K in keyof T]: ReturnType<T[K]>; }; isAnalyticsReady: boolean; }; export declare function useAnalytics<T extends Events>(): AnalyticsContextInterface<T>; export type AnalyticsProviderProps<T> = { settings?: { writeKey: string; cdnURL: string; }; loadOptions?: LoadOptions; /** * This option force provider to render children only when isAnalytics is ready, you can also set a timeout to prevent blocking indefinitely and use isAnalyticsReady to show a loading screen. */ shouldRenderOnlyWhenReady?: boolean; /** * used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time. You can also set a timeout to prevent blocking indefinitely. */ needConsent?: boolean; timeout?: number; allowedConsents: CategoryKind[]; deniedConsents: CategoryKind[]; onError?: (err: Error) => void; onEventError?: OnEventError; events: T; children: ReactNode; /** * This can be used to set consentManagement or modify the config */ onLoaded: (analytics: Analytics) => void; }; export declare function AnalyticsProvider<T extends Events>({ children, settings, loadOptions, shouldRenderOnlyWhenReady, needConsent, onError, onEventError, allowedConsents, deniedConsents, events, onLoaded, timeout, }: AnalyticsProviderProps<T>): JSX.Element; export default AnalyticsProvider;