UNPKG

unified-video-framework

Version:

Cross-platform video player framework supporting iOS, Android, Web, Smart TVs (Samsung/LG), Roku, and more

220 lines 7.67 kB
import { createDynamicAnalyticsManager, createPlayerAnalyticsProviderConfig, AnalyticsProviderType } from '../index'; export function createBasicPlayerAnalytics() { const config = { enabled: true, providers: [ { name: 'flicknexs-analytics', type: AnalyticsProviderType.PLAYER_ANALYTICS, enabled: true, priority: 1, config: createPlayerAnalyticsProviderConfig('https://api.flicknexs.com', 'your-api-key-here', 'video-player-main', { tenantId: 'flicknexs-tenant', heartbeatInterval: 10, batchSize: 10, flushInterval: 30 }) } ], globalSettings: { enableConsoleLogging: true, enableErrorReporting: true, sessionTimeout: 60 } }; return createDynamicAnalyticsManager(config); } export function createMultiProviderAnalytics() { const config = { enabled: true, providers: [ { name: 'player-analytics', type: AnalyticsProviderType.PLAYER_ANALYTICS, enabled: true, priority: 1, config: createPlayerAnalyticsProviderConfig(process.env.ANALYTICS_BASE_URL || 'https://api.flicknexs.com', process.env.ANALYTICS_API_KEY || 'your-api-key', 'main-player', { tenantId: process.env.TENANT_ID, heartbeatInterval: 10, batchSize: 10, flushInterval: 30 }) }, { name: 'google-analytics', type: AnalyticsProviderType.CUSTOM, enabled: true, priority: 2, config: { factory: (config) => new CustomGoogleAnalyticsProvider(config), measurementId: 'G-XXXXXXXXXX', apiSecret: 'your-ga4-secret' } } ], globalSettings: { enableConsoleLogging: process.env.NODE_ENV !== 'production', enableErrorReporting: true, sessionTimeout: 90 } }; return createDynamicAnalyticsManager(config); } export class VideoPlayerWithAnalytics { constructor(analyticsConfig) { this.currentVideoInfo = null; this.analyticsManager = createDynamicAnalyticsManager(analyticsConfig); this.setupAnalyticsEvents(); } loadVideo(videoInfo) { this.currentVideoInfo = videoInfo; const sessionId = this.analyticsManager.startSession(videoInfo, { userId: this.getCurrentUserId(), appVersion: '1.0.0', platform: 'web' }); console.log(`Started analytics session: ${sessionId}`); } play() { this.analyticsManager.trackEvent('play', this.getPlayerState()); } pause() { this.analyticsManager.trackEvent('pause', this.getPlayerState()); } seek(time) { const currentTime = this.getPlayerState().currentTime; this.analyticsManager.trackEvent('seeking', this.getPlayerState(), { seekFrom: currentTime, seekTo: time }); } onError(error) { this.analyticsManager.trackCustomEvent('error', { errorCode: error.code, errorMessage: error.message, errorType: 'player_error', currentTime: this.getPlayerState().currentTime }); } onQualityChange(newQuality, previousQuality) { this.analyticsManager.trackCustomEvent('quality_change', { newQuality, previousQuality, reason: 'user_selected' }); } onChapterSkip(chapterId, chapterType) { this.analyticsManager.trackCustomEvent('custom_chapter_skip', { chapterId, chapterType, skipReason: 'manual' }); } destroy() { this.analyticsManager.endSession(); this.analyticsManager.destroy(); } setupAnalyticsEvents() { this.analyticsManager.on('sessionStart', (data) => { console.log('Analytics session started:', data); }); this.analyticsManager.on('sessionEnd', (data) => { console.log('Analytics session ended:', data); }); this.analyticsManager.on('error', (error) => { console.error('Analytics error:', error); }); } getPlayerState() { return { currentTime: 120, duration: 3600, volume: 0.8, muted: false, paused: false, ended: false, fullscreen: false, playbackRate: 1, qualityLevel: '1080p', buffered: null, seekable: null, networkState: 2, readyState: 4 }; } getCurrentUserId() { return 'user123'; } } class CustomGoogleAnalyticsProvider { constructor(config) { this.sessionId = null; this.config = config; } startSession(videoInfo, customData) { this.sessionId = `ga_${Date.now()}`; gtag('event', 'video_start', { video_title: videoInfo.title, video_id: videoInfo.id, custom_parameter_1: customData?.userId }); return this.sessionId; } async endSession() { if (this.sessionId) { gtag('event', 'video_end', { session_id: this.sessionId }); this.sessionId = null; } } trackEvent(event, playerState, videoInfo) { gtag('event', `video_${event.eventType}`, { video_id: event.videoId, current_time: event.currentTime, session_id: this.sessionId }); } trackCustomEvent(eventType, data) { gtag('event', eventType, { ...data, session_id: this.sessionId }); } async flush() { } getCurrentSession() { return { sessionId: this.sessionId }; } destroy() { this.sessionId = null; } } export function createEnvironmentAnalytics() { const isDevelopment = process.env.NODE_ENV === 'development'; const isProduction = process.env.NODE_ENV === 'production'; const config = { enabled: !isDevelopment || process.env.ENABLE_DEV_ANALYTICS === 'true', providers: [ { name: 'player-analytics', type: AnalyticsProviderType.PLAYER_ANALYTICS, enabled: true, priority: 1, config: createPlayerAnalyticsProviderConfig(process.env.ANALYTICS_BASE_URL || 'https://api.flicknexs.com', process.env.ANALYTICS_API_KEY || '', process.env.PLAYER_ID || 'default-player', { tenantId: process.env.TENANT_ID, heartbeatInterval: isDevelopment ? 5 : 10, batchSize: isDevelopment ? 5 : 10, flushInterval: isDevelopment ? 15 : 30 }) } ], globalSettings: { enableConsoleLogging: isDevelopment, enableErrorReporting: true, sessionTimeout: isProduction ? 120 : 30 } }; return createDynamicAnalyticsManager(config); } //# sourceMappingURL=DynamicAnalyticsExample.js.map