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

Fix for iOS Application Installed event #56

Merged
merged 5 commits into from
Aug 20, 2022
Merged
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
4 changes: 0 additions & 4 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>com.claimsforce.segment.WRITE_KEY</key>
<string>YOUR_WRITE_KEY_GOES_HERE</string>
<key>com.claimsforce.segment.TRACK_APPLICATION_LIFECYCLE_EVENTS</key>
<false/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
Expand Down
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_segment/flutter_segment.dart';

void main() {
void main() async {
/// Wait until the platform channel is properly initialized so we can call
/// `setContext` during the app initialization.
WidgetsFlutterBinding.ensureInitialized();

Segment.config(
await Segment.config(
options: SegmentConfig(
writeKey: 'YOUR_WRITE_KEY_GOES_HERE',
trackApplicationLifecycleEvents: false,
Expand Down
54 changes: 54 additions & 0 deletions ios/Classes/FlutterSegmentPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ - (void)config:(FlutterMethodCall*)call result:(FlutterResult)result {
NSDictionary *options = call.arguments[@"options"];
SEGAnalyticsConfiguration *configuration = [FlutterSegmentPlugin createConfigFromDict:options];
[self setup:configuration];
if(!wasSetupFromFile) {
[self trackInstalledEvent];
}
result([NSNumber numberWithBool:YES]);
}
@catch (NSException *exception) {
Expand Down Expand Up @@ -396,4 +399,55 @@ + (NSDictionary *) mergeDictionary: (NSDictionary *) first with: (NSDictionary *
return result;
}


#pragma mark Work around for Firing Installed / Updated events
// See https://github.com/segmentio/analytics-ios/blob/fc64997865619f73bbab196c164f7845a13da110/Segment/Classes/SEGAnalytics.m#L163

NSString *const SEGVersionKey = @"SEGVersionKey";
NSString *const SEGBuildKeyV1 = @"SEGBuildKey";
NSString *const SEGBuildKeyV2 = @"SEGBuildKeyV2";

- (void)trackInstalledEvent
{
// Previously SEGBuildKey was stored an integer. This was incorrect because the CFBundleVersion
// can be a string. This migrates SEGBuildKey to be stored as a string.
NSInteger previousBuildV1 = [[NSUserDefaults standardUserDefaults] integerForKey:SEGBuildKeyV1];
if (previousBuildV1) {
[[NSUserDefaults standardUserDefaults] setObject:[@(previousBuildV1) stringValue] forKey:SEGBuildKeyV2];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SEGBuildKeyV1];
}

NSString *previousVersion = [[NSUserDefaults standardUserDefaults] stringForKey:SEGVersionKey];
NSString *previousBuildV2 = [[NSUserDefaults standardUserDefaults] stringForKey:SEGBuildKeyV2];

NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
NSString *currentBuild = [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];

if (!previousBuildV2) {
[[SEGAnalytics sharedAnalytics] track:@"Application Installed" properties:@{
@"version" : currentVersion ?: @"",
@"build" : currentBuild ?: @"",
}];
} else if (![currentBuild isEqualToString:previousBuildV2]) {
[[SEGAnalytics sharedAnalytics] track:@"Application Updated" properties:@{
@"previous_version" : previousVersion ?: @"",
@"previous_build" : previousBuildV2 ?: @"",
@"version" : currentVersion ?: @"",
@"build" : currentBuild ?: @"",
}];
}

[[SEGAnalytics sharedAnalytics] track:@"Application Opened" properties:@{
@"from_background" : @NO,
@"version" : currentVersion ?: @"",
@"build" : currentBuild ?: @"",
@"referring_application" : @"",
@"url" : @"",
}];

[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:SEGVersionKey];
[[NSUserDefaults standardUserDefaults] setObject:currentBuild forKey:SEGBuildKeyV2];
[[NSUserDefaults standardUserDefaults] synchronize];
}

@end