UNPKG

@optic7409/resolvo-analytics

Version:

Simplified analytics client library for Next.js with automatic SSR handling, one-line integration, and comprehensive tracking

58 lines (57 loc) 2.46 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { getDefaultConfig, detectEnvironment, shouldEnableAnalytics } from '../../utils/environment'; import { PageTracker } from './PageTracker'; import { AnalyticsProvider } from './AnalyticsProvider'; export function ResolvoAnalytics({ apiKey, websiteToken, apiUrl, debug, ssr = false, autoInitialize = true, environment, children, autoTrackPages = true, autoTrackClicks = true, autoTrackForms = true, ...rest }) { // Merge with defaults const defaults = getDefaultConfig(); const env = detectEnvironment(); const config = { ...defaults, apiKey, websiteToken: websiteToken || process.env.NEXT_PUBLIC_RESOLVO_WEBSITE_TOKEN || '', apiUrl: apiUrl || defaults.apiUrl, debug: debug ?? defaults.debug, ssr: ssr ?? defaults.ssr, autoInitialize: autoInitialize ?? defaults.autoInitialize, environment: (environment || (env.isDevelopment ? 'development' : 'production')), framework: env.framework, routerType: env.routerType, enableAutoTracking: autoTrackClicks || autoTrackForms, ...rest }; // Validate required fields if (!config.apiKey) { if (config.debug) { console.error('[Resolvo Analytics] Missing required apiKey'); } return children || null; } if (!config.websiteToken) { if (config.debug) { console.error('[Resolvo Analytics] Missing required websiteToken'); } return children || null; } // Check if analytics should be enabled if (!shouldEnableAnalytics(config)) { if (config.debug) { console.log('[Resolvo Analytics] Analytics disabled for current environment'); } return children || null; } // If no children provided, just return the tracker if (!children) { return autoTrackPages ? _jsx(PageTracker, { config: config }) : null; } // If children provided, wrap with provider and tracker return (_jsxs(AnalyticsProvider, { config: config, children: [autoTrackPages && _jsx(PageTracker, { config: config }), children] })); } // Export a simplified version for Next.js App Router export function NextJsPageTracker(props) { return _jsx(ResolvoAnalytics, { ...props }); } // Export a version for Pages Router export function PagesPageTracker(props) { return _jsx(ResolvoAnalytics, { ...props }); }