/** * BigCrunch Mobile Ads SDK for React Native * Main API class */ import { NativeBigCrunchAds, BigCrunchAdsEventEmitter, NativeEventNames, } from './NativeBigCrunchAds'; import type { InitializationOptions, Environment, AccountType, SessionInfo, DeviceContext, AppConfig, EventSubscription, ScreenViewOptions, } from './types'; /** * Main entry point for BigCrunch Mobile Ads SDK */ export class BigCrunchAds { private static isInitializedFlag = false; private static initializationPromise: Promise | null = null; /** * Initialize the BigCrunch Ads SDK * Must be called before using any other SDK features * * @param propertyId - Your BigCrunch property ID * @param environment - Environment to use (default: 'production') * @param options - Additional initialization options * * @example * ```typescript * await BigCrunchAds.initialize( * 'your-property-id', * 'production' * ); * ``` */ static async initialize( propertyId: string, environment: Environment = 'production', options?: Partial ): Promise { if (this.isInitializedFlag) { console.warn('BigCrunchAds: SDK is already initialized'); return this.initializationPromise!; } const initOptions: InitializationOptions = { propertyId, environment, ...options, }; this.initializationPromise = NativeBigCrunchAds.initialize(initOptions) .then(() => { this.isInitializedFlag = true; console.log('BigCrunchAds: SDK initialized successfully'); }) .catch((error) => { this.isInitializedFlag = false; this.initializationPromise = null; throw error; }); return this.initializationPromise; } /** * Check if the SDK is initialized */ static async isInitialized(): Promise { if (this.isInitializedFlag) { return true; } return NativeBigCrunchAds.isInitialized(); } /** * Track a screen view for analytics * * @param screenName - Name of the screen being viewed * @param options - Optional overrides for page URL, content metadata, and custom dimensions * * @example * ```typescript * // Simple usage * BigCrunchAds.trackScreenView('HomeScreen'); * * // With options * BigCrunchAds.trackScreenView('ArticlePage', { * pageUrl: 'https://example.com/articles/123', * pageMeta: { * title: 'My Article Title', * author: 'Jane Smith', * articleSection: 'Technology', * keywords: 'tech,mobile,apps', * }, * customDimensions: { * content_type: 'article', * content_id: '123', * }, * }); * ``` */ static async trackScreenView( screenName: string, options?: ScreenViewOptions ): Promise { this.ensureInitialized(); return NativeBigCrunchAds.trackScreenView(screenName, options ?? null); } /** * Get the current app configuration * Returns null if config hasn't been loaded yet */ static async getAppConfig(): Promise { this.ensureInitialized(); return NativeBigCrunchAds.getAppConfig(); } /** * Force refresh the app configuration from backend */ static async refreshConfig(): Promise { this.ensureInitialized(); return NativeBigCrunchAds.refreshConfig(); } /** * Get current session information */ static async getSessionInfo(): Promise { this.ensureInitialized(); const info: any = await NativeBigCrunchAds.getSessionInfo(); // Native modules send the session start timestamp as `startTime` return { ...info, sessionStartTime: info.sessionStartTime ?? info.startTime, }; } /** * Start a new session (usually handled automatically) */ static async startNewSession(): Promise { this.ensureInitialized(); return NativeBigCrunchAds.startNewSession(); } /** * Get device context information */ static async getDeviceContext(): Promise { return NativeBigCrunchAds.getDeviceContext(); } /** * Set GDPR consent string * * @param consent - GDPR consent string */ static async setGdprConsent(consent: string): Promise { return NativeBigCrunchAds.setGdprConsent(consent); } /** * Set CCPA compliance string * * @param ccpaString - CCPA string (e.g., "1YNN") */ static async setCcpaString(ccpaString: string): Promise { return NativeBigCrunchAds.setCcpaString(ccpaString); } /** * Set COPPA compliance * * @param isCompliant - Whether the app is COPPA compliant */ static async setCoppaCompliant(isCompliant: boolean): Promise { return NativeBigCrunchAds.setCoppaCompliant(isCompliant); } /** * Set the account type for the current user * * This value is included in all analytics events (pageviews, impressions, revenue, etc.) * and persists until changed. Defaults to 'guest' if not set. * * @param accountType - The user's account type * * @example * ```typescript * BigCrunchAds.setAccountType('subscriber'); * ``` */ static async setAccountType(accountType: AccountType): Promise { return NativeBigCrunchAds.setAccountType(accountType); } /** * Enable or disable debug mode * * @param enabled - Whether to enable debug mode */ static async setDebugMode(enabled: boolean): Promise { return NativeBigCrunchAds.setDebugMode(enabled); } /** * Add a test device ID for testing ads * * @param deviceId - Device ID to add as test device */ static async addTestDevice(deviceId: string): Promise { return NativeBigCrunchAds.addTestDevice(deviceId); } /** * Remove a test device ID * * @param deviceId - Device ID to remove from test devices */ static async removeTestDevice(deviceId: string): Promise { return NativeBigCrunchAds.removeTestDevice(deviceId); } /** * Get list of test device IDs */ static async getTestDevices(): Promise { return NativeBigCrunchAds.getTestDevices(); } /** * Set UTM parameters for attribution tracking * * These parameters are persisted and used for all analytics events. * Typically set from deep link parameters. * * @param params - UTM parameters object * * @example * ```typescript * BigCrunchAds.setUTMParameters({ * source: 'google', * medium: 'cpc', * campaign: 'summer_sale', * term: 'running shoes', * content: 'variant_a' * }); * ``` */ static async setUTMParameters(params: { source?: string; medium?: string; campaign?: string; term?: string; content?: string; }): Promise { return NativeBigCrunchAds.setUTMParameters(params); } /** * Clear UTM parameters */ static async clearUTMParameters(): Promise { return NativeBigCrunchAds.clearUTMParameters(); } /** * Add a global event listener * * @param eventName - Native event name to listen to * @param listener - Callback function * @returns Subscription object with remove() method */ static addEventListener( eventName: string, listener: (event: any) => void ): EventSubscription { const subscription = BigCrunchAdsEventEmitter.addListener(eventName, listener); return { remove: () => subscription.remove(), }; } /** * Listen for configuration updates */ static onConfigUpdated(listener: (config: AppConfig) => void): EventSubscription { return this.addEventListener(NativeEventNames.CONFIG_UPDATED, listener); } /** * Listen for configuration failures */ static onConfigFailed(listener: (error: Error) => void): EventSubscription { return this.addEventListener(NativeEventNames.CONFIG_FAILED, listener); } /** * Listen for session start events */ static onSessionStarted(listener: (session: SessionInfo) => void): EventSubscription { return this.addEventListener(NativeEventNames.SESSION_STARTED, listener); } /** * Listen for session end events * * Note: the native SDKs do not currently emit this event; the listener is * accepted but will not fire until session-end detection is implemented natively. */ static onSessionEnded(listener: (session: SessionInfo) => void): EventSubscription { return this.addEventListener(NativeEventNames.SESSION_ENDED, listener); } /** * Ensure SDK is initialized before calling methods * @private */ private static ensureInitialized(): void { if (!this.isInitializedFlag && !this.initializationPromise) { throw new Error( 'BigCrunchAds: SDK is not initialized. Call BigCrunchAds.initialize() first.' ); } } } // Export as default for convenience export default BigCrunchAds;