Skip to content

Commit 4d6c808

Browse files
feat: Add Consent Support (#30)
1 parent c166f5e commit 4d6c808

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

mParticle-Google-Analytics-Firebase/MPKitFirebaseAnalytics.m

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#if __has_include(<FirebaseCore/FirebaseCore.h>)
66
#import <FirebaseCore/FirebaseCore.h>
77
#import <FirebaseAnalytics/FIRAnalytics.h>
8+
#import <FirebaseAnalytics/FIRAnalytics+Consent.h>
89
#else
910
#import "FirebaseCore/FirebaseCore.h"
1011
#import "FirebaseAnalytics/FIRAnalytics.h"
12+
#import "FirebaseAnalytics/FIRAnalytics+Consent.h"
1113
#endif
1214
#endif
1315

@@ -40,6 +42,15 @@ @implementation MPKitFirebaseAnalytics
4042
static NSString *const aToZCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4143
static NSString *const instanceIdIntegrationKey = @"app_instance_id";
4244

45+
static NSString *const kMPFIRGA4AdStorageKey = @"ad_storage";
46+
static NSString *const kMPFIRGA4AdUserDataKey = @"ad_user_data";
47+
static NSString *const kMPFIRGA4AdPersonalizationKey = @"ad_personalization";
48+
static NSString *const kMPFIRGA4AnalyticsStorageKey = @"analytics_storage";
49+
static NSString *const kMPFIRGA4DefaultAdStorageKey = @"defaultAdStorageConsentSDK";
50+
static NSString *const kMPFIRGA4DefaultAdUserDataKey = @"defaultAdUserDataConsentSDK";
51+
static NSString *const kMPFIRGA4DefaultAdPersonalizationKey = @"defaultAdPersonalizationConsentSDK";
52+
static NSString *const kMPFIRGA4DefaultAnalyticsStorageKey = @"defaultAnalyticsStorageConsentSDK";
53+
4354
const NSInteger FIR_MAX_CHARACTERS_EVENT_NAME_INDEX = 39;
4455
const NSInteger FIR_MAX_CHARACTERS_IDENTITY_NAME_INDEX = 23;
4556
const NSInteger FIR_MAX_CHARACTERS_EVENT_ATTR_VALUE_INDEX = 99;
@@ -74,6 +85,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu
7485
[self updateInstanceIDIntegration];
7586
}
7687

88+
[self updateConsent];
89+
7790
_started = YES;
7891

7992
dispatch_async(dispatch_get_main_queue(), ^{
@@ -332,6 +345,88 @@ - (void)logUserAttributes:(NSDictionary<NSString *, id> *)userAttributes {
332345
}
333346
}
334347

348+
- (MPKitExecStatus *)setConsentState:(nullable MPConsentState *)state {
349+
[self updateConsent];
350+
351+
return [self execStatus:MPKitReturnCodeSuccess];
352+
}
353+
354+
- (void)updateConsent {
355+
FIRConsentStatus adStorageStatus;
356+
FIRConsentStatus adUserDataStatus;
357+
FIRConsentStatus analyticsStorageStatus;
358+
FIRConsentStatus adPersonalizationStatus;
359+
360+
// Default Consent States
361+
if (self.configuration[kMPFIRGA4DefaultAdStorageKey]) {
362+
if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Granted"]) {
363+
adStorageStatus = FIRConsentStatusGranted;
364+
} else if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Denied"]) {
365+
adStorageStatus = FIRConsentStatusDenied;
366+
}
367+
}
368+
if (self.configuration[kMPFIRGA4DefaultAdUserDataKey]) {
369+
if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Granted"]) {
370+
adUserDataStatus = FIRConsentStatusGranted;
371+
} else if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Denied"]) {
372+
adUserDataStatus = FIRConsentStatusDenied;
373+
}
374+
}
375+
if (self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey]) {
376+
if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Granted"]) {
377+
analyticsStorageStatus = FIRConsentStatusGranted;
378+
} else if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Denied"]) {
379+
analyticsStorageStatus = FIRConsentStatusDenied;
380+
}
381+
}
382+
if (self.configuration[kMPFIRGA4DefaultAdPersonalizationKey]) {
383+
if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Granted"]) {
384+
adPersonalizationStatus = FIRConsentStatusGranted;
385+
} else if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Denied"]) {
386+
adPersonalizationStatus = FIRConsentStatusDenied;
387+
}
388+
}
389+
390+
MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser];
391+
NSDictionary<NSString *, MPGDPRConsent *> *userConsentMap = currentUser.consentState.gdprConsentState;
392+
393+
// Update from mParticle consent
394+
if (self.configuration[kMPFIRGA4AdStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]]) {
395+
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]];
396+
adStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
397+
}
398+
if (self.configuration[kMPFIRGA4AdUserDataKey] && userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]]) {
399+
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]];
400+
adUserDataStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
401+
}
402+
if (self.configuration[kMPFIRGA4AnalyticsStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]]) {
403+
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]];
404+
analyticsStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
405+
}
406+
if (self.configuration[kMPFIRGA4AdPersonalizationKey] && userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]]) {
407+
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]];
408+
adPersonalizationStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
409+
}
410+
411+
// Construct a dictionary of consents
412+
NSMutableDictionary *uploadDict = [[NSMutableDictionary alloc] init];
413+
if (adStorageStatus) {
414+
uploadDict[FIRConsentTypeAdStorage] = adStorageStatus;
415+
}
416+
if (adUserDataStatus) {
417+
uploadDict[FIRConsentTypeAdUserData] = adUserDataStatus;
418+
}
419+
if (analyticsStorageStatus) {
420+
uploadDict[FIRConsentTypeAnalyticsStorage] = analyticsStorageStatus;
421+
}
422+
if (adPersonalizationStatus) {
423+
uploadDict[FIRConsentTypeAdPersonalization] = adPersonalizationStatus;
424+
}
425+
426+
// Update consent state with FIRAnalytics
427+
[FIRAnalytics setConsent:uploadDict];
428+
}
429+
335430
- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters {
336431
switch (commerceEvent.action) {
337432
case MPCommerceEventActionAddToCart:

0 commit comments

Comments
 (0)