Skip to main content

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

ParameterTypeDescription
publisherIdstringYour 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

ParameterTypeDescription
adUnitIdstringThe 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

ParameterTypeRequiredDescription
options.prebidConfigAdIdstringNoPrebid configuration ID
options.gamAdUnitIdstringYesGoogle 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.

PropTypeRequiredDescription
styleViewStyleYesView style (must include width/height)
adUnitIdstringYes*Ad unit identifier
prebidConfigAdIdstringNoPrebid configuration ID
gamAdUnitIdstringNo**GAM ad unit ID
format'banner' | 'native'NoAd format (default: 'banner')
refreshIntervalnumberNoAuto-refresh interval in seconds (Android only)
onAdEventfunctionNoAd 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 TypeDescriptionError Field
loadedAd successfully loaded-
failedAd failed to loaderror message included
clickedUser tapped the ad-
impressionAd impression tracked-
native_loadedNative 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

// 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