Skip to content

Commit

Permalink
fix: mark trackingoptions as atomic to prevent issues from concurrent…
Browse files Browse the repository at this point in the history
… access (#499)
  • Loading branch information
crleona authored Jul 26, 2024
1 parent bfdec45 commit 83465ed
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ @interface Amplitude ()
@property (nonatomic, copy, readwrite, nullable) NSString *userId;
@property (nonatomic, copy, readwrite) NSString *deviceId;
@property (nonatomic, copy, readwrite) NSString *contentTypeHeader;

@property (atomic, strong, nonnull) AMPTrackingOptions *inputTrackingOptions;
@property (atomic, strong, nonnull) AMPTrackingOptions *appliedTrackingOptions;
@property (atomic, copy, nonnull) NSDictionary *apiPropertiesTrackingOptions;
@end

static NSString *const BACKGROUND_QUEUE_NAME = @"BACKGROUND";
Expand Down Expand Up @@ -137,9 +141,6 @@ @implementation Amplitude {
AMPDeviceInfo *_deviceInfo;
BOOL _useAdvertisingIdForDeviceId;

AMPTrackingOptions *_inputTrackingOptions;
AMPTrackingOptions *_appliedTrackingOptions;
NSDictionary *_apiPropertiesTrackingOptions;
BOOL _coppaControlEnabled;

BOOL _inForeground;
Expand Down Expand Up @@ -788,31 +789,31 @@ - (void)truncateEventQueues {
- (void)annotateEvent:(NSMutableDictionary *)event {
[event setValue:self.userId forKey:@"user_id"];
[event setValue:self.deviceId forKey:@"device_id"];
if ([_appliedTrackingOptions shouldTrackPlatform]) {
if ([self.appliedTrackingOptions shouldTrackPlatform]) {
[event setValue:kAMPPlatform forKey:@"platform"];
}
if ([_appliedTrackingOptions shouldTrackVersionName]) {
if ([self.appliedTrackingOptions shouldTrackVersionName]) {
[event setValue:_deviceInfo.appVersion forKey:@"version_name"];
}
if ([_appliedTrackingOptions shouldTrackOSName]) {
if ([self.appliedTrackingOptions shouldTrackOSName]) {
[event setValue:_deviceInfo.osName forKey:@"os_name"];
}
if ([_appliedTrackingOptions shouldTrackOSVersion]) {
if ([self.appliedTrackingOptions shouldTrackOSVersion]) {
[event setValue:_deviceInfo.osVersion forKey:@"os_version"];
}
if ([_appliedTrackingOptions shouldTrackDeviceModel]) {
if ([self.appliedTrackingOptions shouldTrackDeviceModel]) {
[event setValue:_deviceInfo.model forKey:@"device_model"];
}
if ([_appliedTrackingOptions shouldTrackDeviceManufacturer]) {
if ([self.appliedTrackingOptions shouldTrackDeviceManufacturer]) {
[event setValue:_deviceInfo.manufacturer forKey:@"device_manufacturer"];
}
if ([_appliedTrackingOptions shouldTrackCarrier]) {
if ([self.appliedTrackingOptions shouldTrackCarrier]) {
[event setValue:_deviceInfo.carrier forKey:@"carrier"];
}
if ([_appliedTrackingOptions shouldTrackCountry]) {
if ([self.appliedTrackingOptions shouldTrackCountry]) {
[event setValue:_deviceInfo.country forKey:@"country"];
}
if ([_appliedTrackingOptions shouldTrackLanguage]) {
if ([self.appliedTrackingOptions shouldTrackLanguage]) {
[event setValue:_deviceInfo.language forKey:@"language"];
}
NSDictionary *library = @{
Expand All @@ -833,26 +834,26 @@ - (void)annotateEvent:(NSMutableDictionary *)event {

NSMutableDictionary *apiProperties = [event valueForKey:@"api_properties"];

if ([_appliedTrackingOptions shouldTrackIDFA]) {
if ([self.appliedTrackingOptions shouldTrackIDFA]) {
NSString *advertiserID = [self getAdSupportID];
if (advertiserID != nil) {
[apiProperties setValue:advertiserID forKey:@"ios_idfa"];
}
}
NSString *vendorID = _deviceInfo.vendorID;
if ([_appliedTrackingOptions shouldTrackIDFV] && vendorID) {
if ([self.appliedTrackingOptions shouldTrackIDFV] && vendorID) {
[apiProperties setValue:vendorID forKey:@"ios_idfv"];
}

if ([_appliedTrackingOptions shouldTrackLatLng] && self.locationInfoBlock != nil) {
if ([self.appliedTrackingOptions shouldTrackLatLng] && self.locationInfoBlock != nil) {
NSDictionary *location = self.locationInfoBlock();
if (location != nil) {
[apiProperties setValue:location forKey:@"location"];
}
}

if (self->_apiPropertiesTrackingOptions.count > 0) {
[apiProperties setValue:self->_apiPropertiesTrackingOptions forKey:@"tracking_options"];
if (self.apiPropertiesTrackingOptions.count > 0) {
[apiProperties setValue:self.apiPropertiesTrackingOptions forKey:@"tracking_options"];
}
}

Expand Down Expand Up @@ -1495,26 +1496,26 @@ - (void)setTrackingOptions:(AMPTrackingOptions *)options {
}

_inputTrackingOptions = options;
_appliedTrackingOptions = [AMPTrackingOptions copyOf:options];

AMPTrackingOptions *appliedTrackingOptions = [AMPTrackingOptions copyOf:options];
if (_coppaControlEnabled) {
[_appliedTrackingOptions mergeIn:[AMPTrackingOptions forCoppaControl]];
[appliedTrackingOptions mergeIn:[AMPTrackingOptions forCoppaControl]];
}

self->_apiPropertiesTrackingOptions = [NSDictionary dictionaryWithDictionary:[options getApiPropertiesTrackingOption]];
self.appliedTrackingOptions = appliedTrackingOptions;
self.apiPropertiesTrackingOptions = [options getApiPropertiesTrackingOption];
}

- (void)enableCoppaControl {
_coppaControlEnabled = YES;
[_appliedTrackingOptions mergeIn:[AMPTrackingOptions forCoppaControl]];
_apiPropertiesTrackingOptions = [_appliedTrackingOptions getApiPropertiesTrackingOption];
[self.appliedTrackingOptions mergeIn:[AMPTrackingOptions forCoppaControl]];
self.apiPropertiesTrackingOptions = [self.appliedTrackingOptions getApiPropertiesTrackingOption];
}

- (void)disableCoppaControl {
_coppaControlEnabled = NO;
// Restore it to original input.
_appliedTrackingOptions = [AMPTrackingOptions copyOf:_inputTrackingOptions];
_apiPropertiesTrackingOptions = [_appliedTrackingOptions getApiPropertiesTrackingOption];
self.appliedTrackingOptions = [AMPTrackingOptions copyOf:self.inputTrackingOptions];
self.apiPropertiesTrackingOptions = [self.appliedTrackingOptions getApiPropertiesTrackingOption];
}

- (void)setUserId:(NSString *)userId {
Expand Down Expand Up @@ -1665,7 +1666,7 @@ - (void)disableIdentifyBatching:(BOOL)disable {
#pragma mark - Getters for device data
- (NSString *)getAdSupportID {
NSString *result = nil;
if (self.adSupportBlock != nil && [_appliedTrackingOptions shouldTrackIDFA]) {
if (self.adSupportBlock != nil && [self.appliedTrackingOptions shouldTrackIDFA]) {
result = self.adSupportBlock();
}
// IDFA access was denied or still in progress.
Expand Down Expand Up @@ -1703,12 +1704,12 @@ - (void)initializeDeviceInfo {

- (NSString *)_getDeviceId {
NSString *deviceId = nil;
if (_useAdvertisingIdForDeviceId && [_appliedTrackingOptions shouldTrackIDFA]) {
if (_useAdvertisingIdForDeviceId && [self.appliedTrackingOptions shouldTrackIDFA]) {
deviceId = [self getAdSupportID];
}

// return identifierForVendor
if ([_appliedTrackingOptions shouldTrackIDFV] && !deviceId) {
if ([self.appliedTrackingOptions shouldTrackIDFV] && !deviceId) {
deviceId = _deviceInfo.vendorID;
}

Expand Down

0 comments on commit 83465ed

Please sign in to comment.