Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yarneo committed Apr 28, 2017
1 parent 66986bc commit 142152e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
10 changes: 6 additions & 4 deletions Mixpanel/AutomaticEvents.m
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions Mixpanel/MPNetwork.m
Expand Up @@ -10,6 +10,7 @@
#import "MPNetworkPrivate.h"
#import "MPLogger.h"
#import "Mixpanel.h"
#import "MixpanelPrivate.h"
#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#endif
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 17 additions & 5 deletions Mixpanel/Mixpanel.m
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"];
}
}

Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion Mixpanel/MixpanelPrivate.h
Expand Up @@ -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;
Expand Down

0 comments on commit 142152e

Please sign in to comment.