|
| 1 | +/** |
| 2 | + * Debug Plugin |
| 3 | + * |
| 4 | + * Emits structured debug events to window and optionally logs to console. |
| 5 | + * Useful for debugging and Chrome extension integration. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { PluginFunction } from '@lytics/sdk-kit'; |
| 9 | + |
| 10 | +export interface DebugPluginConfig { |
| 11 | + debug?: { |
| 12 | + enabled?: boolean; |
| 13 | + console?: boolean; |
| 14 | + window?: boolean; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +export interface DebugPlugin { |
| 19 | + log(message: string, data?: unknown): void; |
| 20 | + isEnabled(): boolean; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Debug Plugin |
| 25 | + * |
| 26 | + * Listens to all SDK events and emits them as window events for debugging. |
| 27 | + * Also optionally logs to console. |
| 28 | + * |
| 29 | + * @example |
| 30 | + * ```typescript |
| 31 | + * import { createInstance } from '@prosdevlab/experience-sdk'; |
| 32 | + * import { debugPlugin } from '@prosdevlab/experience-sdk-plugins'; |
| 33 | + * |
| 34 | + * const sdk = createInstance({ debug: { enabled: true, console: true } }); |
| 35 | + * sdk.use(debugPlugin); |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +export const debugPlugin: PluginFunction = (plugin, instance, config) => { |
| 39 | + plugin.ns('debug'); |
| 40 | + |
| 41 | + // Set defaults |
| 42 | + plugin.defaults({ |
| 43 | + debug: { |
| 44 | + enabled: false, |
| 45 | + console: false, |
| 46 | + window: true, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // Helper to check if debug is enabled |
| 51 | + const isEnabled = (): boolean => config.get('debug.enabled') ?? false; |
| 52 | + const shouldLogConsole = (): boolean => config.get('debug.console') ?? false; |
| 53 | + const shouldEmitWindow = (): boolean => config.get('debug.window') ?? true; |
| 54 | + |
| 55 | + // Log function |
| 56 | + const log = (message: string, data?: unknown): void => { |
| 57 | + if (!isEnabled()) return; |
| 58 | + |
| 59 | + const timestamp = new Date().toISOString(); |
| 60 | + const logData = { |
| 61 | + timestamp, |
| 62 | + message, |
| 63 | + data, |
| 64 | + }; |
| 65 | + |
| 66 | + // Console logging |
| 67 | + if (shouldLogConsole()) { |
| 68 | + console.log(`[experiences] ${message}`, data || ''); |
| 69 | + } |
| 70 | + |
| 71 | + // Window event emission |
| 72 | + if (shouldEmitWindow() && typeof window !== 'undefined') { |
| 73 | + const event = new CustomEvent('experience-sdk:debug', { |
| 74 | + detail: logData, |
| 75 | + }); |
| 76 | + window.dispatchEvent(event); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + // Expose debug API |
| 81 | + plugin.expose({ |
| 82 | + debug: { |
| 83 | + log, |
| 84 | + isEnabled, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + // If debug is enabled, listen to all events |
| 89 | + if (isEnabled()) { |
| 90 | + // Listen to experiences:* events |
| 91 | + instance.on('experiences:ready', () => { |
| 92 | + log('SDK initialized and ready'); |
| 93 | + }); |
| 94 | + |
| 95 | + instance.on('experiences:registered', (payload) => { |
| 96 | + log('Experience registered', payload); |
| 97 | + }); |
| 98 | + |
| 99 | + instance.on('experiences:evaluated', (payload) => { |
| 100 | + log('Experience evaluated', payload); |
| 101 | + }); |
| 102 | + |
| 103 | + // Listen to all events using wildcard |
| 104 | + instance.on('*', (eventName, ...args) => { |
| 105 | + // Skip if already logged above |
| 106 | + if ( |
| 107 | + eventName === 'experiences:ready' || |
| 108 | + eventName === 'experiences:registered' || |
| 109 | + eventName === 'experiences:evaluated' |
| 110 | + ) { |
| 111 | + return; |
| 112 | + } |
| 113 | + log(`Event: ${eventName}`, args); |
| 114 | + }); |
| 115 | + } |
| 116 | +}; |
| 117 | + |
0 commit comments