|
5 | 5 | #if __has_include(<FirebaseCore/FirebaseCore.h>)
|
6 | 6 | #import <FirebaseCore/FirebaseCore.h>
|
7 | 7 | #import <FirebaseAnalytics/FIRAnalytics.h>
|
| 8 | + #import <FirebaseAnalytics/FIRAnalytics+Consent.h> |
8 | 9 | #else
|
9 | 10 | #import "FirebaseCore/FirebaseCore.h"
|
10 | 11 | #import "FirebaseAnalytics/FIRAnalytics.h"
|
| 12 | + #import "FirebaseAnalytics/FIRAnalytics+Consent.h" |
11 | 13 | #endif
|
12 | 14 | #endif
|
13 | 15 |
|
@@ -42,6 +44,15 @@ @implementation MPKitFirebaseGA4Analytics
|
42 | 44 | static NSString *const instanceIdIntegrationKey = @"app_instance_id";
|
43 | 45 | static NSString *const invalidFirebaseKey = @"invalid_ga4_key";
|
44 | 46 |
|
| 47 | +static NSString *const kMPFIRGA4AdStorageKey = @"ad_storage"; |
| 48 | +static NSString *const kMPFIRGA4AdUserDataKey = @"ad_user_data"; |
| 49 | +static NSString *const kMPFIRGA4AdPersonalizationKey = @"ad_personalization"; |
| 50 | +static NSString *const kMPFIRGA4AnalyticsStorageKey = @"analytics_storage"; |
| 51 | +static NSString *const kMPFIRGA4DefaultAdStorageKey = @"defaultAdStorageConsentSDK"; |
| 52 | +static NSString *const kMPFIRGA4DefaultAdUserDataKey = @"defaultAdUserDataConsentSDK"; |
| 53 | +static NSString *const kMPFIRGA4DefaultAdPersonalizationKey = @"defaultAdPersonalizationConsentSDK"; |
| 54 | +static NSString *const kMPFIRGA4DefaultAnalyticsStorageKey = @"defaultAnalyticsStorageConsentSDK"; |
| 55 | + |
45 | 56 | // Following limits are based off Google Analytics 360 limits, docs here "https://support.google.com/analytics/answer/11202874?sjid=14644072134282618832-NA#limits"
|
46 | 57 | const NSInteger FIR_MAX_CHARACTERS_EVENT_NAME = 40;
|
47 | 58 | const NSInteger FIR_MAX_CHARACTERS_IDENTITY_NAME = 24;
|
@@ -87,6 +98,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu
|
87 | 98 |
|
88 | 99 | [self updateInstanceIDIntegration];
|
89 | 100 |
|
| 101 | + [self updateConsent]; |
| 102 | + |
90 | 103 | _started = YES;
|
91 | 104 |
|
92 | 105 | dispatch_async(dispatch_get_main_queue(), ^{
|
@@ -358,6 +371,88 @@ - (void)logUserAttributes:(NSDictionary<NSString *, id> *)userAttributes {
|
358 | 371 | }
|
359 | 372 | }
|
360 | 373 |
|
| 374 | +- (MPKitExecStatus *)setConsentState:(nullable MPConsentState *)state { |
| 375 | + [self updateConsent]; |
| 376 | + |
| 377 | + return [self execStatus:MPKitReturnCodeSuccess]; |
| 378 | +} |
| 379 | + |
| 380 | +- (void)updateConsent { |
| 381 | + FIRConsentStatus adStorageStatus; |
| 382 | + FIRConsentStatus adUserDataStatus; |
| 383 | + FIRConsentStatus analyticsStorageStatus; |
| 384 | + FIRConsentStatus adPersonalizationStatus; |
| 385 | + |
| 386 | + // Default Consent States |
| 387 | + if (self.configuration[kMPFIRGA4DefaultAdStorageKey]) { |
| 388 | + if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Granted"]) { |
| 389 | + adStorageStatus = FIRConsentStatusGranted; |
| 390 | + } else if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Denied"]) { |
| 391 | + adStorageStatus = FIRConsentStatusDenied; |
| 392 | + } |
| 393 | + } |
| 394 | + if (self.configuration[kMPFIRGA4DefaultAdUserDataKey]) { |
| 395 | + if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Granted"]) { |
| 396 | + adUserDataStatus = FIRConsentStatusGranted; |
| 397 | + } else if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Denied"]) { |
| 398 | + adUserDataStatus = FIRConsentStatusDenied; |
| 399 | + } |
| 400 | + } |
| 401 | + if (self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey]) { |
| 402 | + if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Granted"]) { |
| 403 | + analyticsStorageStatus = FIRConsentStatusGranted; |
| 404 | + } else if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Denied"]) { |
| 405 | + analyticsStorageStatus = FIRConsentStatusDenied; |
| 406 | + } |
| 407 | + } |
| 408 | + if (self.configuration[kMPFIRGA4DefaultAdPersonalizationKey]) { |
| 409 | + if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Granted"]) { |
| 410 | + adPersonalizationStatus = FIRConsentStatusGranted; |
| 411 | + } else if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Denied"]) { |
| 412 | + adPersonalizationStatus = FIRConsentStatusDenied; |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser]; |
| 417 | + NSDictionary<NSString *, MPGDPRConsent *> *userConsentMap = currentUser.consentState.gdprConsentState; |
| 418 | + |
| 419 | + // Update from mParticle consent |
| 420 | + if (self.configuration[kMPFIRGA4AdStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]]) { |
| 421 | + MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]]; |
| 422 | + adStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied; |
| 423 | + } |
| 424 | + if (self.configuration[kMPFIRGA4AdUserDataKey] && userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]]) { |
| 425 | + MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]]; |
| 426 | + adUserDataStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied; |
| 427 | + } |
| 428 | + if (self.configuration[kMPFIRGA4AnalyticsStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]]) { |
| 429 | + MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]]; |
| 430 | + analyticsStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied; |
| 431 | + } |
| 432 | + if (self.configuration[kMPFIRGA4AdPersonalizationKey] && userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]]) { |
| 433 | + MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]]; |
| 434 | + adPersonalizationStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied; |
| 435 | + } |
| 436 | + |
| 437 | + // Construct a dictionary of consents |
| 438 | + NSMutableDictionary *uploadDict = [[NSMutableDictionary alloc] init]; |
| 439 | + if (adStorageStatus) { |
| 440 | + uploadDict[FIRConsentTypeAdStorage] = adStorageStatus; |
| 441 | + } |
| 442 | + if (adUserDataStatus) { |
| 443 | + uploadDict[FIRConsentTypeAdUserData] = adUserDataStatus; |
| 444 | + } |
| 445 | + if (analyticsStorageStatus) { |
| 446 | + uploadDict[FIRConsentTypeAnalyticsStorage] = analyticsStorageStatus; |
| 447 | + } |
| 448 | + if (adPersonalizationStatus) { |
| 449 | + uploadDict[FIRConsentTypeAdPersonalization] = adPersonalizationStatus; |
| 450 | + } |
| 451 | + |
| 452 | + // Update consent state with FIRAnalytics |
| 453 | + [FIRAnalytics setConsent:uploadDict]; |
| 454 | +} |
| 455 | + |
361 | 456 | - (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters {
|
362 | 457 | switch (commerceEvent.action) {
|
363 | 458 | case MPCommerceEventActionAddToCart:
|
|
0 commit comments