Skip to content

Commit

Permalink
feat: Support seamless integration with amplitude experiment SDK (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Feb 5, 2022
1 parent 27af7b7 commit 26e7830
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
run: |
sudo xcode-select -switch /Applications/Xcode_12.app
- name: Carthage Bootstrap
run: carthage bootstrap --use-xcframeworks

- name: Setup Ruby
uses: actions/setup-ruby@v1
with:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
run: |
sudo xcode-select -switch /Applications/Xcode_12.app
- name: Carthage Bootstrap
run: carthage bootstrap --use-xcframeworks

- name: Setup Ruby
uses: actions/setup-ruby@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/Pods
/Build/
/DerivedData/
Carthage/Build
Carthage/

doc/
.swiftpm
Expand All @@ -28,4 +28,4 @@ DerivedData/
!default.perspectivev3

## Gcc Patch
/*.gcno
/*.gcno
4 changes: 4 additions & 0 deletions Amplitude.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.library = 'sqlite3.0'

s.swift_version = '4.1'

s.ios.deployment_target = '10.0'
s.ios.source_files = 'Sources/Amplitude/**/*.{h,m}'
s.ios.resources = 'Sources/Resources/*.{der}'
Expand All @@ -26,6 +28,8 @@ Pod::Spec.new do |s|
s.watchos.deployment_target = '3.0'
s.watchos.source_files = 'Sources/Amplitude/**/*.{h,m}'
s.watchos.resources = 'Sources/Resources/*.{der}'

s.dependency 'AnalyticsConnector', '~> 1.0.0'

s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
end
232 changes: 169 additions & 63 deletions Amplitude.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "amplitude/analytics-connector-ios" ~> 1.0.0
1 change: 1 addition & 0 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "amplitude/analytics-connector-ios" "v1.0.0"
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let package = Package(
products: [
.library(name: "Amplitude", targets: ["Amplitude"]),
],
dependencies: [
.package(url: "https://github.com/amplitude/amplitude-ios-core.git", from: "1.0.0")
],
targets: [
.target(
name: "Amplitude",
Expand All @@ -21,3 +24,4 @@ let package = Package(
publicHeadersPath: "Amplitude/Public"),
]
)

2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project 'Amplitude'
abstract_target 'shared' do

pod 'OCMock', '~> 3.2.1'

target 'Amplitude_iOSTests' do
platform :ios, '10.0'
end
Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ SPEC REPOS:
SPEC CHECKSUMS:
OCMock: 18c9b7e67d4c2770e95bb77a9cc1ae0c91fe3835

PODFILE CHECKSUM: c234af1832073dccffe31825daef96d6b0eb126c
PODFILE CHECKSUM: f4462aedc7398f89b3216737d86da7f673eb4649

COCOAPODS: 1.11.2
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ iOS/tvOS/macOS SDK for tracking events and revenue to [Amplitude](https://www.am
* A [demo application](https://github.com/amplitude/iOS-Extension-Demo) showing an integration in an iOS extension.
* A [demo application](https://github.com/amplitude/GTM-iOS-Demo) demonstrating a potential integration with Google Tag Manager.

## Developing

Prerequisites
* [Cocoapods](https://cocoapods.org/)
* [Carthage](https://github.com/Carthage/Carthage)

1. Install Dependencies via Carthage
```
carthage update --use-xcframeworks
```
2. Install Test Dependencies via CocoaPods
```
pod install
```
## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
Expand Down
26 changes: 26 additions & 0 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
#import <Cocoa/Cocoa.h>
#endif

@import AnalyticsConnector;

@interface Amplitude ()

@property (nonatomic, strong) NSOperationQueue *backgroundQueue;
Expand Down Expand Up @@ -221,6 +223,11 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName {
self.eventUploadPeriodSeconds = kAMPEventUploadPeriodSeconds;
self.minTimeBetweenSessionsMillis = kAMPMinTimeBetweenSessionsMillis;
_backoffUploadBatchSize = self.eventUploadMaxBatchSize;

// Set the event receiver to forward events generated by Experiment SDK
[[[AnalyticsConnector getInstance:self.instanceName] eventBridge] setEventReceiver:^(AnalyticsEvent * _Nonnull event) {
[self logEvent:[event eventType] withEventProperties:[event eventProperties] withApiProperties:nil withUserProperties:[event userProperties] withGroups:nil withGroupProperties:nil withTimestamp:nil outOfSession:false];
}];

_initializerQueue = [[NSOperationQueue alloc] init];
_backgroundQueue = [[NSOperationQueue alloc] init];
Expand Down Expand Up @@ -448,6 +455,10 @@ - (void)initializeApiKey:(NSString *)apiKey
} else {
self.userId = [self.dbHelper getValue:USER_ID];
}
// Set the user ID and device ID in the amplitude core instance. This is used to share user identity and user properties
// between Analytics and Experiment SDKs.
id<IdentityStoreEditor> identityStoreEditor = [[[AnalyticsConnector getInstance:self.instanceName] identityStore] editIdentity];
[[[identityStoreEditor setUserId:self.userId] setDeviceId:self.deviceId] commit];
if (self.initCompletionBlock != nil) {
self.initCompletionBlock();
}
Expand Down Expand Up @@ -626,6 +637,12 @@ - (void)logEvent:(NSString *)eventType withEventProperties:(NSDictionary *)event
} else {
(void) [self.dbHelper addEvent:jsonString];
}

// Apply identify events to amplitude core to notify experiment SDK that user properties have changed.
if ([eventType isEqualToString:IDENTIFY_EVENT]) {
id<IdentityStoreEditor> editor = [[[AnalyticsConnector getInstance:self.instanceName] identityStore] editIdentity];
[[editor updateUserProperties:[event valueForKey:@"user_properties"]] commit];
}

AMPLITUDE_LOG(@"Logged %@ Event", event[@"event_type"]);

Expand Down Expand Up @@ -1376,6 +1393,11 @@ - (void)setUserId:(NSString *)userId startNewSession:(BOOL)startNewSession {

self->_userId = userId;
[self.dbHelper insertOrReplaceKeyValue:USER_ID value:self.userId];

// Set the user ID amplitude core instance. This is used to share user identity
// between Analytics and Experiment SDKs.
id<IdentityStoreEditor> identityStoreEditor = [[[AnalyticsConnector getInstance:self.instanceName] identityStore] editIdentity];
[[identityStoreEditor setUserId:self.userId] commit];

if (startNewSession) {
NSNumber *timestamp = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000];
Expand Down Expand Up @@ -1444,6 +1466,10 @@ - (void)setDeviceId:(NSString *)deviceId {
[self runOnBackgroundQueue:^{
self->_deviceId = deviceId;
[self.dbHelper insertOrReplaceKeyValue:DEVICE_ID value:deviceId];
// Set the device ID in the amplitude core instance. This is used to share user identity
// between Analytics and Experiment SDKs.
id<IdentityStoreEditor> identityStoreEditor = [[[AnalyticsConnector getInstance:self.instanceName] identityStore] editIdentity];
[[identityStoreEditor setDeviceId:self.deviceId] commit];
}];
}

Expand Down

0 comments on commit 26e7830

Please sign in to comment.