@caesar1030/react-analytics
Version:
Analytics library for React applications
207 lines (169 loc) • 7.08 kB
TypeScript
import { default as default_2 } from 'react';
import { JSX } from 'react/jsx-runtime';
import { JSXElementConstructor } from 'react';
import { MouseEvent as MouseEvent_2 } from 'react';
import { PropsWithChildren } from 'react';
import { ReactElement } from 'react';
export declare interface ChildProps {
onClick?: (event: MouseEvent_2<Element>) => void;
}
export declare type CommandMap = {
event: {
eventName: GtagEventName;
params: GtagEventParams;
};
set: {
setArgs: GtagSetArgs;
params: GtagConfigParams[GtagSetArgs];
};
config: {
params: GtagConfigParams;
};
consent: {
consentArgs: GtagConsentArg;
params: GtagConsentParams;
};
};
export declare type DeepReadonly<T> = {
readonly [P in keyof T]: T[P] extends Record<string, unknown> ? DeepReadonly<T[P]> : T[P];
};
export declare class GA4 {
private readonly measurementId;
private isScriptLoaded;
private analyticsConfig;
private unHandledCommandList;
private loadHandlers;
constructor(measurementId: string);
private loadGoogleAnalytics;
initialize(config?: GtagConfigParams): void;
event(eventName: GtagEventName, eventParams: GtagEventParams): void;
config(config: GtagConfigParams): void;
consent(args: GtagConsentArg, params: GtagConsentParams): void;
set<K extends GtagSetArgs>(args: K, params: GtagConfigParams[K]): void;
onLoad(handler: () => void): void;
get configInfo(): Nullable<DeepReadonly<GtagConfigParams>>;
private queueCommand;
private handleQueuedCommands;
private printDebug;
private printError;
}
export declare function GAConfig({ children, config, enabled, }: PropsWithChildren<GAConfigProps>): JSX.Element;
export declare interface GAConfigProps {
config: GtagConfigParams;
enabled?: boolean;
}
export declare function GAConsent({ children, args, params, enabled, }: PropsWithChildren<GAConsentProps>): JSX.Element;
export declare interface GAConsentProps {
args: GtagConsentArg;
params: GtagConsentParams;
enabled?: boolean;
}
declare interface GAContextValue {
config: Nullable<DeepReadonly<GtagConfigParams>>;
ga4Event: (eventName: GtagEventName, eventParams: GtagEventParams) => void;
ga4Config: (config: GtagConfigParams) => void;
ga4Consent: (args: GtagConsentArg, params: GtagConsentParams) => void;
ga4Set: <K extends GtagSetArgs>(args: K, params: GtagConfigParams[K]) => void;
}
export declare function GAEvent({ children, eventName, eventParams, enabled, }: PropsWithChildren<LogEventProps>): ReactElement<ChildProps, string | JSXElementConstructor<any>>;
export declare function GAPageView({ children, enabled, eventParams, }: PageViewProps): JSX.Element;
export declare const GAProvider: ({ measurementId, config, children, }: default_2.PropsWithChildren<GAProviderProps>) => JSX.Element;
declare interface GAProviderProps {
measurementId: string;
config?: GtagConfigParams;
}
export declare function GASet<K extends GtagSetArgs>({ children, args, params, enabled, }: PropsWithChildren<{
args: K;
params: GtagConfigParams[K];
enabled?: boolean;
}>): JSX.Element;
export declare interface GtagConfigParams {
allow_google_signals?: boolean;
allow_ad_personalization_signals?: boolean;
campaign_id?: string;
campaign_source?: string;
campaign_medium?: string;
campaign_name?: string;
campaign_term?: string;
campaign_content?: string;
client_id?: string;
user_id?: string;
user_properties?: Record<string, string>;
content_group?: string;
cookie_domain?: string;
cookie_expires?: number;
cookie_flags?: string;
cookie_path?: string;
cookie_prefix?: string;
cookie_update?: boolean;
ignore_referrer?: boolean;
language?: string;
page_location?: string;
page_referrer?: string;
page_title?: string;
screen_resolution?: string;
send_page_view?: boolean;
[key: string]: unknown;
}
export declare type GtagConsentArg = "default" | "update";
export declare type GtagConsentParams = {
ad_storage?: GtagConsentStatus;
ad_user_data?: GtagConsentStatus;
ad_personalization?: GtagConsentStatus;
analytics_storage?: GtagConsentStatus;
wait_for_update?: number;
};
export declare type GtagConsentStatus = "granted" | "denied";
export declare type GtagEventName = RecommendedEventName | string;
export declare type GtagEventParams = {
[key: string]: unknown;
};
export declare type GtagSetArgs = keyof GtagConfigParams;
export declare interface LogEventProps {
eventName: GtagEventName;
eventParams: GtagEventParams;
enabled?: boolean;
}
export declare type Nullable<T> = T | null | undefined;
export declare interface PageViewProps {
children?: React.ReactNode;
enabled?: boolean;
eventParams?: GtagEventParams;
}
export declare type QueuedCommand = {
[K in keyof CommandMap]: {
command: K;
} & CommandMap[K];
}[keyof CommandMap];
export declare type RecommendedEventName = "ad_impression" | "earn_virtual_currency" | "generate_lead" | "join_group" | "login" | "purchase" | "refund" | "search" | "select_content" | "share" | "sign_up" | "spend_virtual_currency" | "tutorial_begin" | "tutorial_complete" | "add_payment_info" | "add_shipping_info" | "add_to_cart" | "add_to_wishlist" | "begin_checkout" | "remove_from_cart" | "select_item" | "select_promotion" | "view_cart" | "view_item" | "view_item_list" | "view_promotion" | "qualify_lead" | "disqualify_lead" | "working_lead" | "close_convert_lead" | "close_unconvert_lead" | "level_end" | "level_start" | "level_up" | "post_score" | "unlock_achievement";
export declare const useGA: () => GAContextValue;
export declare function useGAConfig({ config, enabled }: UseGAConfigProps): void;
declare interface UseGAConfigProps {
config: GtagConfigParams;
enabled?: boolean;
}
export declare function useGAConsent({ args, params, enabled, }: UseGAConsentProps): void;
declare interface UseGAConsentProps {
args: GtagConsentArg;
params: GtagConsentParams;
enabled?: boolean;
}
export declare function useGAEvent({ eventName, eventParams, enabled, }: UseGAEventProps): {
handleEvent: (e: MouseEvent_2<Element>, originalHandler?: (e: MouseEvent_2<Element>) => void) => void;
};
declare interface UseGAEventProps {
eventName: GtagEventName;
eventParams: GtagEventParams;
enabled?: boolean;
}
export declare const useGAPageView: ({ eventParams, enabled, }: UsePageViewTrackingProps) => void;
export declare function useGASet<K extends GtagSetArgs>({ args, params, enabled, }: {
args: K;
params: GtagConfigParams[K];
enabled?: boolean;
}): void;
export declare interface UsePageViewTrackingProps {
eventParams?: GtagEventParams;
enabled?: boolean;
}
export { }