Skip to content

Best practices

This page gathers the practices that avoid the most common integration mistakes, together with the reason behind each one.

  • Initialize the SDK once, when the app starts. Call RNWortiseSdk.initialize from an effect of the root component, and never from the body of a component, where it would be executed again on every render.
  • Load the ads after the initialization. In the first launch of the app, an ad that is requested before calling initialize fails with the SDK_NOT_INITIALIZED error. From any other component, RNWortiseSdk.wait() returns a Promise that is resolved once the SDK is ready.
  • Initialize from JavaScript even if Android is initialized natively. The native initialization in the MainApplication class does not initialize the SDK on iOS.
useEffect(() => {
RNWortiseSdk.wait().then(() => {
RNWortiseInterstitial.setAdUnitId('Wortise Ad Unit ID');
RNWortiseInterstitial.loadAd();
});
}, []);
  • Request the consent before loading the first ad. The SDK never shows the consent dialog on its own: call RNWortiseConsentManager.requestIfRequired() when the app starts, so the ad requests already include the choice of the user.
  • Request it from a component. The dialog needs a visible screen. If there is none (for example, while the JavaScript code is being loaded), the Promise is resolved with false and nothing is shown.
  • Do not read the resolved value as the answer of the user. It only indicates if the dialog has been shown.
  • Use request() only for a settings option. It always shows the dialog, while requestIfRequired() only shows it when it is needed.
  • On iOS, if your app requests the App Tracking Transparency authorization, do it before the first ad is requested. Requesting it is optional, and neither the plugin nor the SDK shows this prompt. See SDK integration.
  • Set the child-directed treatment and the content rating before initializing the SDK. See Privacy.
  • Call setAdUnitId() once per format, before loadAd(). loadAd() does nothing until an ad unit has been set, and every call to setAdUnitId() discards the ad that was already loaded.
  • Use a single ad unit per fullscreen format. RNWortiseInterstitial, RNWortiseRewarded and RNWortiseAppOpen hold one ad each, so alternating between two ad units discards the loaded ads.
  • Set the ad unit from an effect, not at module level. On Android, the ad is released when the activity is destroyed. Code at module level is not executed again when the activity is created again, so loadAd() would no longer do anything.
  • Do not call loadAd() while an ad is already loaded. The loaded ad is discarded and a new one is requested, so calling it on every screen or render wastes the ads that were ready to be shown.
  • Load the next ad when the previous one is dismissed. A fullscreen ad can only be shown once. For App Open ads, setAutoReload(true) does this automatically.
  • Do not call loadAd() again right after a failure. After a failed load, the SDK ignores the new requests during a cooldown (30 seconds by default) and no event is sent for them. The isInCooldown and cooldownRemainingMs getters report this state.
  • Use await with the getters. isAvailable, isShowing and isInCooldown return a Promise, so a condition without await is always true.
  • Check isAvailable before calling showAd(). If there is no ad available, nothing is shown and no event is sent, so a flow that waits for the dismissed event would never continue. The value that showAd() resolves with does not indicate if the ad has been shown.
  • Grant the reward in onRewardedCompleted, when success is true. The dismissed and shown events are also sent when the user has not completed the ad.
if (await RNWortiseRewarded.isAvailable) {
RNWortiseRewarded.showAd();
} else {
// Continue without showing the ad
}
  • Call loadAd() through the reference of the component. WortiseBanner does not load an ad when it is mounted or when its properties change.
  • Do not call loadAd() from onFailedToLoad. The banner already retries by itself after one minute, and refreshes its ad automatically.
  • Specify autoRefreshTime in milliseconds. The SDK keeps the value between 30 and 120 seconds.
  • Read the data of the listeners from nativeEvent. For example, event.nativeEvent.name in onFailedToLoad.
  • Pass an integer width to the adaptive sizes. On iOS, a width with decimals makes the banner not load. Use Math.floor(width).
  • Keep the banner mounted. Every time a WortiseBanner is mounted (for example, in the rows of a list or in a conditional block), a new banner is created and a new ad is requested.
  • Show the ad when the app returns from the background. The plugin has no component that does it automatically, so use AppState together with tryToShowAd(), which shows the ad if it is available and requests a new one otherwise.
  • Do not show it when the app returns from another fullscreen ad. On Android, AppState also changes to background and back to active while an interstitial or rewarded ad is displayed. Skip that case (for example, with a flag that is set before calling their showAd() method), or two ads would be shown in a row.
const appState = useRef(AppState.currentState);
// Set to true before showing an interstitial or rewarded ad
const skipNextAppOpen = useRef(false);
useEffect(() => {
const subscription = AppState.addEventListener('change', (nextState) => {
if (appState.current === 'background' && nextState === 'active') {
if (!skipNextAppOpen.current) {
RNWortiseAppOpen.tryToShowAd();
}
skipNextAppOpen.current = false;
}
appState.current = nextState;
});
return () => subscription.remove();
}, []);
  • Register the factory on both platforms with the same identifier. If no factory is registered with the identifier passed to WortiseNativeAdView, the component is displayed empty and no error is reported.
  • Destroy the ads that are no longer displayed. Each WortiseNativeAd keeps its native resources until destroy() is called, including when the component is unmounted.
  • Give the media view at least 120x120. Smaller sizes make the ad ineligible for video demand.
  • Register the listeners in an effect and remove them in its cleanup. addEventListener adds a new listener on every call, so registering it in the body of a component multiplies the callbacks (and the rewards) on every render.
  • Do not use removeAllListeners() from a component. It also removes the listeners that other components have registered for that event.
useEffect(() => {
const subscription = RNWortiseInterstitial.addEventListener('onInterstitialDismissed', () => {
RNWortiseInterstitial.loadAd();
});
return () => subscription.remove();
}, []);
  • Use the test ad units or the test mode during development. This way, the development of the app does not generate impressions and clicks on real ads.
  • Enable the test mode with RNWortiseAdSettings.setTestEnabled(__DEV__). The value is not stored, so it must be set on every launch, and __DEV__ keeps it disabled in the release builds.
  • Test on a real build of both platforms. The SDK contains native code, so it does not work in Expo Go.
  • The Google App ID of each platform is the one shown in the Wortise dashboard.
  • On iOS, the Info.plist file contains the SKAdNetworkItems key and, if the app requests the tracking authorization, the NSUserTrackingUsageDescription key.
  • The app uses its own ad unit IDs (none of the test- ones) and the test mode is disabled in the release build.
  • The consent is requested when the app starts, before loading the first ad.
  • The SDK is initialized once from JavaScript, and the ads are loaded after it.
  • Every banner calls loadAd(), and every fullscreen ad checks isAvailable with await before being shown.
  • The reward is only granted in onRewardedCompleted when success is true.
  • The listeners are removed and the native ads are destroyed when their components are unmounted.
  • The release build has been tested on both platforms, and the SDK logs show that the ads are loaded.