Configuration
SDK Config
Configure the SDK in your app with an OcmConfig passed to OcmFlutterPlugin.initializeWithConfig. No remote config file is needed.
import 'package:ocm_ads/ocm_ads.dart';
const config = OcmConfig(
adUnits: <OcmAdUnit>[
OcmAdUnit(
id: 'your-banner-config-id', // pass the same value to OcmBannerView's configId
format: 'banner',
size: '300x250',
position: 'custom',
refresh: 30,
renderMode: OcmRenderMode.prebidGam, // optional, this is the default
),
OcmAdUnit(
id: 'your-prebid-config-id', // direct Prebid: must be the Prebid configuration ID
format: 'banner',
size: '320x50',
position: 'custom',
refresh: 0,
renderMode: OcmRenderMode.prebid,
),
],
gam: OcmGamConfig(
networkCode: '75351959',
adUnitPath: '/75351959/your-ad-unit',
),
privacy: OcmPrivacyConfig(gdpr: 0, ccpa: '1---', coppa: 0),
);
await OcmFlutterPlugin.initializeWithConfig(
config,
prebidAccountId: 'your-prebid-account-id',
);
Ad Unit Fields (OcmAdUnit)
| Field | Type | Default | Description |
|---|---|---|---|
id | String | required | The value you pass to OcmBannerView's configId. With OcmRenderMode.prebid it must be the Prebid configuration ID. |
format | String | required | Ad format, for example 'banner'. |
size | String | required | Width x height, for example '300x250'. |
position | String | required | Placement position, for example 'custom'. |
refresh | int | required | Refresh interval for the ad unit, for example 30. |
renderMode | OcmRenderMode | OcmRenderMode.prebidGam | How the banner is rendered. See Render Modes. |
Render Modes
Each banner ad unit picks how its winning bid is rendered with renderMode:
renderMode | What it does |
|---|---|
OcmRenderMode.prebidGam (default) | Fetches demand from Prebid and renders it through Google Ad Manager (gam.adUnitPath). |
OcmRenderMode.prebid | Fetches and renders the banner directly through Prebid Mobile, with no GAM. The ad unit id must be the Prebid configuration ID. |
Google Ad Manager (OcmGamConfig)
| Field | Type | Description |
|---|---|---|
networkCode | String | Your Google Ad Manager network code. |
adUnitPath | String | The GAM ad unit path, for example '/1234/my_app/banner'. |
Privacy (OcmPrivacyConfig)
| Field | Type | Description |
|---|---|---|
gdpr | int | 1 when GDPR applies to the user, 0 otherwise. |
ccpa | String | US Privacy (CCPA) string, for example '1---'. |
coppa | int | 1 when the app is directed at children under COPPA, 0 otherwise. |
Privacy is set at initialization. Get GDPR applicability and consent from your consent management platform (CMP) before calling initializeWithConfig and build privacy from it. On Android, also pass isGdprApplicable and gdprConsentString in the initialize options.
Initialize Options
initializeWithConfig takes these optional named arguments after the config:
| Option | Type | Platforms | Description |
|---|---|---|---|
prebidAccountId | String? | Android, iOS | Prebid Server account ID. Required on Android (missing_argument otherwise). On iOS, Prebid is skipped when it's omitted. |
prebidServerUrl | String? | Android | Overrides the default Prebid Server endpoint. |
gdprConsentString | String? | Android | TCF consent string. |
isGdprApplicable | bool? | Android | Whether GDPR applies to the user. |
await OcmFlutterPlugin.initializeWithConfig(
config,
prebidAccountId: 'your-prebid-account-id',
gdprConsentString: tcfString, // from your CMP
isGdprApplicable: true,
);
Remote Configuration
Instead of a local config, you can initialize with your OCM publisher ID and let the SDK fetch the ad unit configuration:
await OcmFlutterPlugin.initialize(
publisherId: 'your-publisher-id',
);
initialize also accepts gdprConsentString, isGdprApplicable, and prebidServerUrl, which are used on Android.
🐞 Debugging
Two switches help while testing. Both are off unless you turn them on:
// Prebid debug mode for subsequent auction requests.
await OcmFlutterPlugin.setPrebidDebug(true);
// Logs raw Prebid Server requests and responses, without debug mode.
await OcmFlutterPlugin.setPbsEventLogging(true);
Keep both disabled in production. Raw Prebid Server logs may contain request identifiers and other integration data.
OcmBannerView Properties
| Property | Type | Required | Description |
|---|---|---|---|
configId | String | Yes | The id of an ad unit in the config passed to initializeWithConfig. |
width | double? | No | Banner width in logical pixels. |
height | double? | No | Banner height in logical pixels. |
onAdLoaded | VoidCallback? | No | Called when the banner loads. |
onAdFailed | void Function(OcmAdError)? | No | Called when the banner fails to load. |
onAdClicked | VoidCallback? | No | Called when the user taps the banner. |
onImpression | VoidCallback? | No | Called when the banner records an impression. |
Always set both width and height, matching the ad unit's size, so the banner renders at the right size.
Common Ad Sizes
| Size | Name | Typical placement |
|---|---|---|
| 320x50 | Mobile banner | Top or bottom of the screen |
| 320x100 | Large mobile banner | Top or bottom of the screen |
| 300x250 | Medium rectangle | In content |
| 728x90 | Leaderboard | Tablets |
Set the size both on the ad unit (size: '320x50') and on the widget (width: 320, height: 50).
Best Practices
1. List Every Banner in the Config
OcmBannerView looks its configId up in the config. Add every banner your app shows to adUnits.
2. Hide Failed Slots
Handle onAdFailed and hide or replace the ad slot so the layout doesn't show an empty box:
bool _bannerFailed = false;
Widget build(BuildContext context) {
if (_bannerFailed) return const SizedBox.shrink();
return OcmBannerView(
configId: 'your-banner-config-id',
width: 300,
height: 250,
onAdFailed: (_) => setState(() => _bannerFailed = true),
);
}
Next Steps
- Explore the API Reference
- Check out complete Examples