Skip to content

Commit

Permalink
refactor: add save build/version check and move events to constant file
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang1520 committed Jun 30, 2023
1 parent f181eb3 commit 5a8463f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
20 changes: 20 additions & 0 deletions Sources/Amplitude/AMPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,23 @@ extern NSString *const AMP_PLAN_VERSION_ID;
// Ingestion Metadata
extern NSString *const AMP_INGESTION_METADATA_SOURCE_NAME;
extern NSString *const AMP_INGESTION_METADATA_SOURCE_VERSION;

// Events
extern NSString *const kAMPSessionStartEvent;
extern NSString *const kAMPSessionEndEvent;
extern NSString *const kAMPApplicationInstalled;
extern NSString *const kAMPApplicationUpdated;
extern NSString *const kAMPApplicationOpened;
extern NSString *const kAMPApplicationBackgrounded;
extern NSString *const kAMPDeepLinkOpened;
extern NSString *const kAMPScreenViewed;
extern NSString *const kAMPRevenueEvent;

extern NSString *const kAMPEventPropVersion;
extern NSString *const kAMPEventPropBuild;
extern NSString *const kAMPEventPropPreviousVersion;
extern NSString *const kAMPEventPropPreviousBuild;
extern NSString *const kAMPEventPropFromBackground;
extern NSString *const kAMPEventPropLinkUrl;
extern NSString *const kAMPEventPropLinkReferrer;
extern NSString *const kAMPEventPropScreenName;
20 changes: 20 additions & 0 deletions Sources/Amplitude/AMPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@

NSString *const AMP_INGESTION_METADATA_SOURCE_NAME = @"source_name";
NSString *const AMP_INGESTION_METADATA_SOURCE_VERSION = @"source_version";

// Amplitude Events
NSString *const kAMPSessionStartEvent = @"session_start";
NSString *const kAMPSessionEndEvent = @"session_end";
NSString *const kAMPApplicationInstalled = @"[Amplitude] Application Installed";
NSString *const kAMPApplicationUpdated = @"[Amplitude] Application Updated";
NSString *const kAMPApplicationOpened = @"[Amplitude] Application Opened";
NSString *const kAMPApplicationBackgrounded = @"[Amplitude] Application Backgrounded";
NSString *const kAMPDeepLinkOpened = @"[Amplitude] Deep Link Opened";
NSString *const kAMPScreenViewed = @"[Amplitude] Screen Viewed";
NSString *const kAMPRevenueEvent = @"revenue_amount";

NSString *const kAMPEventPropVersion = @"[Amplitude] Version";
NSString *const kAMPEventPropBuild = @"[Amplitude] Build";
NSString *const kAMPEventPropPreviousVersion = @"[Amplitude] Previous Version";
NSString *const kAMPEventPropPreviousBuild = @"[Amplitude] Previous Build";
NSString *const kAMPEventPropFromBackground = @"[Amplitude] From Background";
NSString *const kAMPEventPropLinkUrl = @"[Amplitude] Link URL";
NSString *const kAMPEventPropLinkReferrer = @"[Amplitude] Link Referrer";
NSString *const kAMPEventPropScreenName = @"[Amplitude] Screen Name";
27 changes: 7 additions & 20 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@ @interface Amplitude ()
@property (nonatomic, copy, readwrite) NSString *contentTypeHeader;
@end

NSString *const kAMPSessionStartEvent = @"session_start";
NSString *const kAMPSessionEndEvent = @"session_end";
NSString *const kAMPApplicationInstalled = @"[Amplitude] Application Installed";
NSString *const kAMPApplicationUpdated = @"[Amplitude] Application Updated";
NSString *const kAMPApplicationOpened = @"[Amplitude] Application Opened";
NSString *const kAMPApplicationBackgrounded = @"[Amplitude] Application Backgrounded";
NSString *const kAMPDeepLinkOpened = @"[Amplitude] Deep Link Opened";
NSString *const kAMPRevenueEvent = @"revenue_amount";

NSString *const kAMPEventPropVersion = @"[Amplitude] Version";
NSString *const kAMPEventPropBuild = @"[Amplitude] Build";
NSString *const kAMPEventPropPreviousVersion = @"[Amplitude] Previous Version";
NSString *const kAMPEventPropPreviousBuild = @"[Amplitude] Previous Build";
NSString *const kAMPEventPropFromBackground = @"[Amplitude] From Background";
NSString *const kAMPEventPropLinkUrl = @"[Amplitude] Link URL";
NSString *const kAMPEventPropLinkReferrer = @"[Amplitude] Link Referrer";

static NSString *const BACKGROUND_QUEUE_NAME = @"BACKGROUND";
static NSString *const DATABASE_VERSION = @"database_version";
static NSString *const DEVICE_ID = @"device_id";
Expand Down Expand Up @@ -470,9 +453,13 @@ - (void)handleAppStateUpdates:(NSNotification *)notification {
kAMPEventPropFromBackground: @NO,
}];

// persist the build/version
[_dbHelper insertOrReplaceKeyValue:APP_BUILD value:currentBuild];
[_dbHelper insertOrReplaceKeyValue:APP_VERSION value:currentVersion];
// persist the build/version when changed
if (currentBuild ? ![currentBuild isEqualToString:previousBuild] : (previousBuild != nil)) {
[_dbHelper insertOrReplaceKeyValue:APP_BUILD value:currentBuild];
}
if (currentVersion ? ![currentVersion isEqualToString:previousVersion] : (previousVersion != nil)) {
[_dbHelper insertOrReplaceKeyValue:APP_VERSION value:currentVersion];
}
} else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
NSString *currentBuild = [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
Expand Down
4 changes: 2 additions & 2 deletions Sources/Amplitude/UIViewController+AMPScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
#import <objc/runtime.h>
#import "UIViewController+AMPScreen.h"
#import "Amplitude.h"
#import "AMPConstants.h"

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
NSString *const kAMPScreenViewed = @"[Amplitude] Screen Viewed";
NSString *const kAMPEventPropScreenName = @"[Amplitude] Screen Name";


@implementation UIViewController (AMPScreen)

Expand Down
24 changes: 12 additions & 12 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1593,11 +1593,11 @@ - (void)testObserveDidFinishLaunchingNotification {
[client flushQueue];

NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 1];
XCTAssertEqualObjects([event1 objectForKey:@"event_type"], @"[Amplitude] Application Installed");
XCTAssertEqualObjects([event1 objectForKey:@"event_type"], kAMPApplicationInstalled);

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], @"[Amplitude] Application Opened");
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:@"[Amplitude] From Background"], @NO);
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @NO);
}

- (void)testObserveDidFinishLaunchingNotificationWithPreviousBuild {
Expand Down Expand Up @@ -1625,8 +1625,8 @@ - (void)testObserveDidFinishLaunchingNotificationWithPreviousBuild {
XCTAssertTrue([[event1 objectForKey:@"event_type"] hasPrefix:@"[Amplitude] Application"]);

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], @"[Amplitude] Application Opened");
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:@"[Amplitude] From Background"], @NO);
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @NO);
}

- (void)testObserveWillEnterForegroundNotification {
Expand All @@ -1646,8 +1646,8 @@ - (void)testObserveWillEnterForegroundNotification {
[client flushQueue];

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], @"[Amplitude] Application Opened");
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:@"[Amplitude] From Background"], @YES);
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @YES);
}

- (void)testObserveDidEnterBackgroundNotification {
Expand All @@ -1667,7 +1667,7 @@ - (void)testObserveDidEnterBackgroundNotification {
[client flushQueue];

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], @"[Amplitude] Application Backgrounded");
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationBackgrounded);
}

#endif
Expand All @@ -1685,8 +1685,8 @@ - (void)testContinueUserActivityFiresDeepLinkEvent {
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:instanceName];
XCTAssertEqualObjects([event objectForKey:@"event_type"], @"[Amplitude] Deep Link Opened");
XCTAssertEqualObjects([[event objectForKey:@"event_properties"] objectForKey:@"[Amplitude] Link URL"], @"https://test-app.com");
XCTAssertEqualObjects([event objectForKey:@"event_type"], kAMPDeepLinkOpened);
XCTAssertEqualObjects([[event objectForKey:@"event_properties"] objectForKey:kAMPEventPropLinkUrl], @"https://test-app.com");
}

- (void)testOpenURLFiresDeepLinkEvent {
Expand All @@ -1701,8 +1701,8 @@ - (void)testOpenURLFiresDeepLinkEvent {
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:instanceName];
XCTAssertEqualObjects([event objectForKey:@"event_type"], @"[Amplitude] Deep Link Opened");
XCTAssertEqualObjects([[event objectForKey:@"event_properties"] objectForKey:@"[Amplitude] Link URL"], @"https://test-app.com");
XCTAssertEqualObjects([event objectForKey:@"event_type"], kAMPDeepLinkOpened);
XCTAssertEqualObjects([[event objectForKey:@"event_properties"] objectForKey:kAMPEventPropLinkUrl], @"https://test-app.com");
}

@end

0 comments on commit 5a8463f

Please sign in to comment.