请参阅 Google 开发者指南,了解 SDK:
Google 意见征求模式 SDK 功能
请参阅本文档开头的指南,了解 SDK 的总体要求和实现。如果您已启用隐私权和消息传递的意见征求模式支持,可能还需要实现此处介绍的其他功能。
意见征求模式:基本模式支持
实现相关 SDK、 在隐私权和消息传递中启用 Google 意见征求模式、 并确定要实现 基本意见征求模式 后,请按照以下说明操作。
如需实现基本模式,请执行以下操作:
对于第 2 步,请咨询法律专家,以确定重新启用 Analytics 功能的标准。
例如,您可以在用户做出意见征求选择后重新启用 Google Analytics 功能,方法是在 loadAndShowConsentFormIfRequired 的回调中重新启用 Google Analytics 功能(API 参考文档:Android、iOS)。
或者,如需根据用户的意见征求模式选择来确定是否重新启用 Analytics 数据收集,请读取 UMP_consentModeValues 本地存储键。该值是一个四位数字的字符串。
- 第一位数字表示 ad_storage 目的值
- 第二位数字表示 ad_user_data 目的值
- 第三位数字表示 ad_personalization 目的值
- 第四位数字表示 analytics_storage 目的值
下表介绍了每个数字的可能值:
| 值 | 含义 |
|---|---|
0 |
尚无可用值。 |
1 |
用户 GRANTED 了此目的。 |
2 |
用户 DENIED 了此目的。 |
3 |
意见征求模式不适用于此用户。 |
4 |
未为此目的配置意见征求模式。 |
以下示例解析了 UMP_consentModeValues,如果所有目的均为 GRANTED 或不适用,则启用 Google Analytics;否则,停用 Analytics 数据收集:
Java
// Helper function that sets Analytics collection based on the value of the
// UMP_consentModeValues local storage key.
private void maybeReEnableAnalyticsCollection() {
String consentModeValues = sharedPreferences.getString("UMP_consentModeValues", null);
if (shouldReEnableAnalyticsCollection(consentModeValues)) {
firebaseAnalytics.setAnalyticsCollectionEnabled(true);
} else {
firebaseAnalytics.setAnalyticsCollectionEnabled(false);
}
}
// Helper function to determine whether Analytics collection should be enabled. Returns true
// if all consent mode values are either granted, not applicable, or not configured.
private boolean shouldReEnableAnalyticsCollection(String consentModeValues) {
if (consentModeValues.isEmpty()) {
return false;
}
for (int i = 0; i < consentModeValues.length(); i++) {
char consentModeValue = consentModeValues.charAt(i);
switch (consentModeValue) {
case '0':
// Consent mode data not ready yet.
return false;
case 1:
// Consent mode is granted for this purpose.
break;
case '2':
// Consent is denied for this consent mode purpose.
return false;
case '3':
// Consent does not apply for this purpose at this time.
break;
case '4':
// Consent mode is not configured for this purpose.
break;
default:
// Unexpected value encountered.
return false;
}
}
// If all checks pass, re-enable Analytics collection.
return true;
}
Kotlin
// Helper function that may re-enable Analytics collection based on the value of the
// UMP_consentModeValues local storage key.
private fun maybeReEnableAnalyticsCollection() {
val consentModeValues = sharedPreferences.getString("UMP_consentModeValues", null)
if (shouldReEnableAnalyticsCollection(consentModeValues)) {
firebaseAnalytics.setAnalyticsCollectionEnabled(true)
}
}
// Helper function to determine whether Analytics collection should be re-enabled. Returns true
// if all consent mode values are either granted, not applicable, or not configured.
private fun shouldReEnableAnalyticsCollection(consentModeValues: String?): Boolean {
if (consentModeValues.isNullOrEmpty()) {
return false
}
for (consentModeValue in consentModeValues) {
when (consentModeValue) {
'0' -> {
// Consent mode data not ready yet.
return false
}
'1' -> {
// Consent mode is granted for this purpose.
}
'2' -> {
// Consent is denied for this consent mode purpose.
return false
}
'3' -> {
// Consent does not apply for this purpose at this time.
}
'4' -> {
// Consent mode is not configured for this purpose.
}
else -> {
// Unexpected value encountered.
return false
}
}
}
// If all checks pass, can re-enable Analytics collection.
return true
}
Swift
// Helper function that may re-enable Analytics collection based on the value of the
// UMP_consentModeValues local storage key.
private func maybeReEnableAnalyticsCollection() {
let consentModeValues = userDefaults.string(forKey: "UMP_consentModeValues")
if shouldReEnableAnalyticsCollection(consentModeValues) {
Analytics.setAnalyticsCollectionEnabled(true)
}
}
// Helper function to determine whether Analytics collection should be re-enabled. Returns true
// if all consent mode values are either granted, not applicable, or not configured.
private func shouldReEnableAnalyticsCollection(_ consentModeValues: String?) -> Bool {
guard let consentModeValues = consentModeValues, !consentModeValues.isEmpty else {
return false
}
for consentModeValue in consentModeValues {
switch consentModeValue {
case "0":
// Consent mode data not ready yet.
return false
case "1":
// Consent mode is granted for this purpose.
break
case "2":
// Consent is denied for this consent mode purpose.
return false
case "3":
// Consent does not apply for this purpose at this time.
break
case "4":
// Consent mode is not configured for this purpose.
break
default:
// Unexpected value encountered.
return false
}
}
// If all checks pass, can re-enable Analytics collection.
return true
}
Objective-C
// Helper function that may re-enable Analytics collection based on the value of the
// UMP_consentModeValues local storage key.
- (void)maybeReEnableAnalyticsCollection {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *consentModeValues = [defaults stringForKey:@"UMP_consentModeValues"];
if ([self shouldReEnableAnalyticsCollection:consentModeValues]) {
[FIRAnalytics setAnalyticsCollectionEnabled:YES];
}
}
// Helper function to determine whether Analytics collection should be re-enabled. Returns true
// if all consent mode values are either granted, not applicable, or not configured.
- (BOOL)shouldReEnableAnalyticsCollection:(NSString *)consentModeValues {
if (!consentModeValues || [consentModeValues length] == 0) {
return NO;
}
for (NSUInteger i = 0; i < [consentModeValues length]; i++) {
unichar consentModeValue = [consentModeValues characterAtIndex:i];
switch (consentModeValue) {
case '0':
// Consent mode data not ready yet.
return NO;
case '1':
// Consent mode is granted for this purpose.
break;
case '2':
// Consent is denied for this consent mode purpose.
return NO;
case '3':
// Consent does not apply for this purpose at this time.
break;
case '4':
// Consent mode is not configured for this purpose.
break;
default:
// Unexpected value encountered.
return NO;
}
}
// If all checks pass, can re-enable Analytics collection.
return YES;
}
在以下情况下调用 maybeReEnableAnalyticsCollection:
- 意见征求信息已更新
- 已征求用户意见。