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

Make compatible with iOS Extensions #50

Closed
wants to merge 15 commits into from
Closed
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
22 changes: 17 additions & 5 deletions Amplitude.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
605E43CA1BA0D92A00FD78CE /* AMPDatabaseHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 605E43C91BA0D92A00FD78CE /* AMPDatabaseHelper.m */; };
605E43CD1BA0FEE500FD78CE /* AMPDatabaseHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 605E43CC1BA0FEE500FD78CE /* AMPDatabaseHelperTests.m */; };
605E43CF1BA1065D00FD78CE /* AMPDatabaseHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 605E43C91BA0D92A00FD78CE /* AMPDatabaseHelper.m */; };
608865D51BC30EBB00233023 /* AMPIdentify.m in Sources */ = {isa = PBXBuildFile; fileRef = 608865D41BC30EBB00233023 /* AMPIdentify.m */; settings = {ASSET_TAGS = (); }; };
608865D71BC311D700233023 /* AMPIdentify.m in Sources */ = {isa = PBXBuildFile; fileRef = 608865D41BC30EBB00233023 /* AMPIdentify.m */; settings = {ASSET_TAGS = (); }; };
608865D91BC3176600233023 /* IdentifyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 608865D81BC3176600233023 /* IdentifyTests.m */; settings = {ASSET_TAGS = (); }; };
60D418B41BC1BFE9006CC505 /* AMPUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 60D418B31BC1BFE9006CC505 /* AMPUtils.m */; settings = {ASSET_TAGS = (); }; };
60D418B51BC1C019006CC505 /* AMPUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 60D418B31BC1BFE9006CC505 /* AMPUtils.m */; settings = {ASSET_TAGS = (); }; };
9D265EF41AB3FA2500399D93 /* SSLPinningTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D265EF31AB3FA2500399D93 /* SSLPinningTests.m */; };
Expand Down Expand Up @@ -61,7 +64,9 @@
605E43C91BA0D92A00FD78CE /* AMPDatabaseHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPDatabaseHelper.m; sourceTree = "<group>"; };
605E43CB1BA0DB4F00FD78CE /* AMPDatabaseHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMPDatabaseHelper.h; sourceTree = "<group>"; };
605E43CC1BA0FEE500FD78CE /* AMPDatabaseHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPDatabaseHelperTests.m; sourceTree = "<group>"; };
605E43CE1BA0FF4300FD78CE /* AMPDatabaseHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMPDatabaseHelperTests.h; sourceTree = "<group>"; };
608865D41BC30EBB00233023 /* AMPIdentify.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPIdentify.m; sourceTree = "<group>"; };
608865D61BC30ECA00233023 /* AMPIdentify.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMPIdentify.h; sourceTree = "<group>"; };
608865D81BC3176600233023 /* IdentifyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IdentifyTests.m; sourceTree = "<group>"; };
60D418B21BC1BFE9006CC505 /* AMPUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMPUtils.h; sourceTree = "<group>"; };
60D418B31BC1BFE9006CC505 /* AMPUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPUtils.m; sourceTree = "<group>"; };
73478F50C66F6A68F4367561 /* Pods-test.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-test.debug.xcconfig"; path = "Pods/Target Support Files/Pods-test/Pods-test.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -182,22 +187,24 @@
E98C05251A48E7FE00800C63 /* Amplitude */ = {
isa = PBXGroup;
children = (
60D418B21BC1BFE9006CC505 /* AMPUtils.h */,
60D418B31BC1BFE9006CC505 /* AMPUtils.m */,
E96785E11A48E93F00887CCD /* AMPARCMacros.h */,
E96785E21A48E93F00887CCD /* AMPConstants.h */,
9DC708591AD4B28300949778 /* AMPConstants.m */,
605E43C91BA0D92A00FD78CE /* AMPDatabaseHelper.m */,
605E43CB1BA0DB4F00FD78CE /* AMPDatabaseHelper.h */,
605E43C91BA0D92A00FD78CE /* AMPDatabaseHelper.m */,
E96785E31A48E93F00887CCD /* AMPDeviceInfo.h */,
E96785E41A48E93F00887CCD /* AMPDeviceInfo.m */,
608865D61BC30ECA00233023 /* AMPIdentify.h */,
608865D41BC30EBB00233023 /* AMPIdentify.m */,
E96785E61A48E93F00887CCD /* Amplitude.h */,
E96785E71A48E93F00887CCD /* Amplitude.m */,
9DC708561AD4A49E00949778 /* Amplitude+SSLPinning.h */,
E96785E81A48E93F00887CCD /* AMPLocationManagerDelegate.h */,
E96785E91A48E93F00887CCD /* AMPLocationManagerDelegate.m */,
9D40E1791AB3BF7F0095C7C6 /* AMPURLConnection.h */,
9D40E17A1AB3BF7F0095C7C6 /* AMPURLConnection.m */,
60D418B21BC1BFE9006CC505 /* AMPUtils.h */,
60D418B31BC1BFE9006CC505 /* AMPUtils.m */,
9D76A3A41ABA5F890062E132 /* ComodoRsaCA.der */,
9D76A3A31ABA5F890062E132 /* ComodoRsaDomainValidationCA.der */,
9D6E194B1ACCDE1C00352C6C /* SSLCertificatePinning */,
Expand All @@ -208,7 +215,6 @@
E98C052F1A48E7FE00800C63 /* AmplitudeTests */ = {
isa = PBXGroup;
children = (
605E43CE1BA0FF4300FD78CE /* AMPDatabaseHelperTests.h */,
605E43CC1BA0FEE500FD78CE /* AMPDatabaseHelperTests.m */,
9DFBB9C51AB0D1DD0017F703 /* Amplitude+Test.h */,
9DFBB9C61AB0D1DD0017F703 /* Amplitude+Test.m */,
Expand All @@ -217,6 +223,7 @@
9DFBB9C81AB0D26E0017F703 /* BaseTestCase.h */,
9DFBB9C91AB0D26E0017F703 /* BaseTestCase.m */,
9DDE2C021AE7069200B740EC /* DeviceInfoTests.m */,
608865D81BC3176600233023 /* IdentifyTests.m */,
9D40E1941AB3F6550095C7C6 /* InvalidCertificationAuthority.der */,
9DFBB9CB1AB0D47A0017F703 /* SessionTests.m */,
9D82D1D71AC1006600C3F321 /* SetupTests.m */,
Expand Down Expand Up @@ -393,6 +400,7 @@
files = (
9D6E19541ACCDE1C00352C6C /* ISPPinnedNSURLConnectionDelegate.m in Sources */,
605E43CA1BA0D92A00FD78CE /* AMPDatabaseHelper.m in Sources */,
608865D51BC30EBB00233023 /* AMPIdentify.m in Sources */,
60D418B41BC1BFE9006CC505 /* AMPUtils.m in Sources */,
9D40E17E1AB3BF7F0095C7C6 /* AMPURLConnection.m in Sources */,
E96785EC1A48E93F00887CCD /* AMPDeviceInfo.m in Sources */,
Expand All @@ -409,7 +417,9 @@
buildActionMask = 2147483647;
files = (
9D6E19561ACCDE9000352C6C /* ISPCertificatePinning.m in Sources */,
608865D91BC3176600233023 /* IdentifyTests.m in Sources */,
605E43CF1BA1065D00FD78CE /* AMPDatabaseHelper.m in Sources */,
608865D71BC311D700233023 /* AMPIdentify.m in Sources */,
9D6E19571ACCDE9000352C6C /* ISPPinnedNSURLConnectionDelegate.m in Sources */,
9D6E19581ACCDE9000352C6C /* ISPPinnedNSURLSessionDelegate.m in Sources */,
602A9A6B1B754E7B0067230C /* AmplitudeTests.m in Sources */,
Expand Down Expand Up @@ -512,6 +522,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 764FF1F5D4DC82CCE3BF4FE7 /* Pods.debug.xcconfig */;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_OBJC_ARC = YES;
OTHER_LDFLAGS = (
"$(inherited)",
Expand All @@ -526,6 +537,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 78F948F3BAF6B3E720A38B16 /* Pods.release.xcconfig */;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_OBJC_ARC = YES;
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down
7 changes: 7 additions & 0 deletions Amplitude/AMPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ extern const int kAMPEventMaxCount;
extern const int kAMPEventRemoveBatchSize;
extern const int kAMPEventUploadPeriodSeconds;
extern const long kAMPMinTimeBetweenSessionsMillis;
extern const int kAMPMaxStringLength;

extern NSString *const IDENTIFY_EVENT;
extern NSString *const AMP_OP_ADD;
extern NSString *const AMP_OP_SET;
extern NSString *const AMP_OP_SET_ONCE;
extern NSString *const AMP_OP_UNSET;
11 changes: 9 additions & 2 deletions Amplitude/AMPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
NSString *const kAMPVersion = @"3.1.1";
NSString *const kAMPEventLogDomain = @"api.amplitude.com";
NSString *const kAMPEventLogUrl = @"https://api.amplitude.com/";
const int kAMPApiVersion = 2;
const int kAMPDBVersion = 2;
const int kAMPApiVersion = 3;
const int kAMPDBVersion = 3;
const int kAMPDBFirstVersion = 2; // to detect if DB exists yet
const int kAMPEventUploadThreshold = 30;
const int kAMPEventUploadMaxBatchSize = 100;
const int kAMPEventMaxCount = 1000;
const int kAMPEventRemoveBatchSize = 20;
const int kAMPEventUploadPeriodSeconds = 30; // 30s
const long kAMPMinTimeBetweenSessionsMillis = 5 * 60 * 1000; // 5m
const int kAMPMaxStringLength = 1024;

NSString *const IDENTIFY_EVENT = @"$identify";
NSString *const AMP_OP_ADD = @"$add";
NSString *const AMP_OP_SET = @"$set";
NSString *const AMP_OP_SET_ONCE = @"$setOnce";
NSString *const AMP_OP_UNSET = @"$unset";
9 changes: 8 additions & 1 deletion Amplitude/AMPDatabaseHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
- (BOOL)deleteDB;

- (BOOL)addEvent:(NSString*) event;
- (NSDictionary*)getEvents:(long) upToId limit:(long) limit;
- (BOOL)addIdentify:(NSString*) identify;
- (NSMutableArray*)getEvents:(long) upToId limit:(long) limit;
- (NSMutableArray*)getIdentifys:(long) upToId limit:(long) limit;
- (int)getEventCount;
- (int)getIdentifyCount;
- (int)getTotalEventCount;
- (BOOL)removeEvents:(long) maxId;
- (BOOL)removeIdentifys:(long) maxIdentifyId;
- (BOOL)removeEvent:(long) eventId;
- (BOOL)removeIdentify:(long) identifyId;
- (long long)getNthEventId:(long) n;
- (long long)getNthIdentifyId:(long) n;

- (BOOL)insertOrReplaceKeyValue:(NSString*) key value:(NSString*) value;
- (BOOL)insertOrReplaceKeyLongValue:(NSString*) key value:(NSNumber*) value;
Expand Down
57 changes: 50 additions & 7 deletions Amplitude/AMPDatabaseHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ @implementation AMPDatabaseHelper
}

static NSString *const EVENT_TABLE_NAME = @"events";
static NSString *const IDENTIFY_TABLE_NAME = @"identifys";
static NSString *const ID_FIELD = @"id";
static NSString *const EVENT_FIELD = @"event";

Expand All @@ -33,6 +34,7 @@ @implementation AMPDatabaseHelper

static NSString *const DROP_TABLE = @"DROP TABLE IF EXISTS %@;";
static NSString *const CREATE_EVENT_TABLE = @"CREATE TABLE IF NOT EXISTS %@ (%@ INTEGER PRIMARY KEY AUTOINCREMENT, %@ TEXT);";
static NSString *const CREATE_IDENTIFY_TABLE = @"CREATE TABLE IF NOT EXISTS %@ (%@ INTEGER PRIMARY KEY AUTOINCREMENT, %@ TEXT);";
static NSString *const CREATE_STORE_TABLE = @"CREATE TABLE IF NOT EXISTS %@ (%@ TEXT PRIMARY KEY NOT NULL, %@ TEXT);";
static NSString *const CREATE_LONG_STORE_TABLE = @"CREATE TABLE IF NOT EXISTS %@ (%@ TEXT PRIMARY KEY NOT NULL, %@ INTEGER);";

Expand Down Expand Up @@ -97,6 +99,9 @@ - (BOOL)createTables
NSString *createEventsTable = [NSString stringWithFormat:CREATE_EVENT_TABLE, EVENT_TABLE_NAME, ID_FIELD, EVENT_FIELD];
success &= [db executeUpdate:createEventsTable];

NSString *createIdentifysTable = [NSString stringWithFormat:CREATE_IDENTIFY_TABLE, IDENTIFY_TABLE_NAME, ID_FIELD, EVENT_FIELD];
success &= [db executeUpdate:createIdentifysTable];

NSString *createStoreTable = [NSString stringWithFormat:CREATE_STORE_TABLE, STORE_TABLE_NAME, KEY_FIELD, VALUE_FIELD];
success &= [db executeUpdate:createStoreTable];

Expand Down Expand Up @@ -136,6 +141,9 @@ - (BOOL)upgrade:(int) oldVersion newVersion:(int) newVersion
if (newVersion <= 2) break;
}
case 2: {
NSString *createIdentifysTable = [NSString stringWithFormat:CREATE_IDENTIFY_TABLE, IDENTIFY_TABLE_NAME, ID_FIELD, EVENT_FIELD];
success &= [db executeUpdate:createIdentifysTable];

if (newVersion <= 3) break;
}
default:
Expand Down Expand Up @@ -168,6 +176,9 @@ - (BOOL)dropTables
NSString *dropEventTableSQL = [NSString stringWithFormat:DROP_TABLE, EVENT_TABLE_NAME];
success &= [db executeUpdate: dropEventTableSQL];

NSString *dropIdentifyTableSQL = [NSString stringWithFormat:DROP_TABLE, IDENTIFY_TABLE_NAME];
success &= [db executeUpdate: dropIdentifyTableSQL];

NSString *dropStoreTableSQL = [NSString stringWithFormat:DROP_TABLE, STORE_TABLE_NAME];
success &= [db executeUpdate: dropStoreTableSQL];

Expand Down Expand Up @@ -208,6 +219,11 @@ - (BOOL)addEvent:(NSString*) event
return [self addEventToTable:EVENT_TABLE_NAME event:event];
}

- (BOOL)addIdentify:(NSString*) identifyEvent
{
return [self addEventToTable:IDENTIFY_TABLE_NAME event:identifyEvent];
}

- (BOOL)addEventToTable:(NSString*) table event:(NSString*) event
{
__block BOOL success = NO;
Expand All @@ -234,15 +250,19 @@ - (BOOL)addEventToTable:(NSString*) table event:(NSString*) event
return success;
}

- (NSDictionary*)getEvents:(long) upToId limit:(long) limit
- (NSMutableArray*)getEvents:(long) upToId limit:(long) limit
{
return [self getEventsFromTable:EVENT_TABLE_NAME upToId:upToId limit:limit];
}

- (NSDictionary*)getEventsFromTable:(NSString*) table upToId:(long) upToId limit:(long) limit
- (NSMutableArray*)getIdentifys:(long) upToId limit:(long) limit
{
__block long maxId = -1;
__block NSMutableArray *events = [NSMutableArray array];
return [self getEventsFromTable:IDENTIFY_TABLE_NAME upToId:upToId limit:limit];
}

- (NSMutableArray*)getEventsFromTable:(NSString*) table upToId:(long) upToId limit:(long) limit
{
__block NSMutableArray *events = [[NSMutableArray alloc] init];

[_dbQueue inDatabase:^(FMDatabase *db) {
if (![db open]) {
Expand Down Expand Up @@ -283,14 +303,12 @@ - (NSDictionary*)getEventsFromTable:(NSString*) table upToId:(long) upToId limit
[event setValue:[NSNumber numberWithInt:eventId] forKey:@"event_id"];
[events addObject:event];
SAFE_ARC_RELEASE(event);
maxId = eventId;
}

[db close];
}];

NSDictionary *fetchedEvents = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithLong:maxId], @"max_id", events, @"events", nil];
return SAFE_ARC_AUTORELEASE(fetchedEvents);
return SAFE_ARC_AUTORELEASE(events);
}

- (BOOL)insertOrReplaceKeyValue:(NSString*) key value:(NSString*) value
Expand Down Expand Up @@ -386,6 +404,16 @@ - (int)getEventCount
return [self getEventCountFromTable:EVENT_TABLE_NAME];
}

- (int)getIdentifyCount
{
return [self getEventCountFromTable:IDENTIFY_TABLE_NAME];
}

- (int)getTotalEventCount
{
return [self getEventCount] + [self getIdentifyCount];
}

- (int)getEventCountFromTable:(NSString*) table
{
__block int count = 0;
Expand Down Expand Up @@ -421,6 +449,11 @@ - (BOOL)removeEvents:(long) maxId
return [self removeEventsFromTable:EVENT_TABLE_NAME maxId:maxId];
}

- (BOOL)removeIdentifys:(long) maxIdentifyId
{
return [self removeEventsFromTable:IDENTIFY_TABLE_NAME maxId:maxIdentifyId];
}

- (BOOL)removeEventsFromTable:(NSString*) table maxId:(long) maxId
{
__block BOOL success = NO;
Expand Down Expand Up @@ -448,6 +481,11 @@ - (BOOL)removeEvent:(long) eventId
return [self removeEventFromTable:EVENT_TABLE_NAME eventId:eventId];
}

- (BOOL)removeIdentify:(long) identifyId
{
return [self removeEventFromTable:IDENTIFY_TABLE_NAME eventId:identifyId];
}

- (BOOL)removeEventFromTable:(NSString*) table eventId:(long) eventId
{
__block BOOL success = NO;
Expand Down Expand Up @@ -475,6 +513,11 @@ - (long long)getNthEventId:(long) n
return [self getNthEventIdFromTable:EVENT_TABLE_NAME n:n];
}

- (long long)getNthIdentifyId:(long) n
{
return [self getNthEventIdFromTable:IDENTIFY_TABLE_NAME n:n];
}

- (long long)getNthEventIdFromTable:(NSString*) table n:(long) n
{
__block long long eventId = -1;
Expand Down
19 changes: 19 additions & 0 deletions Amplitude/AMPIdentify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// AMPIdentify.h
// Amplitude
//
// Created by Daniel Jih on 10/5/15.
// Copyright © 2015 Amplitude. All rights reserved.
//

@interface AMPIdentify : NSObject

@property (nonatomic, strong, readonly) NSMutableDictionary *userPropertyOperations;

+ (instancetype)identify;
- (AMPIdentify*)add:(NSString*) property value:(NSObject*) value;
- (AMPIdentify*)set:(NSString*) property value:(NSObject*) value;
- (AMPIdentify*)setOnce:(NSString*) property value:(NSObject*) value;
- (AMPIdentify*)unset:(NSString*) property;

@end
Loading