第 3 步:设置 Firebase Remote Config 以显示特定的广告体验
简介:使用 AdMob、Google Analytics 和 Firebase 优化混合创收 |
第 1 步:使用 AdMob 创建新的广告单元进行展示 |
第 2 步:设置 Google Analytics(分析) |
第 3 步:设置 Firebase Remote Config 以显示特定的广告体验 |
在最后一步结束时,您了解了 Google Analytics 受众群体。在此步骤中,您将创建一个利用“购买者”受众的远程配置布尔控制参数(称为ad_control_switch
)。然后,您将根据该参数的值将逻辑添加到应用程序的代码中,以决定应用程序应显示的内容。
在 Firebase 控制台中设置远程配置参数和条件
在Firebase 控制台中,打开您的 Firebase 项目。
在左侧窗格中,展开参与部分,然后选择远程配置。
单击创建配置(如果您之前使用过远程配置,则单击添加参数)。
在创建参数面板中,完成以下步骤:
在参数名称字段中,输入
ad_control_switch
。从
Data type
下拉菜单中,选择布尔值。单击“新建” ,然后选择“创建新条件” 。
在“定义新条件”对话框中,完成以下步骤:
在“名称”字段中,输入
Purchasers Group
(或任何其他易于识别的条件名称)。从“适用于...”下拉菜单中,选择“用户受众” 。
从选择受众下拉菜单中,选择购买者。
单击保存条件。
返回创建参数面板,完成以下步骤:
对于购买者组的价值,选择false 。
对于默认值,选择true 。
单击“保存” ,然后单击“发布更改” 。
此配置将检查用户是否属于“购买者”受众(即,他们是付费用户):
如果用户属于“购买者”受众群体,则远程配置将为
ad_control_switch
参数返回false
值。如果用户不属于“购买者”受众群体,则远程配置将为
ad_control_switch
参数返回true
值。
在以下步骤中,您将在应用程序中实现远程配置来处理这些参数值。
将远程配置 SDK 添加到您的应用程序
在应用程序代码中使用远程配置之前,请将远程配置 SDK 添加到应用程序的代码库中。请注意,您的应用应该已经具有本教程前面步骤中的 Google 移动广告 (AdMob) SDK 和 Google Analytics for Firebase SDK。
迅速
在 podfile 中添加并安装 Remote Config pod:
pod 'Firebase/RemoteConfig'
安卓
将远程配置库依赖项添加到您的build.gradle
文件中:
implementation 'com.google.firebase:firebase-config:21.6.3'
扑
从 Flutter 项目的根目录中,运行以下命令来安装远程配置插件:
flutter pub add firebase_remote_config
统一
下载并安装最新的Firebase Unity SDK ,然后将远程配置包添加到您的项目中:
FirebaseRemoteConfig.unitypackage
配置远程配置实例
为了让您的应用程序可以使用远程配置参数值,请配置远程配置实例,以便它可以为客户端应用程序实例获取新值。
在此示例中,远程配置配置为每小时检查一次新参数值。
迅速
remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings
Kotlin+KTX
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
Java
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
扑
remoteConfig = FirebaseRemoteConfig.instance;
final configSettings = FirebaseRemoteConfigSettings(
minimumFetchInterval: Duration(hours: 1),
);
await remoteConfig.setConfigSettings(configSettings);
// Use the `onConfigUpdated` callback to listen for changes to the config settings.
remoteConfig.onConfigUpdated.listen((_) {
print('Config settings confirmed');
});
统一
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");
}
获取并激活远程配置
获取并激活远程配置参数,以便它可以开始使用新的参数值。
您希望在应用程序的加载阶段尽早进行此调用,因为此调用是异步的,并且您需要预获取远程配置值,以便您的应用程序知道是否显示广告。
迅速
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()
}
Kotlin+KTX
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()
}
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();
}
});
扑
remoteConfig = FirebaseRemoteConfig.instance;
// Fetch and activate the latest Remote Config values.
final updated = await remoteConfig.fetchAndActivate();
// Check if the config params were updated successfully.
if (updated) {
print('Config params updated');
} else {
print('Config params failed to update');
}
// Load the ad unit.
_loadAdUnit();
统一
remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task => {
if (task.IsFaulted) {
Debug.LogWarning("Config params failed to update");
} else {
Debug.Log("Config params updated: " + task.Result);
}
LoadAdUnit();
});
您的应用程序现已配置为处理您之前在此步骤中创建的远程配置参数。
使用远程配置参数值
使用loadAdUnit()
函数中预取的远程配置值来确定应用程序实例是否应执行以下操作之一:
ad_control_switch
参数值解析为true
:显示插页式广告(因为用户是非付费用户)。ad_control_switch
参数值解析为false
:不显示广告(因为用户是付费用户)。
迅速
private func loadAdUnit() {
let showAds = remoteConfig["ad_control_switch"].boolValue
if showAds {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Kotlin+KTX
private fun loadAdUnit() {
var showAds = remoteConfig.getBoolean(ad_control_switch)
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Java
private void loadAdUnit() {
boolean showAds =
mFirebaseRemoteConfig.getBoolean(ad_control_switch);
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
扑
void _loadAdUnit() {
bool showAds = remoteConfig.getBool(ad_control_switch);
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
统一
void LoadAdUnit() {
bool showAds =
remoteConfig.GetValue("ad_control_switch").BooleanValue;
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
发布您的应用程序
由于显示或不显示广告的逻辑位于您的代码库中,因此您需要发布包含此逻辑的应用程序的新版本。
如果您按照本教程的步骤操作,您的应用应立即开始为用户提供自定义的应用内广告体验。您可以在 AdMob 帐户和 Google Analytics(分析)仪表板(Firebase 控制台或 Google Analytics UI)中监控广告收入。
就是这样!您已完成使用 AdMob、Google Analytics 和 Firebase 优化混合创收的教程。
相关资源
查看另一个解决方案指南:
观看视频系列:使用 Firebase 和 AdMob 优化您的应用收入