Tutorial: Optimize AdMob ad frequency

Step 3: Handle Remote Config parameter values in your app's code


Introduction: Optimize AdMob ad frequency using Firebase
Step 1: Use AdMob to create new ad unit variants for testing
Step 2: Set up an A/B test in the Firebase console

Step 3: Handle Remote Config parameter values in your app's code

Step 4: Start the A/B test and review the test results in the Firebase console
Step 5: Decide whether to roll out the new ad format


At the end of the last step, you created a Remote Config parameter (INTERSTITIAL_AD_KEY). In this step, you'll add the logic to your app's code for what your app should display based on the value of that parameter.

Add the required SDKs

Before using Remote Config in your application code, add both the Remote Config SDK and the Firebase SDK for Google Analytics to your project build files.

Swift

Add and install the following pods in your podfile:

pod'Google-Mobile-Ads-SDK'pod'Firebase/Analytics'pod'Firebase/RemoteConfig'

Objective-C

Add and install the following pods in your podfile:

pod'Google-Mobile-Ads-SDK'pod'Firebase/Analytics'pod'Firebase/RemoteConfig'

Android

Add the following library dependencies to your build.gradle file:

implementation'com.google.android.gms:play-services-ads:24.2.0'implementation'com.google.firebase:firebase-analytics:22.4.0'implementation'com.google.firebase:firebase-config:22.1.0'

Unity

Download and install the Firebase Unity SDK, then add the following Unity packages to your project:

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

Configure Remote Config instance

To use the Remote Config parameter values, configure the Remote Config instance so that it's set up to fetch new values for the client app instance.

In this example, Remote Config is configured to check for new parameter values once every hour.

Swift

remoteConfig=RemoteConfig.remoteConfig()letsettings=RemoteConfigSettings()settings.minimumFetchInterval=3600remoteConfig.configSettings=settings

Objective-C

self.remoteConfig=[FIRRemoteConfigremoteConfig];FIRRemoteConfigSettings*remoteConfigSettings=[[FIRRemoteConfigSettingsalloc]init];remoteConfigSettings.minimumFetchInterval=3600;self.remoteConfig.configSettings=remoteConfigSettings;

Java

mFirebaseRemoteConfig=FirebaseRemoteConfig.getInstance();FirebaseRemoteConfigSettingsconfigSettings=newFirebaseRemoteConfigSettings.Builder().setMinimumFetchIntervalInSeconds(3600).build();mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);

Kotlin

remoteConfig=Firebase.remoteConfigvalconfigSettings=remoteConfigSettings{minimumFetchIntervalInSeconds=3600}remoteConfig.setConfigSettingsAsync(configSettings)

Unity

varremoteConfig=FirebaseRemoteConfig.DefaultInstance;varconfigSettings=newConfigSettings{MinimumFetchInternalInMilliseconds=(ulong)(newTimeSpan(1,0,0).TotalMilliseconds)};remoteConfig.SetConfigSettingsAsync(configSettings).ContinueWithOnMainThread(task=>{Debug.Log("Config settings confirmed");}

Fetch and activate Remote Config

Fetch and activate the Remote Config parameters so that it can start using the new parameter values.

You'll want to make this call as early as possible in your app's loading phase because this call is asynchronous and you'll need the Remote Config value pre-fetched so that your app knows which ad to show.

Swift

remoteConfig.fetch(){(status,error)->Voidinifstatus==.success{print("Config fetched!")self.remoteConfig.activate(){(changed,error)in// ...}}else{print("Config not fetched")print("Error: \(error?.localizedDescription??"No error available.")")}self.loadAdUnit()}

Objective-C

[self.remoteConfigfetchWithCompletionHandler:^(FIRRemoteConfigFetchStatusstatus,NSError*error){if(status==FIRRemoteConfigFetchStatusSuccess){NSLog(@"Config fetched!");[self.remoteConfigactivateWithCompletion:^(BOOLchanged,NSError*_Nullableerror){// ...}];}else{NSLog(@"Config not fetched");NSLog(@"Error %@",error.localizedDescription);}[selfloadAdUnit];}];

Java

mFirebaseRemoteConfig.fetchAndActivate().addOnCompleteListener(this,newOnCompleteListener<Boolean>(){@OverridepublicvoidonComplete(@NonNullTask<Boolean>task){if(task.isSuccessful()){booleanupdated=task.getResult();Log.d(TAG,"Config params updated: "+updated);}else{Log.d(TAG,"Config params failed to update");}loadAdUnit();}});

Kotlin

remoteConfig.fetchAndActivate().addOnCompleteListener(this){task-> if(task.isSuccessful){valupdated=task.resultLog.d(TAG,"Config params updated: $updated")}else{Log.d(TAG,"Config params failed to update")}loadAdUnit()}

Unity

remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task=>{if(task.IsFaulted){Debug.LogWarning("Config params failed to update");}else{Debug.Log("Config params updated: "+task.Result);}LoadAdUnit();});

Your app is now ready to handle the Remote Config parameter that you created during the A/B test set up earlier in this tutorial.

Use the Remote Config parameter value

Use the pre-fetched Remote Config value in the loadAdUnit() function to determine which ad frequency variant should be shown for this app instance.

Swift

privatefuncloadAdUnit(){letadUnitId=remoteConfig["INTERSTITIAL_AD_KEY"].stringValue;letrequest=GADRequest()GADInterstitialAd.load(withAdUnitID:adUnitId,request:request,completionHandler:{[self]ad,errorinifleterror=error{print("Failed to load: \(error.localizedDescription)")return}interstitial=ad// Register for callbacks.})}// Register for callbacks.

Objective-C

-(void)loadAdUnit{NSString*adUnitId=self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;GADRequest*request=[GADRequestrequest];[GADInterstitialAdloadAdWithAdUnitId:adUnitIdrequest:requestcompletionHandler:^(GADInterstitialAd*ad,NSError*error){if(error){NSLog(@"Failed to load interstitial ad with error: %@",[errorlocalizedDescription]);return;}self.interstitial=ad;}];}

Java

privatevoidloadAdUnit(){StringadUnitId=mFirebaseRemoteConfig.getString("INTERSTITIAL_AD_KEY");// Load Interstitial Ad (assume adUnitId not null)AdRequestadRequest=newAdRequest.Builder().build();InterstitialAd.load(this,adUnitId,adRequest,newInterstitialAdLoadCallback(){@OverridepublicvoidonAdLoaded(@NonNullInterstitialAdintertitialAd){mInterstitialAd=interstitialAd;}@OverridepublicvoidonAdFailedToLoad(@NonNullLoadAdErrorloadAdError){mInterstitialAd=null;}});}

Kotlin

privatefunloadAdUnit(){StringadUnitId=remoteConfig.getString("INTERSTITIAL_AD_KEY")varadRequest=AdRequestBuilder.Builder().build()AdRequestBuilder.load(this,adUnitId,adRequest,object:InterstitialAdLoadCallback(){overridefunonAdFailedToLoad(adError:LoadAdError){mInterstitialAd=null}overridefunonAdLoaded(interstitialAd:InterstitialAd){mInterstitialAd=interstitialAd}})}

Unity

voidLoadAdUnit(){// Note that you may want to encode and parse two sets of ad unit IDs for// Android / iOS in the Unity implementation.StringadUnitId=remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue;this.interstitial=newInterstitialAd(adUnitId);}

Add other checks for the parameter value

There are other areas in your application code where you'll need to check the value of this Remote Config parameter to dictate which ad experience will be loaded. For example, you can decide whether to reload an ad after the user has finished viewing the current one.

The fetch and activate calls should be made first to get any parameter value changes — for example, if you decide to end or create a new experiment.

From there, you can always check the value for the parameter using the following calls:

Swift

remoteConfig["INTERSTITIAL_AD_KEY"].stringValue

Objective-C

self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;

Java

mFirebaseRemoteConfig.getString(INTERSTITIAL_AD_KEY)

Kotlin

remoteConfig.getString(INTERSTITIAL_AD_KEY)

Unity

remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue

These calls will always return the same value for an app instance depending on whether it was placed in the control group or one of the new ad variant groups, unless any changes were made in the Firebase console that were fetched and activated in the previous calls.




Step 2: Set up an A/B test in the Firebase consoleStep 4: Start the A/B test & review test results