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

Add enableAutoTrackingIP & disableAutoTrackingIP #131

Merged
merged 6 commits into from
Jun 25, 2024
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: 7 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ commands:
jobs:
test_ios17:
macos:
xcode: "15.1.0"
resource_class: macos.m1.large.gen1
xcode: "15.4.0"
resource_class: macos.m1.medium.gen1
steps:
- run_tests_flow:
os: "17.2"
os: "17.5"
simulator: "iPhone 15"

test_ios16:
macos:
xcode: "14.3.1"
xcode: "15.4.0"
resource_class: macos.m1.medium.gen1
steps:
- run_tests_flow:
Expand All @@ -126,7 +126,8 @@ jobs:

test_ios15:
macos:
xcode: "14.1.0"
xcode: "14.3.1"
resource_class: macos.m1.medium.gen1
steps:
- run_tests_flow:
os: "15.5"
Expand All @@ -135,19 +136,12 @@ jobs:
test_ios14:
macos:
xcode: "13.4.1"
resource_class: macos.m1.medium.gen1
steps:
- run_tests_flow:
os: "14.5"
simulator: "iPhone 12"

test_ios13:
macos:
xcode: "12.5.1"
steps:
- run_tests_flow:
os: "13.7"
simulator: "iPhone 11"

build_spm_ios15:
macos:
xcode: "13.4.1"
Expand All @@ -164,6 +158,5 @@ workflows:
- test_ios16
- test_ios15
- test_ios14
- test_ios13
- build_spm_ios15

7 changes: 7 additions & 0 deletions TreasureData/TDClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
*/
@property(nonatomic, strong) NSString *apiEndpoint;

#pragma mark - Tracking

/**
* Enable tracking td_ip
*/
@property BOOL enableTrackingIP;

#pragma mark - Retry

/**
Expand Down
6 changes: 4 additions & 2 deletions TreasureData/TDClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (id)__initWithApiKey:(NSString *)apiKey apiEndpoint:(NSString*)apiEndpoint {
self.uploadRetryIntervalBase = 2;
self.uploadRetryCount = 5;
self.enableRetryUploading = true;
self.enableTrackingIP = false;
_session = [NSURLSession sharedSession];
return self;
}
Expand Down Expand Up @@ -72,8 +73,9 @@ - (void)sendEvents:(NSData *)data database:(NSString *)database table:(NSString
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat: @"TD1 %@", self.apiKey] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/vnd.treasuredata.v1+json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/vnd.treasuredata.v1+json" forHTTPHeaderField:@"Accept"];
NSString *contentType = self.enableTrackingIP ? @"application/vnd.treasuredata.v1.mobile+json" : @"application/vnd.treasuredata.v1+json";
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[request setValue:contentType forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"TD-iOS-SDK/%@ (%@ %@)", version, [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion]] forHTTPHeaderField:@"User-Agent"];

if (_enableEventCompression) {
Expand Down
10 changes: 10 additions & 0 deletions TreasureData/TreasureData.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ typedef void (^ErrorHandler)(NSString* _Nonnull errorCode, NSString* _Nullable e
*/
- (void)disableAutoAppendAdvertisingIdentifier;

/**
* Automatically tracking device's IP address in `td_ip` column. Disabled by default
*/
- (void)enableAutoTrackingIP;

/**
* Disable automatically tracking device's IP
*/
- (void)disableAutoTrackingIP;

#pragma mark - Session

/**
Expand Down
8 changes: 8 additions & 0 deletions TreasureData/TreasureData.m
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,14 @@ - (void)disableAutoAppendAdvertisingIdentifier {
self.autoAppendAdvertisingIdColumn = nil;
}

- (void)enableAutoTrackingIP {
self.client.enableTrackingIP = true;
}

- (void)disableAutoTrackingIP {
self.client.enableTrackingIP = false;
}

+ (instancetype)sharedInstance {
NSAssert(sharedInstance, @"%@ sharedInstance is called before [TreasureData initializeWithApiKey:]", self);
return sharedInstance;
Expand Down
20 changes: 20 additions & 0 deletions TreasureDataTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
import Network

class IntegrationTests: XCTestCase {

Expand Down Expand Up @@ -172,6 +173,20 @@ class IntegrationTests: XCTestCase {
XCTAssert(result[0][0] as! String != result[1][0] as! String)
}

func testTrackingIP() {
let table = newTempTable()
sdkClient.enableAutoTrackingIP()
sdkClient.addEvent([:], table: table)
sdkClient.uploadEvents()
sleep(2)
sdkClient.disableAutoTrackingIP()
sdkClient.addEvent([:], table: table)
sdkClient.uploadEvents()
let result = try! IntegrationTests.api.stubbornQuery("select td_ip from \(table) limit 2", database: IntegrationTests.TargetDatabase)
XCTAssert(IntegrationTests.isValidIPAddress(address: result[0][0] as? String))
XCTAssert(result.count == 1) // count == 1 means there is only the first event with td_ip
}

func testFetchUserSegmentsSucceed() {
let expectation = self.expectation(description: "fetchUserSegments should succeed")
sdkClient.fetchUserSegments(tokens: IntegrationTests.audienceTokens, keys: IntegrationTests.userSegmentKeys) { (jsonResponse, error) in
Expand Down Expand Up @@ -201,4 +216,9 @@ class IntegrationTests: XCTestCase {
static func uuid() -> String {
return NSUUID().uuidString.lowercased().replacingOccurrences(of: "-", with: "_")
}

static func isValidIPAddress(address: String?) -> Bool {
guard let address = address else { return false }
return IPv4Address(address) != nil || IPv6Address(address) != nil
}
}
2 changes: 1 addition & 1 deletion TreasureDataTests/TDAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class TDAPI {
*
* - Parameter timeout: timeout in seconds, default is 30 minutes
*/
func stubbornQuery(_ query: String, database: String, timeout: TimeInterval = 30 * 60) throws -> [[Any]] {
func stubbornQuery(_ query: String, database: String, timeout: TimeInterval = 15 * 60) throws -> [[Any]] {
let started = Date()

func doRetry(_ query: String, database: String) throws -> [[Any]] {
Expand Down
23 changes: 23 additions & 0 deletions TreasureDataTests/TreasureDataTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,29 @@ - (void)testTrackIAPEvent {
}
}

#pragma mark - Tracking td_ip

- (void)testTrackingIPEnabled {
@try {
XCTAssertFalse(self.td.client.enableTrackingIP);
[self.td enableAutoTrackingIP];
XCTAssertTrue(self.td.client.enableTrackingIP);
} @finally {
self.isFinished = YES;
}
}

- (void)testTrackingIPDisabled {
@try {
[self.td enableAutoTrackingIP];
XCTAssertTrue(self.td.client.enableTrackingIP);
[self.td disableAutoTrackingIP];
XCTAssertFalse(self.td.client.enableTrackingIP);
} @finally {
self.isFinished = YES;
}
}

#pragma mark - Default Values

- (void) testAddDefaultValuesSuccesfully {
Expand Down
Loading