Passaggio 3: gestisci i valori dei parametri Remote Config nel codice dell'app
| Introduzione: Test nuova adozione del formato di annunci AdMob utilizzando Firebase |
| Passaggio 1: Utilizza AdMob per creare una nuova variante dell'unità pubblicitaria per i test |
| Passaggio 2: Configura un test A/B nella console Firebase |
|
Passaggio 3: Gestisci i valori dei parametri Remote Config nel codice dell'app |
| Passaggio 4: Avvia il test A/B ed esamina i risultati nella console Firebase |
| Passaggio 5: Decidi se implementare il nuovo formato dell'annuncio |
Al termine dell'ultimo passaggio, hai creato un parametro Remote Config
(SHOW_NEW_AD_KEY). In questo passaggio, aggiungerai la logica al codice dell'app per
determinare cosa deve visualizzare l'app in base al valore di questo parametro: true
(mostra il nuovo annuncio) o false ( _non_ mostrare il nuovo annuncio).
Aggiungi gli SDK necessari
Prima di utilizzare Remote Config nel codice dell'applicazione, aggiungi sia l' Remote Config SDK sia l'SDK Firebase per Google Analytics ai file di build del progetto.
Piattaforme Apple
Aggiungi e installa i seguenti pod nel tuo podfile:
pod 'Google-Mobile-Ads-SDK'
pod 'Firebase/Analytics'
pod 'Firebase/RemoteConfig'
Android
Aggiungi le seguenti dipendenze della libreria al file build.gradle:
implementation 'com.google.android.gms:play-services-ads:25.2.0'
implementation 'com.google.firebase:firebase-analytics:23.2.0'
implementation 'com.google.firebase:firebase-config:23.1.0'
Unity
Scarica e installa l'SDK Firebase Unity, quindi aggiungi i seguenti pacchetti Unity al tuo progetto:
FirebaseAnalytics.unitypackageFirebaseRemoteConfig.unitypackage
Configura l'istanza Remote Config
Per utilizzare i valori parametro di Remote Config, configura l' Remote Config istanza in modo che sia impostata per recuperare nuovi valori per l' istanza dell'app client.
In questo esempio, Remote Config è configurato per verificare la presenza di nuovi valori parametro una volta all'ora.
Swift
remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings
Objective-C
self.remoteConfig = [FIRRemoteConfig remoteConfig];
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
remoteConfigSettings.minimumFetchInterval = 3600;
self.remoteConfig.configSettings = remoteConfigSettings;
Java
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
Kotlin
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
Unity
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
var configSettings = new ConfigSettings {
MinimumFetchInternalInMilliseconds =
(ulong)(new TimeSpan(1, 0, 0).TotalMilliseconds)
};
remoteConfig.SetConfigSettingsAsync(configSettings)
.ContinueWithOnMainThread(task => {
Debug.Log("Config settings confirmed");
}
Recupera e attiva Remote Config
Recupera e attiva i parametri Remote Config in modo che possano iniziare a utilizzare i nuovi valori parametro.
Ti consigliamo di effettuare questa chiamata il prima possibile durante la fase di caricamento dell'app poiché si tratta di una chiamata asincrona e l'app avrà bisogno del Remote Config valore precaricato per sapere se mostrare l'annuncio.
Swift
remoteConfig.fetch() { (status, error) -> Void in
if status == .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.remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
NSLog(@"Config fetched!");
[self.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) {
// ...
}];
} else {
NSLog(@"Config not fetched");
NSLog(@"Error %@", error.localizedDescription);
}
[self loadAdUnit];
}];
Java
mFirebaseRemoteConfig.fetchAndActivate()
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
@Override
public void onComplete(@NonNull Task<Boolean> task) {
if (task.isSuccessful()) {
boolean updated = 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) {
val updated = task.result
Log.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();
});
L'app è ora pronta per gestire il parametro Remote Config che hai creato durante la configurazione del test A/B in precedenza in questo tutorial.
Utilizza il valore parametro Remote Config
Utilizza il valore Remote Config precaricato nella funzione loadAdUnit() per
determinare se l'istanza dell'app deve mostrare (valore parametro true) o
non mostrare (valore parametro false) la nuova unità pubblicitaria interstitial con premio.
Swift
private func loadAdUnit() {
let showNewAdFormat = remoteConfig["users"].boolValue
if showNewAdFormat {
// Load Rewarded Interstitial Ad.
// This should load your new implemented ad unit
// as per AdMob instructions (the first step of this tutorial).
} else {
// Show the existing ad unit.
}
}
Objective-C
- (void)loadAdUnit {
BOOL showAds = self.remoteConfig[@"SHOW_NEW_AD_KEY"].boolValue;
if (showAds) {
// Load Rewarded Interstitial Ad.
// This should load your new implemented ad unit
// per AdMob instructions (the first step of this tutorial).
} else {
// Show the existing ad unit.
}
}
Java
private void loadAdUnit() {
boolean showNewAdFormat =
mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY);
if (showNewAdFormat) {
// Load Rewarded Interstitial Ad.
// This should load your new implemented ad unit
// per AdMob instructions (the first step of this tutorial).
} else {
// Show the existing ad unit.
}
}
Kotlin
private fun loadAdUnit() {
var showNewAdFormat = remoteConfig.getBoolean(SHOW_NEW_AD_KEY)
if (showNewAdFormat) {
// Load Rewarded Interstitial Ad.
// This should load your new implemented ad unit
// per AdMob instructions (the first step of this tutorial).
} else {
// Show the existing ad unit.
}
}
Unity
void LoadAdUnit() {
bool showNewAdFormat =
remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue;
if (showNewAdFormat) {
// Load Rewarded Interstitial Ad (new implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Show the existing ad unit.
}
}
Aggiungi altri controlli per il valore parametro
Ci sono altre aree nel codice dell'applicazione in cui dovrai controllare il valore di questo Remote Config parametro per determinare quale esperienza pubblicitaria verrà caricata. Ad esempio, puoi decidere se ricaricare un annuncio dopo che l'utente ha finito di visualizzare quello corrente.
Le chiamate di recupero e attivazione devono essere effettuate per prime per ottenere eventuali modifiche dei valori dei parametri, ad esempio se decidi di terminare o creare un nuovo esperimento.
A questo punto, puoi sempre controllare il valore del parametro utilizzando le seguenti chiamate:
Swift
remoteConfig["showNewAdKey"].boolValue
Objective-C
self.remoteConfig[@"SHOW_NEW_AD_KEY"].boolValue;
Java
mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY)
Kotlin
remoteConfig.getBoolean(SHOW_NEW_AD_KEY)
Unity
remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue
Queste chiamate restituiranno sempre lo stesso valore per un'istanza dell'app, a seconda che sia stata inserita nel gruppo di controllo o nel nuovo gruppo di varianti dell'annuncio, a meno che non siano state apportate modifiche nella console Firebase che sono state recuperate e attivate nelle chiamate precedenti.
Passaggio 2: configura un test A/B nella console Firebase Passaggio 4: avvia il test A/B ed esamina i risultati