diff --git a/Mixpanel/AutomaticEvents.m b/Mixpanel/AutomaticEvents.m index 702951a3d..0e9a306cc 100644 --- a/Mixpanel/AutomaticEvents.m +++ b/Mixpanel/AutomaticEvents.m @@ -25,7 +25,9 @@ + (void)setAppStartTime:(NSTimeInterval)appStartTime { _appStartTime = appStartT __attribute__((constructor)) static void initialize_appStartTime() { - AutomaticEvents.appStartTime = [[NSDate date] timeIntervalSince1970]; + if (AutomaticEvents.appStartTime == 0) { + AutomaticEvents.appStartTime = [[NSDate date] timeIntervalSince1970]; + } } - (instancetype)init @@ -67,8 +69,8 @@ - (void)initializeEvents:(MixpanelPeople *)peopleInstance { } [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(appEnteredBackground:) - name:UIApplicationDidEnterBackgroundNotification + selector:@selector(appWillResignActive:) + name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self @@ -97,7 +99,7 @@ - (void)initializeEvents:(MixpanelPeople *)peopleInstance { } -- (void)appEnteredBackground:(NSNotification *)notification { +- (void)appWillResignActive:(NSNotification *)notification { sessionLength = [[NSDate date] timeIntervalSince1970] - sessionStartTime; if (sessionLength > (double)(self.minimumSessionDuration / 1000)) { NSMutableDictionary *properties = [[NSMutableDictionary alloc] diff --git a/Mixpanel/MPNetwork.m b/Mixpanel/MPNetwork.m index 0493c5d7b..48432be86 100644 --- a/Mixpanel/MPNetwork.m +++ b/Mixpanel/MPNetwork.m @@ -10,6 +10,7 @@ #import "MPNetworkPrivate.h" #import "MPLogger.h" #import "Mixpanel.h" +#import "MixpanelPrivate.h" #if !TARGET_OS_OSX #import #endif @@ -45,7 +46,32 @@ - (instancetype)initWithServerURL:(NSURL *)serverURL mixpanel:(Mixpanel *)mixpan #pragma mark - Flush - (void)flushEventQueue:(NSMutableArray *)events { + NSMutableArray *automaticEventsQueue; + @synchronized (self.mixpanel) { + automaticEventsQueue = [self orderAutomaticEvents:events]; + } [self flushQueue:events endpoint:MPNetworkEndpointTrack]; + @synchronized (self.mixpanel) { + if (automaticEventsQueue) { + [events addObjectsFromArray:automaticEventsQueue]; + } + } +} + +- (NSMutableArray *)orderAutomaticEvents:(NSMutableArray *)events { + if (!self.mixpanel.automaticEventsEnabled || !self.mixpanel.automaticEventsEnabled.boolValue) { + NSMutableArray *discardedItems = [NSMutableArray array]; + for (NSDictionary *e in events) { + if ([e[@"event"] hasPrefix:@"MP: "]) { + [discardedItems addObject:e]; + } + } + [events removeObjectsInArray:discardedItems]; + if (!self.mixpanel.automaticEventsEnabled) { + return discardedItems; + } + } + return nil; } - (void)flushPeopleQueue:(NSMutableArray *)people { diff --git a/Mixpanel/Mixpanel.m b/Mixpanel/Mixpanel.m index 8fb7f1f41..3c2e82c31 100755 --- a/Mixpanel/Mixpanel.m +++ b/Mixpanel/Mixpanel.m @@ -128,13 +128,15 @@ - (instancetype)initWithToken:(NSString *)apiToken launchOptions:(NSDictionary * self.network = [[MPNetwork alloc] initWithServerURL:[NSURL URLWithString:self.serverURL] mixpanel:self]; self.people = [[MixpanelPeople alloc] initWithMixpanel:self]; -#if !MIXPANEL_NO_AUTOMATIC_EVENTS_SUPPORT - self.automaticEvents = [[AutomaticEvents alloc] init]; - self.automaticEvents.delegate = self; - [self.automaticEvents initializeEvents:self.people]; -#endif [self setUpListeners]; [self unarchive]; +#if !MIXPANEL_NO_AUTOMATIC_EVENTS_SUPPORT + if (!self.automaticEventsEnabled || [self.automaticEventsEnabled boolValue]) { + self.automaticEvents = [[AutomaticEvents alloc] init]; + self.automaticEvents.delegate = self; + [self.automaticEvents initializeEvents:self.people]; + } +#endif #if !MIXPANEL_NO_NOTIFICATION_AB_TEST_SUPPORT [self executeCachedVariants]; [self executeCachedEventBindings]; @@ -712,6 +714,7 @@ - (void)archiveProperties [p setValue:self.people.unidentifiedQueue forKey:@"peopleUnidentifiedQueue"]; [p setValue:self.shownNotifications forKey:@"shownNotifications"]; [p setValue:self.timedEvents forKey:@"timedEvents"]; + [p setValue:self.automaticEventsEnabled forKey:@"automaticEvents"]; MPLogInfo(@"%@ archiving properties data to %@: %@", self, filePath, p); if (![self archiveObject:p withFilePath:filePath]) { MPLogError(@"%@ unable to archive properties data", self); @@ -824,6 +827,7 @@ - (void)unarchiveProperties self.variants = properties[@"variants"] ?: [NSSet set]; self.eventBindings = properties[@"event_bindings"] ?: [NSSet set]; self.timedEvents = properties[@"timedEvents"] ?: [NSMutableDictionary dictionary]; + self.automaticEventsEnabled = properties[@"automaticEvents"]; } } @@ -1415,6 +1419,14 @@ - (void)checkForDecideResponseWithCompletion:(void (^)(NSArray *notifications, N MPLogError(@"%@ variants check response format error: %@", self, object); } + id rawAutomaticEvents = object[@"automatic_events"]; + if ([rawAutomaticEvents isKindOfClass:[NSNumber class]]) { + if (!self.automaticEventsEnabled || [self.automaticEventsEnabled boolValue] != [rawAutomaticEvents boolValue]) { + self.automaticEventsEnabled = rawAutomaticEvents; + [self archiveProperties]; + } + } + // Variants that are already running (may or may not have been marked as finished). NSSet *runningVariants = [NSSet setWithSet:[self.variants objectsPassingTest:^BOOL(MPVariant *var, BOOL *stop) { return var.running; }]]; // Variants that are marked as finished, (may or may not be running still). diff --git a/Mixpanel/MixpanelPrivate.h b/Mixpanel/MixpanelPrivate.h index dd350b319..dfad50c45 100644 --- a/Mixpanel/MixpanelPrivate.h +++ b/Mixpanel/MixpanelPrivate.h @@ -90,7 +90,7 @@ @property (nonatomic, strong) NSMutableDictionary *timedEvents; @property (nonatomic) BOOL decideResponseCached; - +@property (nonatomic, strong) NSNumber *automaticEventsEnabled; @property (nonatomic, strong) NSArray *notifications; @property (nonatomic, strong) id currentlyShowingNotification; @property (nonatomic, strong) NSMutableSet *shownNotifications;