Google 同意聲明管理解決方案 SDK

請參閱 SDK 的 Google 開發人員指南:


如需 SDK 整體需求和導入方式,請參閱本文件開頭的指南。如果您已啟用「隱私權與訊息」的同意聲明模式支援功能,可能也想導入本文介紹的額外功能。

導入相關 SDK、在「隱私權與訊息」中啟用 Google 同意聲明模式,並確定要導入基本同意聲明模式後,請按照下列操作說明進行。

如要導入基本模式,請按照下列步驟操作:

  1. 暫時停用 Analytics 收集功能 (AndroidiOS)。
  2. 準備好解除封鎖 Google Analytics 功能時,請重新啟用 Analytics 收集功能 (AndroidiOS)。

在步驟 2 中,請諮詢法律專家,判斷重新啟用 Analytics 功能的條件。

舉例來說,使用者做出同意聲明選擇後,您可以在 loadAndShowConsentFormIfRequired 的回呼中重新啟用 Google Analytics 功能 (API 參考資料:AndroidiOS)。

或者,如要根據使用者的同意聲明模式選項,判斷是否重新啟用 Analytics 收集功能,請讀取UMP_consentModeValues本機儲存空間金鑰。此值為四位數的字串。

  • 第一個數字代表 ad_storage 用途值
  • 第二個數字代表 ad_user_data 用途值
  • 第三位數表示廣告個人化用途值
  • 第四位數字表示 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

  • 同意聲明資訊已更新
  • 取得同意聲明。