Skip to content

Commit

Permalink
Add AppEnvironment to AppMetricaConfiguration
Browse files Browse the repository at this point in the history
dfac7bc6e235063c1768a1a667ef709b9e857564
  • Loading branch information
Sergey70 committed Jun 25, 2024
1 parent 16dc93c commit 3b74ab0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions AppMetricaCore/Sources/AMAAppMetricaConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ - (void)setDefaultValues
_allowsBackgroundLocationUpdates = NO;
_revenueAutoTrackingEnabled = kAMADefaultRevenueAutoTrackingEnabled;
_appOpenTrackingEnabled = kAMADefaultAppOpenTrackingEnabled;
_appEnvironment = nil;
}

#pragma mark - Properties
Expand Down
12 changes: 12 additions & 0 deletions AppMetricaCore/Sources/AMAAppMetricaImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -1067,6 +1070,15 @@ - (void)setupAppEnvironmentPollingDelegatesWithStorage:(AMAReporterStorage *)rep

#pragma mark - Environment -

- (void)applyAppEnvironment:(NSDictionary<NSString *, NSString *> *)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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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<NSString *, NSString *> *appEnvironment;

@end

NS_ASSUME_NONNULL_END
9 changes: 9 additions & 0 deletions AppMetricaCore/Tests/AMAAppMetricaImplTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ @interface AMAAppMetricaImpl () <AMAExtendedStartupObservingDelegate>
[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, ^{
Expand Down

0 comments on commit 3b74ab0

Please sign in to comment.