Skip to content

Commit 0b4dedc

Browse files
extract logic and make functions available in swift
1 parent 78816e9 commit 0b4dedc

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

mParticle-Google-Analytics-Firebase-GA4/MPKitFirebaseGA4Analytics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
+ (void)setCustomNameStandardization:(NSString * _Nonnull (^_Nullable)(NSString * _Nonnull name))standardization;
2121
+ (NSString * _Nonnull (^_Nullable)(NSString * _Nonnull name))customNameStandardization;
2222

23+
- (nonnull NSDictionary<NSString *, NSString *> *)consentDictionaryForCurrentUser;
24+
- (NSNumber * _Nullable)resolvedConsentForPurpose:(NSString * _Nullable)purpose
25+
gdprConsents:(NSDictionary<NSString *, MPGDPRConsent *> * _Nonnull)gdprConsents;
26+
- (NSNumber * _Nullable)resolvedConsentFromDefault:(NSString * _Nonnull)defaultKey;
27+
- (NSNumber * _Nullable)resolvedConsentForMappingKey:(NSString * _Nonnull)mappingKey
28+
defaultKey:(NSString * _Nonnull)defaultKey
29+
gdprConsents:(NSDictionary<NSString *, MPGDPRConsent *> * _Nonnull)gdprConsents
30+
mapping:(NSDictionary<NSString *, NSString*> * _Nonnull)mapping;
31+
2332
@end
2433

2534
static NSString * _Nonnull const kMPFIRGA4ExternalUserIdentityType = @"externalUserIdentityType";

mParticle-Google-Analytics-Firebase-GA4/MPKitFirebaseGA4Analytics.m

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,36 +387,35 @@ - (MPKitExecStatus *)setConsentState:(nullable MPConsentState *)state {
387387
return [self execStatus:MPKitReturnCodeSuccess];
388388
}
389389

390-
- (void)updateConsent {
391-
NSArray<NSDictionary *> *mappings = [self mappingForKey: @"consentMappingSDK"];
390+
- (NSDictionary<NSString *, NSString *> *)consentDictionaryForCurrentUser {
391+
NSArray<NSDictionary *> *mappings = [self mappingForKey:@"consentMappingSDK"];
392392
NSDictionary<NSString *, NSString *> *mappingsConfig;
393393
if (mappings != nil) {
394-
mappingsConfig = [self convertToKeyValuePairs: mappings];
394+
mappingsConfig = [self convertToKeyValuePairs:mappings];
395395
}
396-
397-
396+
398397
MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser];
399398
NSDictionary<NSString *, MPGDPRConsent *> *gdprConsents = currentUser.consentState.gdprConsentState;
400399

401400
NSNumber *adStorage = [self resolvedConsentForMappingKey:kMPFIRGA4AdStorageKey
402401
defaultKey:kMPFIRGA4DefaultAdStorageKey
403402
gdprConsents:gdprConsents
404-
mapping:mappingsConfig];
403+
mapping:mappingsConfig];
405404

406405
NSNumber *adUserData = [self resolvedConsentForMappingKey:kMPFIRGA4AdUserDataKey
407406
defaultKey:kMPFIRGA4DefaultAdUserDataKey
408407
gdprConsents:gdprConsents
409-
mapping:mappingsConfig];
408+
mapping:mappingsConfig];
410409

411410
NSNumber *analyticsStorage = [self resolvedConsentForMappingKey:kMPFIRGA4AnalyticsStorageKey
412411
defaultKey:kMPFIRGA4DefaultAnalyticsStorageKey
413412
gdprConsents:gdprConsents
414-
mapping:mappingsConfig];
413+
mapping:mappingsConfig];
415414

416415
NSNumber *adPersonalization = [self resolvedConsentForMappingKey:kMPFIRGA4AdPersonalizationKey
417416
defaultKey:kMPFIRGA4DefaultAdPersonalizationKey
418417
gdprConsents:gdprConsents
419-
mapping:mappingsConfig];
418+
mapping:mappingsConfig];
420419

421420
NSMutableDictionary *uploadDict = [NSMutableDictionary dictionary];
422421

@@ -432,12 +431,16 @@ - (void)updateConsent {
432431
if (adPersonalization != nil) {
433432
uploadDict[FIRConsentTypeAdPersonalization] = adPersonalization.boolValue ? FIRConsentStatusGranted : FIRConsentStatusDenied;
434433
}
435-
436-
437-
// Update consent state with FIRAnalytics
438-
[FIRAnalytics setConsent:uploadDict];
434+
435+
return uploadDict;
439436
}
440437

438+
- (void)updateConsent {
439+
NSDictionary *consentDict = [self consentDictionaryForCurrentUser];
440+
[FIRAnalytics setConsent:consentDict];
441+
}
442+
443+
441444
- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters {
442445
switch (commerceEvent.action) {
443446
case MPCommerceEventActionAddToCart:
@@ -673,16 +676,24 @@ - (NSNumber * _Nullable)resolvedConsentForMappingKey:(NSString *)mappingKey
673676
gdprConsents:(NSDictionary<NSString *, MPGDPRConsent *> *)gdprConsents
674677
mapping:(NSDictionary<NSString *, NSString*> *) mapping {
675678

676-
// Prefer mParticle Consent if available
677679
NSString *purpose = mapping[mappingKey];
678-
if (purpose) {
679-
MPGDPRConsent *consent = gdprConsents[purpose];
680-
if (consent) {
681-
return @(consent.consented);
682-
}
680+
NSNumber *consent = [self resolvedConsentForPurpose:purpose gdprConsents:gdprConsents];
681+
if (consent) {
682+
return consent;
683683
}
684+
return [self resolvedConsentFromDefault:defaultKey];
685+
}
686+
687+
- (NSNumber * _Nullable)resolvedConsentForPurpose:(NSString *)purpose
688+
gdprConsents:(NSDictionary<NSString *, MPGDPRConsent *> *)gdprConsents {
689+
MPGDPRConsent *consent = gdprConsents[purpose];
690+
if (consent) {
691+
return @(consent.consented);
692+
}
693+
return nil;
694+
}
684695

685-
// Fallback to configuration defaults
696+
- (NSNumber * _Nullable)resolvedConsentFromDefault:(NSString *)defaultKey {
686697
NSString *value = self->_configuration[defaultKey];
687698
if ([value isEqualToString:@"Granted"]) {
688699
return @(YES);

0 commit comments

Comments
 (0)