请参阅 SDK 的 Google 开发者指南:
Google 意见征求模式 SDK 功能
如需了解整体 SDK 要求和实现,请参阅本文档开头的指南。如果您已启用“隐私权和消息”的意见征求模式支持,可能还需要实现本文中介绍的其他功能。
意见征求模式:基本模式支持
实现相关 SDK、在“隐私和消息”中启用 Google 意见征求模式,并确定要实现基本意见征求模式后,请按照以下说明操作。
如需实现基本模式,请执行以下操作:
- 暂时停用 Google Analytics 数据收集功能(Android、iOS)。
- 当您准备好解除 Google Analytics 功能的屏蔽后,请重新启用 Google Analytics 数据收集功能(Android、iOS)。
对于第 2 步,请咨询法律专家,确定重新启用 Google Analytics 功能的条件。
例如,您可以在用户做出意见征求选择后,通过在 loadAndShowConsentFormIfRequired
的回调中重新启用 Google Analytics 功能来重新启用 Google Analytics 功能(API 参考:Android、iOS)。
或者,如需根据用户的意见征求模式选择确定是否重新启用 Google Analytics 数据收集,请读取 UMP_consentModeValues
本地存储键。该值为四位数字符串。
- 第一个数字表示 ad_storage 用途值
- 第二位数字表示 ad_user_data 用途值
- 第三位数表示 ad_personalization 用途值
- 第四位数字表示 analytics_storage 用途值
下表介绍了每个数字的可能值:
值 | 含义 |
---|---|
0 |
尚无可用值。 |
1 |
用户GRANTED 此用途。 |
2 |
用户DENIED 此用途。 |
3 |
意见征求模式不适用于此用户。 |
4 |
意见征求模式未配置为此用途。 |
以下示例会解析 UMP_consentModeValues
,如果所有用途均为 GRANTED
或不适用,则启用 Google Analytics,否则停用 Google 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
:
- 用户意见信息已更新
- 征得用户同意。