Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions PostHog/Classes/PHGPostHog.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@interface PHGPostHog ()

@property (nonatomic, assign) BOOL enabled;
@property (nonatomic, strong) NSDate *lastApplicationLifecycleEventExecutionTime;
@property (nonatomic, strong) PHGPostHogConfiguration *configuration;
@property (nonatomic, strong) PHGStoreKitCapturer *storeKitCapturer;
@property (nonatomic, strong) PHGPayloadManager *payloadManager;
Expand All @@ -46,6 +47,7 @@ - (instancetype)initWithConfiguration:(PHGPostHogConfiguration *)configuration
if (self = [self init]) {
self.configuration = configuration;
self.enabled = YES;
self.lastApplicationLifecycleEventExecutionTime = nil;

// In swift this would not have been OK... But hey.. It's objc
// TODO: Figure out if this is really the best way to do things here.
Expand Down Expand Up @@ -167,7 +169,7 @@ - (void)_applicationDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (void)_applicationWillEnterForeground
{
if (!self.configuration.captureApplicationLifecycleEvents) {
if (!self.configuration.captureApplicationLifecycleEvents || !self.shouldCaptureApplicationLifecycleEvent) {
return;
}
[self capture:@"Application Opened" properties:@{
Expand All @@ -177,7 +179,7 @@ - (void)_applicationWillEnterForeground

- (void)_applicationDidEnterBackground
{
if (!self.configuration.captureApplicationLifecycleEvents) {
if (!self.configuration.captureApplicationLifecycleEvents || !self.shouldCaptureApplicationLifecycleEvent) {
return;
}
[self capture: @"Application Backgrounded"];
Expand Down Expand Up @@ -460,4 +462,16 @@ - (void)run:(PHGEventType)eventType payload:(PHGPayload *)payload
[self.runner run:context callback:nil];
}

- (BOOL)shouldCaptureApplicationLifecycleEvent
{
NSDate *now = [NSDate date];
NSTimeInterval throttleInterval = _configuration.applicationLifecycleEventsThrottleInterval;

if (_lastApplicationLifecycleEventExecutionTime != nil && [now timeIntervalSinceDate:_lastApplicationLifecycleEventExecutionTime] < throttleInterval) {
return NO;
} else {
_lastApplicationLifecycleEventExecutionTime = now;
return YES;
}
}
@end
4 changes: 4 additions & 0 deletions PostHog/Classes/PHGPostHogConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ typedef NSMutableURLRequest *_Nonnull (^PHGRequestFactory)(NSURL *_Nonnull);
*/
@property (nonatomic, assign) BOOL captureApplicationLifecycleEvents;

/**
* The throttle interval for certain application lifecycle events, such as "Application Opened" (from background) and "Application Backgrounded". 900 seconds by default.
*/
@property (nonatomic, assign) NSTimeInterval applicationLifecycleEventsThrottleInterval;

/**
* Whether the posthog client should record bluetooth information. If `YES`, please make sure to add a description for `NSBluetoothPeripheralUsageDescription` in your `Info.plist` explaining explaining why your app is accessing Bluetooth APIs. `NO` by default.
Expand Down
1 change: 1 addition & 0 deletions PostHog/Classes/PHGPostHogConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ - (instancetype)init
self.flushAt = 20;
self.flushInterval = 30;
self.maxQueueSize = 1000;
self.applicationLifecycleEventsThrottleInterval = 900; // 15 minutes in seconds
self.libraryName = @"posthog-ios";
self.libraryVersion = [PHGPostHog version];
self.payloadFilters = @{
Expand Down