Skip to content

Commit

Permalink
Merge pull request #78 from adjust/deeplink_attribution
Browse files Browse the repository at this point in the history
Deeplink attribution
  • Loading branch information
nonelse committed Feb 6, 2015
2 parents 24de701 + 2c1ae66 commit 05f8840
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Adjust.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = "Adjust"
s.version = "4.0.5"
s.version = "4.0.6"
s.summary = "This is the iOS SDK of adjust. You can read more about it at http://adjust.com."
s.homepage = "http://adjust.com"
s.license = { :type => 'MIT', :file => 'MIT-LICENSE' }
s.author = { "Christian Wellenbrock" => "[email protected]" }
s.source = { :git => "https://github.com/adjust/ios_sdk.git", :tag => "v4.0.5" }
s.source = { :git => "https://github.com/adjust/ios_sdk.git", :tag => "v4.0.6" }
s.platform = :ios, '4.3'
s.framework = 'SystemConfiguration'
s.weak_framework = 'AdSupport', 'iAd'
Expand Down
100 changes: 58 additions & 42 deletions Adjust/ADJActivityHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ - (void)trackEvent:(ADJEvent *)event

- (void)finishedTrackingWithResponse:(NSDictionary *)jsonDict{
[self launchDeepLink:jsonDict];
[self.attributionHandler checkAttribution:jsonDict];
[[self getAttributionHandler] checkAttribution:jsonDict];
}

- (void)launchDeepLink:(NSDictionary *)jsonDict{
Expand Down Expand Up @@ -205,15 +205,14 @@ - (void)launchAttributionDelegate{
}

- (void)setOfflineMode:(BOOL)isOffline {
self.offline = isOffline;
if (isOffline) {
self.offline = YES;
[self endInternal];
[self.logger info:@"Pausing package handler to put in offline mode"];
[self endInternal];
} else {
self.offline = NO;
[self.logger info:@"Resuming package handler to put in online mode"];
[self.packageHandler resumeSending];
[self startTimer];
[self.logger info:@"Resuming package handler to put in online mode"];
}
}

Expand All @@ -240,30 +239,31 @@ - (void)initInternal {
[self.logger info:@"Event buffering is enabled"];
}

[[UIDevice currentDevice] adjSetIad:self];

[self readAttribution];
[self readActivityState];

self.packageHandler = [ADJAdjustFactory packageHandlerForActivityHandler:self];

self.attributionHandler = [self buildAttributionHandler];

self.shouldGetAttribution = YES;

[[UIDevice currentDevice] adjSetIad:self];

[self startInternal];
}

- (id<ADJAttributionHandler>) buildAttributionHandler {
ADJPackageBuilder *attributionBuilder = [[ADJPackageBuilder alloc] initWithDeviceInfo:self.deviceInfo
activityState:self.activityState
config:self.adjustConfig];
ADJActivityPackage *attributionPackage = [attributionBuilder buildAttributionPackage];
id<ADJAttributionHandler> attributionHandler = [ADJAdjustFactory attributionHandlerForActivityHandler:self
withMaxDelay:nil
withAttributionPackage:attributionPackage];
- (id<ADJAttributionHandler>) getAttributionHandler {
//TODO self.activity state can be null in the first session
if (self.attributionHandler == nil) {
ADJPackageBuilder *attributionBuilder = [[ADJPackageBuilder alloc] initWithDeviceInfo:self.deviceInfo
activityState:self.activityState
config:self.adjustConfig];
ADJActivityPackage *attributionPackage = [attributionBuilder buildAttributionPackage];
self.attributionHandler = [ADJAdjustFactory attributionHandlerForActivityHandler:self
withMaxDelay:nil
withAttributionPackage:attributionPackage];
}

return attributionHandler;
return self.attributionHandler;
}

- (void)startInternal {
Expand Down Expand Up @@ -325,7 +325,7 @@ - (void)startInternal {

if (self.attribution == nil || self.activityState.askingAttribution) {
if (self.shouldGetAttribution) {
[self.attributionHandler getAttribution];
[[self getAttributionHandler] getAttribution];
}
}
}
Expand Down Expand Up @@ -373,65 +373,81 @@ - (void)eventInternal:(ADJEvent *)event

- (void) appWillOpenUrlInternal:(NSURL *)url {
NSArray* queryArray = [url.query componentsSeparatedByString:@"&"];
if (queryArray == nil) {
return;
}

NSMutableDictionary* adjustDeepLinks = [NSMutableDictionary dictionary];
ADJAttribution *attribution = [[ADJAttribution alloc] init];
ADJAttribution *deeplinkAttribution = [[ADJAttribution alloc] init];
BOOL hasDeepLink = NO;

for (NSString* fieldValuePair in queryArray) {
NSArray* pairComponents = [fieldValuePair componentsSeparatedByString:@"="];
if (pairComponents.count != 2) continue;

NSString* key = [pairComponents objectAtIndex:0];
if (![key hasPrefix:kAdjustPrefix]) continue;

NSString* value = [pairComponents objectAtIndex:1];
if (value.length == 0) continue;

NSString* keyWOutPrefix = [key substringFromIndex:kAdjustPrefix.length];
if (keyWOutPrefix.length == 0) continue;

if (![self trySetAttributionDeeplink:attribution withKey:keyWOutPrefix withValue:value]) {
[adjustDeepLinks setObject:value forKey:keyWOutPrefix];
if([self readDeeplinkQueryString:fieldValuePair adjustDeepLinks:adjustDeepLinks attribution:deeplinkAttribution]) {
hasDeepLink = YES;
}
}

if ([adjustDeepLinks count] == 0) {
if (!hasDeepLink) {
return;
}

[self.attributionHandler getAttribution];
[[self getAttributionHandler] getAttribution];

ADJPackageBuilder *clickBuilder = [[ADJPackageBuilder alloc] initWithDeviceInfo:self.deviceInfo
activityState:self.activityState
config:self.adjustConfig];
clickBuilder.deeplinkParameters = adjustDeepLinks;
clickBuilder.attribution = attribution;
clickBuilder.attribution = deeplinkAttribution;
[clickBuilder setClickTime:[NSDate date]];

ADJActivityPackage *clickPackage = [clickBuilder buildClickPackage:@"deeplink"];
[self.packageHandler sendClickPackage:clickPackage];
}

- (BOOL) trySetAttributionDeeplink:(ADJAttribution *)attribution
- (BOOL) readDeeplinkQueryString:(NSString *)queryString
adjustDeepLinks:(NSMutableDictionary*)adjustDeepLinks
attribution:(ADJAttribution *)deeplinkAttribution
{
NSArray* pairComponents = [queryString componentsSeparatedByString:@"="];
if (pairComponents.count != 2) return NO;

NSString* key = [pairComponents objectAtIndex:0];
if (![key hasPrefix:kAdjustPrefix]) return NO;

NSString* value = [pairComponents objectAtIndex:1];
if (value.length == 0) return NO;

NSString* keyWOutPrefix = [key substringFromIndex:kAdjustPrefix.length];
if (keyWOutPrefix.length == 0) return NO;

if (![self trySetAttributionDeeplink:deeplinkAttribution withKey:keyWOutPrefix withValue:value]) {
[adjustDeepLinks setObject:value forKey:keyWOutPrefix];
}

return YES;
}

- (BOOL) trySetAttributionDeeplink:(ADJAttribution *)deeplinkAttribution
withKey:(NSString *)key
withValue:(NSString*)value {

if ([key isEqualToString:@"tracker"]) {
attribution.trackerName = value;
deeplinkAttribution.trackerName = value;
return YES;
}

if ([key isEqualToString:@"campaign"]) {
attribution.campaign = value;
deeplinkAttribution.campaign = value;
return YES;
}

if ([key isEqualToString:@"adgroup"]) {
attribution.adgroup = value;
deeplinkAttribution.adgroup = value;
return YES;
}

if ([key isEqualToString:@"creative"]) {
attribution.creative = value;
deeplinkAttribution.creative = value;
return YES;
}

Expand Down
2 changes: 1 addition & 1 deletion Adjust/ADJPackageBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ - (ADJActivityPackage *)buildAttributionPackage {
[self parameters:parameters setString:self.adjustConfig.appToken forKey:@"app_token"];
[self parameters:parameters setString:self.adjustConfig.environment forKey:@"environment"];
[self parameters:parameters setString:self.activityState.uuid forKey:@"ios_uuid"];
[self parameters:parameters setBool:self.adjustConfig.hasDelegate forKey:@"needs_attribution_data"];
[self parameters:parameters setBool:self.adjustConfig.hasDelegate forKey:@"needs_attribution_data"];

ADJActivityPackage *attributionPackage = [self defaultActivityPackage];
attributionPackage.path = @"/attribution";
Expand Down
2 changes: 1 addition & 1 deletion Adjust/ADJUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <sys/xattr.h>

static NSString * const kBaseUrl = @"https://app.adjust.com";
static NSString * const kClientSdk = @"ios4.0.5";
static NSString * const kClientSdk = @"ios4.0.6";

static NSString * const kDateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'Z";
static NSDateFormatter *dateFormat;
Expand Down
6 changes: 3 additions & 3 deletions AdjustTests/ADJActivityHandlerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ - (void)testFirstRun
// Handler initializations
XCTAssert([self.loggerMock containsMessage:ADJLogLevelTest beginsWith:@"ADJPackageHandler initWithActivityHandler"],
@"%@", self.loggerMock);
XCTAssert([self.loggerMock containsMessage:ADJLogLevelTest beginsWith:@"ADJAttributionHandlerMock initWithActivityHandler"],
@"%@", self.loggerMock);

// when a session package is being sent the package handler should resume sending
XCTAssert([self.loggerMock containsMessage:ADJLogLevelTest beginsWith:@"ADJPackageHandler resumeSending"],
Expand All @@ -123,7 +121,7 @@ - (void)testFirstRun
ADJActivityPackage *activityPackage = (ADJActivityPackage *) self.packageHandlerMock.packageQueue[0];

// check the Sdk version is being tested
XCTAssertEqual(@"ios4.0.5", activityPackage.clientSdk, @"%@", activityPackage.extendedString);
XCTAssertEqual(@"ios4.0.6", activityPackage.clientSdk, @"%@", activityPackage.extendedString);

// check the server url
XCTAssertEqual(@"https://app.adjust.com", ADJUtil.baseUrl);
Expand Down Expand Up @@ -210,6 +208,8 @@ - (void)testFirstRun
@"%@", self.loggerMock);

// check that the package handler calls back with the json dict response
XCTAssert([self.loggerMock containsMessage:ADJLogLevelTest beginsWith:@"ADJAttributionHandlerMock initWithActivityHandler"],
@"%@", self.loggerMock);
XCTAssert([self.loggerMock containsMessage:ADJLogLevelTest beginsWith:@"ADJAttributionHandlerMock checkAttribution"],
@"%@", self.loggerMock);

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.5
4.0.6
2 changes: 1 addition & 1 deletion doc/migrate.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Migrate your adjust SDK for iOS to v4.0.5 from v3.4.0
## Migrate your adjust SDK for iOS to v4.0.6 from v3.4.0

### Initial setup

Expand Down
7 changes: 7 additions & 0 deletions example/example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[Adjust appWillOpenUrl:url];

return YES;
}

- (void)adjustAttributionChanged:(ADJAttribution *)attribution {
NSLog(@"adjust attribution %@", attribution);
}
Expand Down
11 changes: 11 additions & 0 deletions example/example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>adjustExample</string>
</array>
<key>CFBundleURLName</key>
<string>com.adjust.example</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down

0 comments on commit 05f8840

Please sign in to comment.