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

Makes IDFA support externally/customer driven. #892

Merged
merged 5 commits into from
May 21, 2020
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
21 changes: 4 additions & 17 deletions Analytics/Classes/Internal/SEGAnalyticsUtils.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "SEGAnalyticsUtils.h"
#import <AdSupport/ASIdentifierManager.h>
#import "SEGAnalytics.h"

static BOOL kAnalyticsLoggerShowLogs = NO;

Expand Down Expand Up @@ -217,23 +217,10 @@ static id SEGCoerceJSONObject(id obj)

NSString *SEGIDFA()
{
NSString *idForAdvertiser = nil;
Class identifierManager = NSClassFromString(@"ASIdentifierManager");
if (identifierManager) {
SEL sharedManagerSelector = NSSelectorFromString(@"sharedManager");
id sharedManager =
((id (*)(id, SEL))
[identifierManager methodForSelector:sharedManagerSelector])(
identifierManager, sharedManagerSelector);
SEL advertisingIdentifierSelector =
NSSelectorFromString(@"advertisingIdentifier");
NSUUID *uuid =
((NSUUID * (*)(id, SEL))
[sharedManager methodForSelector:advertisingIdentifierSelector])(
sharedManager, advertisingIdentifierSelector);
idForAdvertiser = [uuid UUIDString];
if ([SEGAnalytics sharedAnalytics].configuration.adSupportBlock != nil) {
return [SEGAnalytics sharedAnalytics].configuration.adSupportBlock();
}
return idForAdvertiser;
return nil;
}

NSString *SEGEventNameForScreenTitle(NSString *title)
Expand Down
2 changes: 1 addition & 1 deletion Analytics/Classes/Internal/SEGSegmentIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ - (NSDictionary *)staticContext
if (NSClassFromString(SEGAdvertisingClassIdentifier)) {
dict[@"adTrackingEnabled"] = @(GetAdTrackingEnabled());
}
if (self.configuration.enableAdvertisingTracking) {
if (self.configuration.enableAdvertisingTracking && self.configuration.adSupportBlock != nil) {
NSString *idfa = SEGIDFA();
if (idfa.length) dict[@"advertisingId"] = idfa;
}
Expand Down
13 changes: 13 additions & 0 deletions Analytics/Classes/SEGAnalyticsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@end

typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull);
typedef NSString *_Nonnull (^SEGAdSupportBlock)(void);

@protocol SEGIntegrationFactory;
@protocol SEGCrypto;
Expand Down Expand Up @@ -192,6 +193,18 @@ typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull);
*/
@property (nonatomic, strong, nullable) id<NSURLSessionDelegate> httpSessionDelegate;

/**
* Sets a block to be called when IDFA / AdSupport identifier is created.
* This is to allow for apps that do not want ad tracking to pass App Store guidelines in certain categories while
* still allowing apps that do ad tracking to continue to function.
*
* Example:
* configuration.adSupportBlock = ^{
* return [[ASIdentifierManager sharedManager] advertisingIdentifier];
* }
*/
@property (nonatomic, strong, nullable) SEGAdSupportBlock adSupportBlock;

/**
Enable experimental features within the Segment Analytics-iOS library.
*/
Expand Down
14 changes: 14 additions & 0 deletions AnalyticsTests/AnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ class AnalyticsTests: QuickSpec {
expect(UserDefaults.standard.string(forKey: "SEGQueue")).toEventually(beNil())
}

/* TODO: Fix me when the Context object isn't so wild.
it("collects IDFA") {
testMiddleware.swallowEvent = true
analytics.configuration.enableAdvertisingTracking = true
analytics.configuration.adSupportBlock = { () -> String in
return "1234AdsNoMore!"
}

analytics.track("test");

let event = testMiddleware.lastContext?.payload as? SEGTrackPayload
expect(event?.properties?["url"] as? String) == "myapp://auth?token=((redacted/my-auth))&other=stuff"
}*/

it("persists anonymousId") {
let analytics2 = SEGAnalytics(configuration: config)
expect(analytics.getAnonymousId()) == analytics2.getAnonymousId()
Expand Down