Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dev nps survey #986

Merged
merged 4 commits into from
Apr 26, 2022
Merged
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
80 changes: 70 additions & 10 deletions Sources/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ - (instancetype)initWithToken:(NSString *)apiToken
[self.automaticEvents initializeEvents:self.people apiToken:apiToken];
#endif
}
#if defined(DEBUG)
[self didDebugInit:apiToken];
#endif
instances[apiToken] = self;
}
return self;
Expand All @@ -200,6 +203,35 @@ - (instancetype)initWithToken:(NSString *)apiToken
useUniqueDistinctId:NO];
}

- (void)didDebugInit:(NSString *)distinctId
{
NSString *debugInitCountKey = @"MPDebugInitCountKey";
NSInteger debugInitCount = [[NSUserDefaults standardUserDefaults] integerForKey:debugInitCountKey] + 1;
if (debugInitCount == 1) {
[self sendHttpEvent:@"First SDK Debug Launch" apiToken:@"metrics-1" distinctId:distinctId];
}
[self checkForSurvey:distinctId debugInitCount: debugInitCount];
[[NSUserDefaults standardUserDefaults] setInteger:debugInitCount forKey:debugInitCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)checkForSurvey:(NSString *)distinctId
debugInitCount:(NSInteger)debugInitCount
{
NSString *surveyShownCountKey = @"MPSurveyShownCountKey";
NSInteger surveyShownCount = [[NSUserDefaults standardUserDefaults] integerForKey:surveyShownCountKey];
if ((debugInitCount > 10 && surveyShownCount < 1) || (debugInitCount > 20 && surveyShownCount < 2) || (debugInitCount > 30 && surveyShownCount < 3)) {
NSString *waveHand = @"\U0001f44b";
NSString *thumbsUp = @"\U0001f44d";
NSString *thumbsDown = @"\U0001f44e";
NSString *logString = [NSString stringWithFormat:@"%@%@ Zihe & Jared here, tell us about the Mixpanel developer experience! https://www.mixpanel.com/devnps %@%@", waveHand, waveHand, thumbsUp, thumbsDown];
NSLog(@"%@", logString);
surveyShownCount++;
[[NSUserDefaults standardUserDefaults] setInteger:surveyShownCount forKey:surveyShownCountKey];
[self sendHttpEvent:@"Dev NPS Survey Logged" apiToken:@"metrics-1" distinctId:distinctId properties:@{@"Survey Shown Count": @(surveyShownCount), @"Debug Launch Count": @(debugInitCount)}];
}
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
Expand Down Expand Up @@ -1295,20 +1327,18 @@ - (void)applicationDidEnterBackground:(NSNotification *)notification
NSString *trackedKey = [NSString stringWithFormat:@"MPTracked:%@", self.apiToken];
if (![[NSUserDefaults standardUserDefaults] boolForKey:trackedKey]) {
dispatch_group_enter(bgGroup);
NSString *requestData = [MPJSONHandler encodedJSONString:@[@{@"event": @"Integration",
@"properties": @{@"token": @"85053bf24bba75239b16a601d9387e17", @"mp_lib": @"iphone",
@"distinct_id": self.apiToken, @"$lib_version": self.libVersion}}]];

NSURLQueryItem *useIPAddressForGeoLocation = [NSURLQueryItem queryItemWithName:@"ip" value:self.useIPAddressForGeoLocation ? @"1": @"0"];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack withQueryItems:@[useIPAddressForGeoLocation] andBody:requestData];
[[[MPNetwork sharedURLSession] dataTaskWithRequest:request completionHandler:^(NSData *responseData,
NSURLResponse *urlResponse,
NSError *error) {
[self sendHttpEvent:@"Integration"
apiToken:@"85053bf24bba75239b16a601d9387e17"
distinctId:self.apiToken
properties:@{}
completionHandler:^(NSData *responseData,
NSURLResponse *urlResponse,
NSError *error) {
if (!error) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:trackedKey];
}
dispatch_group_leave(bgGroup);
}] resume];
}];
}

@synchronized (self) {
Expand Down Expand Up @@ -1346,6 +1376,36 @@ - (void)applicationWillEnterForeground:(NSNotificationCenter *)notification

#endif // MIXPANEL_NO_APP_LIFECYCLE_SUPPORT

- (void)sendHttpEvent:(NSString *)eventName
apiToken:(NSString *)apiToken
distinctId:(NSString *)distinctId
{
[self sendHttpEvent:eventName apiToken:apiToken distinctId:distinctId properties:@{} completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];
}


- (void)sendHttpEvent:(NSString *)eventName
apiToken:(NSString *)apiToken
distinctId:(NSString *)distinctId
properties:(NSDictionary *)properties
{
[self sendHttpEvent:eventName apiToken:apiToken distinctId:distinctId properties:properties completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];
}

- (void)sendHttpEvent:(NSString *)eventName
apiToken:(NSString *)apiToken
distinctId:(NSString *)distinctId
properties:(NSDictionary *)properties
completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
{
NSMutableDictionary *trackingProperties = [[NSMutableDictionary alloc] initWithDictionary:@{@"token": apiToken, @"mp_lib": @"iphone", @"distinct_id": distinctId, @"$lib_version": self.libVersion}];
[trackingProperties addEntriesFromDictionary:properties];
NSString *requestData = [MPJSONHandler encodedJSONString:@[@{@"event": eventName, @"properties": trackingProperties}]];
NSURLQueryItem *useIPAddressForGeoLocation = [NSURLQueryItem queryItemWithName:@"ip" value:self.useIPAddressForGeoLocation ? @"1": @"0"];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack withQueryItems:@[useIPAddressForGeoLocation] andBody:requestData];
[[[MPNetwork sharedURLSession] dataTaskWithRequest:request completionHandler:completionHandler] resume];
}

#pragma mark - Logging
- (void)setEnableLogging:(BOOL)enableLogging
{
Expand Down