Refactor analytics event handling and manager setup
Updated event handling in `analytics-provider.tsx` to return results, enhancing consistency. Added a new function `registerActiveServices` in `analytics-manager.ts` for initializing active services, streamlining provider setup. Clarified types for `AnalyticsProviderFactory` to accept optional configuration.
This commit is contained in:
@@ -15,7 +15,7 @@ import {
|
||||
type AnalyticsMapping<
|
||||
T extends ConsumerProvidedEventTypes = NonNullable<unknown>,
|
||||
> = {
|
||||
[K in AppEventType<T>]?: (event: AppEvent<T, K>) => void;
|
||||
[K in AppEventType<T>]?: (event: AppEvent<T, K>) => unknown;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -51,17 +51,17 @@ const analyticsMapping: AnalyticsMapping = {
|
||||
const userId = event.payload.userId;
|
||||
|
||||
if (userId) {
|
||||
analytics.identify(userId);
|
||||
return analytics.identify(userId);
|
||||
}
|
||||
},
|
||||
'user.signedUp': (event) => {
|
||||
analytics.trackEvent(event.type, event.payload);
|
||||
return analytics.trackEvent(event.type, event.payload);
|
||||
},
|
||||
'checkout.started': (event) => {
|
||||
analytics.trackEvent(event.type, event.payload);
|
||||
return analytics.trackEvent(event.type, event.payload);
|
||||
},
|
||||
'user.updated': (event) => {
|
||||
analytics.trackEvent(event.type, event.payload);
|
||||
return analytics.trackEvent(event.type, event.payload);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -83,7 +83,7 @@ export function AnalyticsProvider(props: React.PropsWithChildren) {
|
||||
* Hook to report page views to the analytics service
|
||||
* @param reportAnalyticsFn
|
||||
*/
|
||||
function useReportPageView(reportAnalyticsFn: (url: string) => void) {
|
||||
function useReportPageView(reportAnalyticsFn: (url: string) => unknown) {
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
|
||||
@@ -22,6 +22,27 @@ export function createAnalyticsManager<T extends string, Config extends object>(
|
||||
return Array.from(activeServices.values());
|
||||
};
|
||||
|
||||
const registerActiveServices = (
|
||||
options: CreateAnalyticsManagerOptions<T, Config>,
|
||||
) => {
|
||||
Object.keys(options.providers).forEach((provider) => {
|
||||
const providerKey = provider as keyof typeof options.providers;
|
||||
const factory = options.providers[providerKey];
|
||||
|
||||
if (!factory) {
|
||||
console.warn(
|
||||
`Analytics provider '${provider}' not registered. Skipping initialization.`,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
activeServices.set(provider as T, factory());
|
||||
});
|
||||
};
|
||||
|
||||
registerActiveServices(options);
|
||||
|
||||
return {
|
||||
addProvider: (
|
||||
provider: T,
|
||||
|
||||
@@ -24,7 +24,7 @@ export interface AnalyticsService extends TrackPageView, TrackEvent, Identify {
|
||||
}
|
||||
|
||||
export type AnalyticsProviderFactory<Config extends object> = (
|
||||
config: Config,
|
||||
config?: Config,
|
||||
) => AnalyticsService;
|
||||
|
||||
export interface CreateAnalyticsManagerOptions<
|
||||
|
||||
Reference in New Issue
Block a user