Skip to content

Commit

Permalink
feat: Add Consent Support
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStalnaker committed May 30, 2024
1 parent c166f5e commit ec62965
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions mParticle-Google-Analytics-Firebase/MPKitFirebaseAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#if __has_include(<FirebaseCore/FirebaseCore.h>)
#import <FirebaseCore/FirebaseCore.h>
#import <FirebaseAnalytics/FIRAnalytics.h>
#import <FirebaseAnalytics/FIRAnalytics+Consent.h>
#else
#import "FirebaseCore/FirebaseCore.h"
#import "FirebaseAnalytics/FIRAnalytics.h"
#import "FirebaseAnalytics/FIRAnalytics+Consent.h"
#endif
#endif

Expand Down Expand Up @@ -40,6 +42,15 @@ @implementation MPKitFirebaseAnalytics
static NSString *const aToZCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static NSString *const instanceIdIntegrationKey = @"app_instance_id";

static NSString *const kMPFIRGA4AdStorageKey = @"ad_storage";
static NSString *const kMPFIRGA4AdUserDataKey = @"ad_user_data";
static NSString *const kMPFIRGA4AdPersonalizationKey = @"ad_personalization";
static NSString *const kMPFIRGA4AnalyticsStorageKey = @"analytics_storage";
static NSString *const kMPFIRGA4DefaultAdStorageKey = @"defaultAdStorageConsentSDK";
static NSString *const kMPFIRGA4DefaultAdUserDataKey = @"defaultAdUserDataConsentSDK";
static NSString *const kMPFIRGA4DefaultAdPersonalizationKey = @"defaultAdPersonalizationConsentSDK";
static NSString *const kMPFIRGA4DefaultAnalyticsStorageKey = @"defaultAnalyticsStorageConsentSDK";

const NSInteger FIR_MAX_CHARACTERS_EVENT_NAME_INDEX = 39;
const NSInteger FIR_MAX_CHARACTERS_IDENTITY_NAME_INDEX = 23;
const NSInteger FIR_MAX_CHARACTERS_EVENT_ATTR_VALUE_INDEX = 99;
Expand Down Expand Up @@ -74,6 +85,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu
[self updateInstanceIDIntegration];
}

[self updateConsent];

_started = YES;

dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -332,6 +345,88 @@ - (void)logUserAttributes:(NSDictionary<NSString *, id> *)userAttributes {
}
}

- (MPKitExecStatus *)setConsentState:(nullable MPConsentState *)state {
[self updateConsent];

return [self execStatus:MPKitReturnCodeSuccess];
}

- (void)updateConsent {
FIRConsentStatus adStorageStatus;
FIRConsentStatus adUserDataStatus;
FIRConsentStatus analyticsStorageStatus;
FIRConsentStatus adPersonalizationStatus;

// Default Consent States
if (self.configuration[kMPFIRGA4DefaultAdStorageKey]) {
if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Granted"]) {
adStorageStatus = FIRConsentStatusGranted;
} else if ([self.configuration[kMPFIRGA4DefaultAdStorageKey] isEqual: @"Denied"]) {
adStorageStatus = FIRConsentStatusDenied;
}
}
if (self.configuration[kMPFIRGA4DefaultAdUserDataKey]) {
if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Granted"]) {
adUserDataStatus = FIRConsentStatusGranted;
} else if ([self.configuration[kMPFIRGA4DefaultAdUserDataKey] isEqual: @"Denied"]) {
adUserDataStatus = FIRConsentStatusDenied;
}
}
if (self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey]) {
if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Granted"]) {
analyticsStorageStatus = FIRConsentStatusGranted;
} else if ([self.configuration[kMPFIRGA4DefaultAnalyticsStorageKey] isEqual: @"Denied"]) {
analyticsStorageStatus = FIRConsentStatusDenied;
}
}
if (self.configuration[kMPFIRGA4DefaultAdPersonalizationKey]) {
if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Granted"]) {
adPersonalizationStatus = FIRConsentStatusGranted;
} else if ([self.configuration[kMPFIRGA4DefaultAdPersonalizationKey] isEqual: @"Denied"]) {
adPersonalizationStatus = FIRConsentStatusDenied;
}
}

MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser];
NSDictionary<NSString *, MPGDPRConsent *> *userConsentMap = currentUser.consentState.gdprConsentState;

// Update from mParticle consent
if (self.configuration[kMPFIRGA4AdStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]]) {
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdStorageKey]];
adStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
}
if (self.configuration[kMPFIRGA4AdUserDataKey] && userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]]) {
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdUserDataKey]];
adUserDataStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
}
if (self.configuration[kMPFIRGA4AnalyticsStorageKey] && userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]]) {
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AnalyticsStorageKey]];
analyticsStorageStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
}
if (self.configuration[kMPFIRGA4AdPersonalizationKey] && userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]]) {
MPGDPRConsent *consent = userConsentMap[self.configuration[kMPFIRGA4AdPersonalizationKey]];
adPersonalizationStatus = consent.consented ? FIRConsentStatusGranted : FIRConsentStatusDenied;
}

// Construct a dictionary of consents
NSMutableDictionary *uploadDict = [[NSMutableDictionary alloc] init];
if (adStorageStatus) {
uploadDict[FIRConsentTypeAdStorage] = adStorageStatus;
}
if (adUserDataStatus) {
uploadDict[FIRConsentTypeAdUserData] = adUserDataStatus;
}
if (analyticsStorageStatus) {
uploadDict[FIRConsentTypeAnalyticsStorage] = analyticsStorageStatus;
}
if (adPersonalizationStatus) {
uploadDict[FIRConsentTypeAdPersonalization] = adPersonalizationStatus;
}

// Update consent state with FIRAnalytics
[FIRAnalytics setConsent:uploadDict];
}

- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters {
switch (commerceEvent.action) {
case MPCommerceEventActionAddToCart:
Expand Down

0 comments on commit ec62965

Please sign in to comment.