Add Analytics package (#46)
* Add Analytics package Created a new analytics package with a manager to handle tracking of events and page views. The package includes a default provider that can be switched out and uses a NullAnalyticsService if no provider is registered. Additional types, scripts, and package configuration are also provided to support development. * Add marketing components for UI package Introduced new React components under "marketing" for the UI package. These include 'Pill', 'GradientSecondaryText', 'Hero', 'CtaButton', 'FeatureCard', 'FeatureGrid', 'FeatureShowcase', 'GradientText', 'Header', and 'SecondaryHero'. Updated 'package.json' to export these components. Replaced the implementation of 'Home', 'SiteHeader', and 'SiteFooter' with these components for cleaner code and better reusability.
This commit is contained in:
committed by
GitHub
parent
5ee7bacb2a
commit
86d82d889c
72
packages/analytics/src/analytics-manager.ts
Normal file
72
packages/analytics/src/analytics-manager.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { NullAnalyticsService } from './null-analytics-service';
|
||||
import {
|
||||
AnalyticsManager,
|
||||
AnalyticsService,
|
||||
CreateAnalyticsManagerOptions,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* Creates an analytics manager that can be used to track page views and events. The manager is initialized with a
|
||||
* default provider and can be switched to a different provider at any time. The manager will use a NullAnalyticsService
|
||||
* if the provider is not registered.
|
||||
* @param options
|
||||
*/
|
||||
export function createAnalyticsManager<T extends string, Config extends object>(
|
||||
options: CreateAnalyticsManagerOptions<T, Config>,
|
||||
): AnalyticsManager {
|
||||
let activeService: AnalyticsService = NullAnalyticsService;
|
||||
|
||||
const getActiveService = (): AnalyticsService => {
|
||||
if (activeService === NullAnalyticsService) {
|
||||
console.warn(
|
||||
'Analytics service not initialized. Using NullAnalyticsService.',
|
||||
);
|
||||
}
|
||||
|
||||
return activeService;
|
||||
};
|
||||
|
||||
const initialize = (provider: T, config: Config) => {
|
||||
const factory = options.providers[provider];
|
||||
|
||||
if (!factory) {
|
||||
console.error(
|
||||
`Analytics provider '${provider}' not registered. Using NullAnalyticsService.`,
|
||||
);
|
||||
|
||||
activeService = NullAnalyticsService;
|
||||
return;
|
||||
}
|
||||
|
||||
activeService = factory(config);
|
||||
activeService.initialize();
|
||||
};
|
||||
|
||||
// Initialize with the default provider
|
||||
initialize(options.defaultProvider, {} as Config);
|
||||
|
||||
return {
|
||||
identify: (userId: string, traits?: Record<string, string>) => {
|
||||
return getActiveService().identify(userId, traits);
|
||||
},
|
||||
|
||||
/**
|
||||
* Track a page view with the given URL.
|
||||
* @param url
|
||||
*/
|
||||
trackPageView: (url: string) => {
|
||||
return getActiveService().trackPageView(url);
|
||||
},
|
||||
/**
|
||||
* Track an event with the given name and properties.
|
||||
* @param eventName
|
||||
* @param eventProperties
|
||||
*/
|
||||
trackEvent: (
|
||||
eventName: string,
|
||||
eventProperties?: Record<string, string | string[]>,
|
||||
) => {
|
||||
return getActiveService().trackEvent(eventName, eventProperties);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user