Skip to main content

API Reference

Complete reference for the public API of ocm_ads, imported with package:ocm_ads/ocm_ads.dart.


📦 OcmFlutterPlugin​

Static entry point for initializing and configuring the SDK.

initializeWithConfig()​

Initializes the SDK with a local config. Call it once, before loading any ads.

static Future<void> initializeWithConfig(
OcmConfig config, {
String? prebidAccountId,
String? prebidServerUrl,
String? gdprConsentString,
bool? isGdprApplicable,
})
ParameterTypeRequiredDescription
configOcmConfigYesAd units, GAM settings, and privacy. See Configuration.
prebidAccountIdString?AndroidPrebid Server account ID. Required on Android; on iOS, Prebid is skipped when it's omitted.
prebidServerUrlString?NoPrebid Server endpoint. Android only.
gdprConsentStringString?NoTCF consent string. Android only.
isGdprApplicablebool?NoWhether GDPR applies. Android only.

Throws PlatformException with code missing_argument (no prebidAccountId on Android), invalid_config, or initialize_failed.

initialize()​

Initializes the SDK with an OCM publisher ID, fetching the ad unit configuration remotely.

static Future<void> initialize({
required String publisherId,
String? gdprConsentString,
bool? isGdprApplicable,
String? prebidServerUrl,
})
ParameterTypeRequiredDescription
publisherIdStringYesYour OCM publisher ID.
gdprConsentStringString?NoTCF consent string. Android only.
isGdprApplicablebool?NoWhether GDPR applies. Android only.
prebidServerUrlString?NoOverrides the default Prebid Server. Android only.

Throws PlatformException with code missing_argument or initialize_failed.

setPrebidDebug()​

static Future<void> setPrebidDebug(bool enabled)

Turns Prebid debug mode on or off for subsequent auction requests. Keep it disabled in production.

setPbsEventLogging()​

static Future<void> setPbsEventLogging(bool enabled)

Turns logging of raw Prebid Server requests and responses on or off. Logs may contain request identifiers, so keep it disabled in production.


⚙️ Configuration Types​

OcmConfig​

const OcmConfig({
required List<OcmAdUnit> adUnits,
required OcmGamConfig gam,
required OcmPrivacyConfig privacy,
})

OcmAdUnit​

const OcmAdUnit({
required String id,
required String format,
required String size,
required String position,
required int refresh,
OcmRenderMode renderMode = OcmRenderMode.prebidGam,
})

OcmGamConfig​

const OcmGamConfig({required String networkCode, required String adUnitPath})

OcmPrivacyConfig​

const OcmPrivacyConfig({required int gdpr, required String ccpa, required int coppa})

See Configuration for what each field means.

OcmRenderMode​

ValueDescription
prebidGamFetch demand from Prebid and render it through Google Ad Manager. Default.
prebidFetch and render demand directly through Prebid Mobile. The ad unit id must be the Prebid configuration ID.

OcmNativeLoadMode​

ValueDescription
gamLoad native demand through Google Ad Manager, using a GAM custom native format. Requires gamAdUnitId and gamNativeFormatId. Default.
prebidLoad native demand directly from Prebid.

🖼️ Banner Ads​

OcmBannerView​

A widget that shows a banner for an ad unit from the OCM configuration.

const OcmBannerView({
Key? key,
required String configId,
double? width,
double? height,
VoidCallback? onAdLoaded,
OcmAdFailureCallback? onAdFailed,
VoidCallback? onAdClicked,
VoidCallback? onImpression,
})
PropertyDescription
configIdThe ad unit id from the OCM configuration.
width, heightBanner size in logical pixels.
onAdLoadedCalled when the banner loads.
onAdFailedCalled with an OcmAdError when the banner fails to load.
onAdClickedCalled when the user taps the banner.
onImpressionCalled when the banner records an impression.

On platforms other than Android and iOS it renders an empty box and reports an unsupported_platform error through onAdFailed.


🎬 Interstitial Ads​

OcmInterstitialAd​

A full-screen interstitial. Load it, show it once isReady() returns true, and dispose it when it's no longer needed.

OcmInterstitialAd.load()​

static Future<OcmInterstitialAd> load({
required String prebidConfigId,
required String gamAdUnitId,
Duration? fetchTimeout,
Duration? autoRefreshInterval,
OcmInterstitialListener? listener,
})
ParameterTypeRequiredDescription
prebidConfigIdStringYesPrebid configuration ID for the interstitial.
gamAdUnitIdStringYesGAM ad unit path, for example '/75351959/testadunit/test_app_interstitial'.
fetchTimeoutDuration?NoLimits the bid request.
autoRefreshIntervalDuration?NoReloads the ad periodically.
listenerOcmInterstitialListener?NoReceives load and display events.

Returns an OcmInterstitialAd handle as soon as loading starts. Use onLoaded or isReady() to know when it can be shown.

Instance methods​

MethodReturnsDescription
isReady()Future<bool>Whether the interstitial has loaded and can be shown.
show()Future<void>Shows the interstitial. Throws no_activity if there's no visible Activity / view controller, or not_found if the ad was disposed.
dispose()Future<void>Releases the native resources held by this ad.

OcmInterstitialListener​

Extend it and override only the events you need.

MethodDescription
onLoaded()The interstitial loaded.
onFailedToLoad(OcmAdError error)The interstitial failed to load.
onShown()The interstitial is shown.
onClosed()The user closed the interstitial.
onClicked()The user tapped the interstitial.

🏆 Rewarded Ads​

OcmRewardedAd​

A full-screen rewarded ad. Load it, optionally call applyServerSideVerification, then show it and dispose it when it's no longer needed.

OcmRewardedAd.load()​

static Future<OcmRewardedAd> load({
required String adUnitId,
OcmRewardedListener? listener,
})
ParameterTypeRequiredDescription
adUnitIdStringYesGoogle Mobile Ads rewarded ad unit ID, for example 'ca-app-pub-3940256099942544/5224354917' (Google's Android test unit).
listenerOcmRewardedListener?NoReceives load, display, and reward events.

Returns an OcmRewardedAd handle as soon as loading starts. Show it after onLoaded.

Instance methods​

MethodReturnsDescription
applyServerSideVerification({required String userId, String? customData})Future<void>Sets the user ID and optional custom data sent with server-side verification callbacks. Call it before show().
show()Future<void>Shows the rewarded ad. Throws no_activity or not_found like interstitials.
dispose()Future<void>Releases the native resources held by this ad.

OcmRewardedListener​

MethodDescription
onLoaded()The rewarded ad loaded.
onFailedToLoad(OcmAdError error)The rewarded ad failed to load.
onShowFailed(OcmAdError error)The rewarded ad failed to show.
onDisplayed()The rewarded ad is displayed.
onDismissed()The user dismissed the rewarded ad.
onUserEarnedReward(OcmRewardedResult reward)The user earned the reward.

OcmRewardedResult​

FieldTypeDescription
adUnitIdStringThe rewarded ad unit that granted the reward.
rewardTypeStringThe reward type configured for the ad unit.
rewardAmountintThe reward amount.
shownAtMillisintWhen the ad was shown, in milliseconds since the Unix epoch.

📱 Native Ads​

OcmNativeAdView​

A widget that loads and renders a native ad, with impression and click tracking. Use it for every native ad shown to users.

const OcmNativeAdView({
Key? key,
required String prebidConfigId,
String? gamAdUnitId,
String? gamNativeFormatId,
OcmNativeLoadMode mode = OcmNativeLoadMode.gam,
double? width,
double? height,
VoidCallback? onAdLoaded,
OcmAdFailureCallback? onAdFailed,
})
PropertyDescription
prebidConfigIdPrebid configuration ID for the native placement.
gamAdUnitIdGAM ad unit ID. Required in OcmNativeLoadMode.gam.
gamNativeFormatIdGAM custom native format ID. Required in OcmNativeLoadMode.gam.
modeHow the native bid is loaded and rendered. Defaults to OcmNativeLoadMode.gam.
widthView width in logical pixels.
heightView height in logical pixels. On Android, leave it null to size the view to the rendered ad. On iOS, give it a bounded height.
onAdLoadedCalled when the native ad loads.
onAdFailedCalled with an OcmAdError when the native ad fails to load.

The view reloads when prebidConfigId, gamAdUnitId, gamNativeFormatId, or mode change. On iOS it also follows the app theme's brightness.

OcmNativeAdLoader (Android only)​

Loads native ad data without rendering it. It does not register impression or click tracking, so use OcmNativeAdView for ads shown to users.

const OcmNativeAdLoader({OcmNativeListener? listener})

Future<OcmNativeAd> load({
required String prebidConfigId,
String? gamAdUnitId,
String? gamNativeFormatId,
OcmNativeLoadMode mode = OcmNativeLoadMode.gam,
})

The GAM IDs are required in OcmNativeLoadMode.gam. On Android, load throws missing_argument when they're missing, or native_load_failed. On iOS it throws unsupported_platform: the iOS SDK renders native ads as views and doesn't expose asset data.

OcmNativeListener​

MethodDescription
onLoaded(OcmNativeAd ad)Called with the loaded native ad.
onFailedToLoad(OcmAdError error)The native ad failed to load.

OcmNativeAd​

Native ad assets returned by OcmNativeAdLoader. Every field may be null.

FieldTypeDescription
titleString?The headline.
bodyString?The body text.
publisherCompanyString?The advertiser or publisher name.
callToActionString?The call-to-action label, for example Install.
imageUrlString?The main image URL.
iconUrlString?The icon URL.
videoUrlString?The video URL.
layoutTypeString?The layout type reported by the SDK.

⚠️ Error Handling​

The plugin reports errors in two ways:

  • Method calls (initializeWithConfig, load, show, …) throw a PlatformException from package:flutter/services.dart.
  • Ad callbacks and listeners receive an OcmAdError.

OcmAdError​

FieldTypeDescription
codeString?A machine-readable error code, when the SDK provides one.
messageStringA human-readable description of the error.

OcmAdFailureCallback is void Function(OcmAdError error).

Error Codes​

CodeWhen
missing_argumentA required argument is missing, for example prebidAccountId in initializeWithConfig on Android, or the GAM IDs for a GAM native load.
invalid_configThe OcmConfig couldn't be parsed.
initialize_failedThe native SDK failed to initialize.
no_activityshow() was called with no visible Activity (Android) or view controller (iOS).
not_foundThe interstitial or rewarded ad handle doesn't exist, for example after dispose().
native_load_failedOcmNativeAdLoader.load failed on Android.
unsupported_platformThe feature isn't available on this platform: OcmNativeAdLoader on iOS, ad widgets outside Android and iOS, or the iOS SDK wasn't linked (see Installation).
import 'package:flutter/services.dart';

try {
await OcmFlutterPlugin.initializeWithConfig(
config,
prebidAccountId: 'your-prebid-account-id',
);
} on PlatformException catch (error) {
debugPrint('OCM error: ${error.code} ${error.message}');
}

Next Steps​