SDK giải pháp quản lý sự đồng ý của Google

Xem Hướng dẫn dành cho nhà phát triển của Google về các SDK:


Hãy tham khảo hướng dẫn ở đầu tài liệu này để biết các yêu cầu và cách triển khai SDK nói chung. Nếu đã bật tính năng hỗ trợ Chế độ đồng ý của Quyền riêng tư và thông báo, bạn cũng có thể muốn triển khai chức năng bổ sung được đề cập ở đây.

Sau khi bạn triển khai SDK có liên quan, bật Chế độ đồng ý của Google trong mục Quyền riêng tư và thông báo, đồng thời xác định rằng bạn sẽ triển khai chế độ đồng ý cơ bản, hãy làm theo các hướng dẫn này.

Cách triển khai chế độ cơ bản:

  1. Tạm thời tắt tính năng thu thập dữ liệu Analytics (Android, iOS).
  2. Khi bạn đã sẵn sàng bỏ chặn chức năng Google Analytics, hãy bật lại tính năng thu thập dữ liệu của Analytics (Android, iOS).

Đối với Bước 2, hãy tham khảo ý kiến của chuyên gia pháp lý để xác định tiêu chí của bạn về việc bật lại chức năng Phân tích.

Ví dụ: bạn có thể bật lại chức năng Google Analytics sau khi người dùng đã đưa ra lựa chọn đồng ý bằng cách bật lại chức năng Google Analytics trong lệnh gọi lại của loadAndShowConsentFormIfRequired (tài liệu tham khảo API: Android, iOS).

Ngoài ra, để xác định xem có nên bật lại tính năng thu thập dữ liệu Analytics dựa trên lựa chọn của người dùng trong chế độ đồng ý hay không, hãy đọc khoá lưu trữ cục bộ UMP_consentModeValues. Giá trị là một chuỗi gồm 4 chữ số.

  • Chữ số đầu tiên cho biết giá trị mục đích ad_storage
  • Chữ số thứ hai cho biết giá trị mục đích ad_user_data
  • Chữ số thứ ba cho biết giá trị mục đích ad_personalization
  • Chữ số thứ tư cho biết giá trị mục đích analytics_storage

Bảng sau đây mô tả các giá trị có thể có cho từng chữ số:

Giá trị Ý nghĩa
0 Hiện chưa có giá trị nào.
1 Người dùng GRANTED mục đích này.
2 Người dùng DENIED mục đích này.
3 Chế độ đồng ý không áp dụng cho người dùng này.
4 Bạn chưa định cấu hình chế độ đồng ý cho mục đích này.

Ví dụ sau đây phân tích cú pháp UMP_consentModeValues, bật Google Analytics nếu tất cả các mục đích đều là GRANTED hoặc không áp dụng, và tắt tính năng thu thập dữ liệu Analytics nếu không:

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;
  }

Gọi maybeReEnableAnalyticsCollection khi:

  • thông tin về sự đồng ý được cập nhật
  • sự đồng ý được thu thập.