From 3b74ab028f448b0342f908245f19f6a9f4563b97 Mon Sep 17 00:00:00 2001 From: belanovich-sy Date: Tue, 25 Jun 2024 23:07:17 +0300 Subject: [PATCH] Add AppEnvironment to AppMetricaConfiguration dfac7bc6e235063c1768a1a667ef709b9e857564 --- AppMetricaCore/Sources/AMAAppMetricaConfiguration.m | 1 + AppMetricaCore/Sources/AMAAppMetricaImpl.m | 12 ++++++++++++ .../Sources/include/AppMetricaCore/AMAAppMetrica.h | 1 + .../AppMetricaCore/AMAAppMetricaConfiguration.h | 9 ++++++++- AppMetricaCore/Tests/AMAAppMetricaImplTests.m | 9 +++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/AppMetricaCore/Sources/AMAAppMetricaConfiguration.m b/AppMetricaCore/Sources/AMAAppMetricaConfiguration.m index 447e8af..58a340e 100644 --- a/AppMetricaCore/Sources/AMAAppMetricaConfiguration.m +++ b/AppMetricaCore/Sources/AMAAppMetricaConfiguration.m @@ -49,6 +49,7 @@ - (void)setDefaultValues _allowsBackgroundLocationUpdates = NO; _revenueAutoTrackingEnabled = kAMADefaultRevenueAutoTrackingEnabled; _appOpenTrackingEnabled = kAMADefaultAppOpenTrackingEnabled; + _appEnvironment = nil; } #pragma mark - Properties diff --git a/AppMetricaCore/Sources/AMAAppMetricaImpl.m b/AppMetricaCore/Sources/AMAAppMetricaImpl.m index 25cafe6..d9f0084 100644 --- a/AppMetricaCore/Sources/AMAAppMetricaImpl.m +++ b/AppMetricaCore/Sources/AMAAppMetricaImpl.m @@ -173,6 +173,9 @@ - (void)activateWithConfiguration:(AMAAppMetricaConfiguration *)configuration if (configuration.revenueAutoTrackingEnabled) { [self.autoPurchasesWatcher startWatchingWithReporter:reporter]; } + if (configuration.appEnvironment != nil) { + [self applyAppEnvironment:configuration.appEnvironment]; + } [self logMetricaStart]; } @@ -1067,6 +1070,15 @@ - (void)setupAppEnvironmentPollingDelegatesWithStorage:(AMAReporterStorage *)rep #pragma mark - Environment - +- (void)applyAppEnvironment:(NSDictionary *)appEnvironment +{ + @synchronized (self) { + [appEnvironment enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) { + [self setAppEnvironmentValue:value forKey:key]; + }]; + } +} + - (void)setAppEnvironmentValue:(NSString *)value forKey:(NSString *)key { @synchronized (self) { diff --git a/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetrica.h b/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetrica.h index f20d760..4e85d43 100644 --- a/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetrica.h +++ b/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetrica.h @@ -351,6 +351,7 @@ NS_SWIFT_NAME(setupWebViewReporting(with:onFailure:)); /** Setting key - value data to be used as additional information, associated with all future events. If value is nil, previously set key-value is removed. Does nothing if key hasn't been added. + To ensure that data is associated with all events from the moment of activation, specify the appEnvironment property within AMAAppMetricaConfiguration. @param value The app environment value. @param key The app environment key. diff --git a/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetricaConfiguration.h b/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetricaConfiguration.h index 0f68e66..09324fb 100644 --- a/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetricaConfiguration.h +++ b/AppMetricaCore/Sources/include/AppMetricaCore/AMAAppMetricaConfiguration.h @@ -92,7 +92,7 @@ NS_SWIFT_NAME(AppMetricaConfiguration) @property (nonatomic, assign) BOOL allowsBackgroundLocationUpdates; /** Enable/disable accurate location retrieval for internal location manager. - + Disabled by default. Has effect only when locationTrackingEnabled is 'YES', and location is not set manually. */ @@ -178,6 +178,13 @@ NS_SWIFT_NAME(AppMetricaConfiguration) */ @property (nonatomic, copy, nullable) NSArray *customHosts; +/** Application environment to be set during initialization. + + Setting key - value data to be used as additional information, associated with all events from the moment of activation. + If value is nil, previously set key-value is removed. Does nothing if key hasn't been added. + */ +@property (nonatomic, copy, nullable) NSDictionary *appEnvironment; + @end NS_ASSUME_NONNULL_END diff --git a/AppMetricaCore/Tests/AMAAppMetricaImplTests.m b/AppMetricaCore/Tests/AMAAppMetricaImplTests.m index 0be8852..112c441 100644 --- a/AppMetricaCore/Tests/AMAAppMetricaImplTests.m +++ b/AppMetricaCore/Tests/AMAAppMetricaImplTests.m @@ -181,6 +181,15 @@ @interface AMAAppMetricaImpl () [impl activateWithConfiguration:configuration]; [[appEnvironment().dictionaryEnvironment should] haveCountOf:0]; }); + + it(@"Should set configuration app environment", ^{ + NSDictionary *appEnvDict = @{ @"key1" : @"value1", @"key2" : @"value2" }; + AMAAppMetricaConfiguration *configuration = [[AMAAppMetricaConfiguration alloc] initWithAPIKey:apiKey]; + configuration.appEnvironment = appEnvDict; + + [impl activateWithConfiguration:configuration]; + [[appEnvironment().dictionaryEnvironment should] equal:appEnvDict]; + }); }); context(@"Event polling", ^{ let(impl, ^{