API Reference
Complete API documentation for all methods and components in the React Native OCM AdNetwork module.
📦 Module Functions
initialize()
Initializes the SDK. Must be called before loading any ads.
Signature
initialize(publisherId: string): Promise<void>
Parameters
| Parameter | Type | Description |
|---|---|---|
publisherId | string | Your publisher ID (e.g., 'test_pub_001') |
Returns
Promise<void> - Resolves when SDK is initialized
Behavior
- First call: Initializes the SDK asynchronously
- Subsequent calls: Resolve immediately if already initialized
- Thread-safe: Safe to call multiple times
Example
import { initialize } from 'react-native-ocm-adnetwork-module';
// In your root component
React.useEffect(() => {
initialize('test_pub_001')
.then(() => console.log('✅ SDK initialized'))
.catch(error => console.error('❌ Init failed:', error));
}, []);
loadRewarded()
Loads a rewarded ad for the provided ad unit ID.
Signature
loadRewarded(adUnitId: string): Promise<void>
Parameters
| Parameter | Type | Description |
|---|---|---|
adUnitId | string | The rewarded ad unit ID |
Returns
Promise<void> - Resolves when ad is loaded
Throws
Rejects if:
- SDK not initialized
- Invalid ad unit ID
- Network error
- No ad inventory available
Example
import { loadRewarded } from 'react-native-ocm-adnetwork-module';
const loadRewardedAd = async () => {
try {
await loadRewarded('ca-app-pub-3940256099942544/5224354917');
console.log('✅ Rewarded ad loaded');
} catch (error) {
console.error('❌ Failed to load:', error);
}
};
showRewarded()
Presents the previously loaded rewarded ad.
Signature
showRewarded(): Promise<void>
Returns
Promise<void> - Resolves when ad is dismissed
Throws
Rejects if:
- No ad loaded (must call
loadRewarded()first) - Ad already shown
- Presentation error
Example
import { loadRewarded, showRewarded } from 'react-native-ocm-adnetwork-module';
const showRewardedAd = async () => {
try {
await loadRewarded('ca-app-pub-3940256099942544/5224354917');
await showRewarded();
console.log('🏆 User earned reward!');
} catch (error) {
console.error('❌ Error:', error);
}
};
loadInterstitial()
Loads an interstitial ad for the provided configuration.
Signature
loadInterstitial(options: {
prebidConfigAdId?: string;
gamAdUnitId: string;
}): Promise<void>
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
options.prebidConfigAdId | string | No | Prebid configuration ID |
options.gamAdUnitId | string | Yes | Google Ad Manager ad unit ID |
Returns
Promise<void> - Resolves when ad is loaded
Example
import { loadInterstitial } from 'react-native-ocm-adnetwork-module';
const loadInterstitialAd = async () => {
try {
await loadInterstitial({
prebidConfigAdId: '1001-sreq-test-300x250-imp-1',
gamAdUnitId: '/75351959/testadunit/test_app_interstitial',
});
console.log('✅ Interstitial ad loaded');
} catch (error) {
console.error('❌ Failed to load:', error);
}
};
showInterstitial()
Presents the previously loaded interstitial ad.
Signature
showInterstitial(): Promise<void>
Returns
Promise<void> - Resolves when ad is dismissed
Throws
Rejects if:
- No ad loaded (must call
loadInterstitial()first) - Ad already shown
- Presentation error
Example
import { loadInterstitial, showInterstitial } from 'react-native-ocm-adnetwork-module';
const showInterstitialAd = async () => {
try {
await loadInterstitial({
gamAdUnitId: '/75351959/testadunit/test_app_interstitial',
});
await showInterstitial();
console.log('✅ Interstitial shown');
} catch (error) {
console.error('❌ Error:', error);
}
};
🖼️ OcmAdView Component
React Native component for displaying banner and native ads.
Props
See Configuration for detailed prop documentation.
| Prop | Type | Required | Description |
|---|---|---|---|
style | ViewStyle | Yes | View style (must include width/height) |
adUnitId | string | Yes* | Ad unit identifier |
prebidConfigAdId | string | No | Prebid configuration ID |
gamAdUnitId | string | No** | GAM ad unit ID |
format | 'banner' | 'native' | No | Ad format (default: 'banner') |
refreshInterval | number | No | Auto-refresh interval in seconds (Android only) |
onAdEvent | function | No | Ad lifecycle event callback |
* Optional when prebidConfigAdId is provided
** Required for native format
Ref Methods
Access component methods via ref:
interface OcmAdViewHandle {
loadBanner(): void;
}
loadBanner()
Manually requests a banner ad on the component.
import React from 'react';
import { OcmAdView, type OcmAdViewHandle } from 'react-native-ocm-adnetwork-module';
const MyComponent = () => {
const bannerRef = React.useRef<OcmAdViewHandle>(null);
const reloadAd = () => {
bannerRef.current?.loadBanner();
};
return (
<>
<OcmAdView
ref={bannerRef}
adUnitId="1001-sreq-test-300x250-imp-1"
style={{ width: 300, height: 250 }}
/>
<Button title="Reload Banner" onPress={reloadAd} />
</>
);
};
📡 Event Types
AdEvent
Event object passed to onAdEvent callback:
interface AdEvent {
nativeEvent: {
type: AdEventType;
error?: string;
};
}
type AdEventType =
| 'loaded'
| 'failed'
| 'clicked'
| 'impression'
| 'native_loaded';
Event Descriptions
| Event Type | Description | Error Field |
|---|---|---|
loaded | Ad successfully loaded | - |
failed | Ad failed to load | error message included |
clicked | User tapped the ad | - |
impression | Ad impression tracked | - |
native_loaded | Native ad loaded (iOS only) | - |
Example Event Handler
const handleAdEvent = (event) => {
const { type, error } = event.nativeEvent;
switch (type) {
case 'loaded':
setAdLoaded(true);
break;
case 'failed':
console.error('Ad failed:', error);
setAdLoaded(false);
// Show fallback content
break;
case 'clicked':
// Track analytics
analytics.track('ad_clicked');
break;
case 'impression':
// Track impression
analytics.track('ad_impression');
break;
case 'native_loaded':
console.log('Native ad loaded (iOS)');
break;
}
};
🔧 TypeScript Types
Import TypeScript types for better type safety:
import type {
OcmAdViewHandle,
AdEventType,
} from 'react-native-ocm-adnetwork-module';
Available Types
// Ref handle for OcmAdView
interface OcmAdViewHandle {
loadBanner(): void;
}
// Ad event types
type AdEventType =
| 'loaded'
| 'failed'
| 'clicked'
| 'impression'
| 'native_loaded';
// Interstitial load options
interface InterstitialOptions {
prebidConfigAdId?: string;
gamAdUnitId: string;
}
🔄 Complete Workflow Examples
Banner Ad Workflow
// 1. Initialize SDK
await initialize('test_pub_001');
// 2. Render component
<OcmAdView
ref={bannerRef}
adUnitId="1001-sreq-test-300x250-imp-1"
onAdEvent={handleAdEvent}
style={{ width: 300, height: 250 }}
/>
// 3. Manually reload (optional)
bannerRef.current?.loadBanner();
Rewarded Ad Workflow
// 1. Load ad
await loadRewarded('ca-app-pub-3940256099942544/5224354917');
// 2. Show ad
await showRewarded();
// 3. Grant reward
grantUserReward();
// 4. Preload next ad
await loadRewarded('ca-app-pub-3940256099942544/5224354917');
Interstitial Ad Workflow
// 1. Load ad
await loadInterstitial({
gamAdUnitId: '/75351959/testadunit/test_app_interstitial',
});
// 2. Show ad at natural break point
await showInterstitial();
// 3. Preload next ad
await loadInterstitial({
gamAdUnitId: '/75351959/testadunit/test_app_interstitial',
});
Next Steps
- Check out complete Examples
- Learn about Configuration options