Skip to content

Commit

Permalink
Auto tracking IP (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
tung-vu-td authored Jun 25, 2024
1 parent 30bfbf5 commit 2f6463c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repositories {
dependencies {
implementation 'org.komamitsu:android-logger-bridge:0.0.2'
implementation 'com.fasterxml.jackson.jr:jackson-jr-objects:2.17.0'
implementation 'com.treasuredata:keen-client-java-core:3.0.0'
implementation 'com.treasuredata:keen-client-java-core:3.0.1'

compileOnly 'com.google.android:android:4.1.1.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.treasuredata.android.TreasureData;

import java.util.HashMap;
import java.util.Map;

/**
* Created by vinhvd on 2/6/18.
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/treasuredata/android/TDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TDClient extends KeenClient {
.withJsonHandler(new TDJsonHandler(encryptionKey))
.withPublishExecutor(Executors.newSingleThreadExecutor())
);

// setDebugMode(true);
setApiKey(apiKey);
setActive(true);
Expand Down Expand Up @@ -56,7 +57,10 @@ public void enableAutoRetryUploading() {
// Only for test
@Deprecated
TDClient(String apiKey) {
super(new TDClientBuilder());
super(
new TDClientBuilder()
.withHttpHandler(new TDHttpHandler(apiKey, "test-endpoint"))
);
setApiKey(apiKey);
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/treasuredata/android/TDHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TDHttpHandler extends UrlConnectionHttpHandler {
private final String apiKey;
private final String apiEndpoint;

volatile boolean isTrackingIPEnabled = false;

public static void disableEventCompression() {
isEventCompression = false;
}
Expand All @@ -36,10 +38,11 @@ public TDHttpHandler(String apiKey, String apiEndpoint) {
}

protected void sendRequest(HttpURLConnection connection, Request request) throws IOException {
String contentType = isTrackingIPEnabled ? "application/vnd.treasuredata.v1.mobile+json" : "application/vnd.treasuredata.v1+json";
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "TD1 " + apiKey);
connection.setRequestProperty("Content-Type", "application/vnd.treasuredata.v1+json");
connection.setRequestProperty("Accept", "application/vnd.treasuredata.v1+json");
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Accept", contentType);
connection.setRequestProperty("User-Agent", String.format("TD-Android-SDK/%s (%s %s)", VERSION, Build.MODEL, Build.VERSION.RELEASE));
connection.setDoOutput(true);

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/treasuredata/android/TreasureData.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,23 @@ public static void disableEventCompression() {
TDHttpHandler.disableEventCompression();
}

/**
* Device's IP will be added to td_ip column in Treasure Data backend.
*/
public void enableAutoTrackingIP() {
TDHttpHandler tdHttpHandler = (TDHttpHandler) client.getHttpHandler();
tdHttpHandler.isTrackingIPEnabled = true;
}

/**
* Device's IP will not be added to td_ip column in Treasure Data backend.
* This is the default behavior.
*/
public void disableAutoTrackingIP() {
TDHttpHandler tdHttpHandler = (TDHttpHandler) client.getHttpHandler();
tdHttpHandler.isTrackingIPEnabled = false;
}

/**
* The destination database for events that doesn't specify one, default is "td".
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/treasuredata/android/TreasureDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.keen.client.java.KeenCallback;
import io.keen.client.java.KeenClient;
import io.keen.client.java.KeenProject;

import junit.framework.TestCase;

import java.io.IOException;
Expand Down Expand Up @@ -963,4 +964,16 @@ public void testRemoveDefaultValuesNoop() {
assertEquals(event1.get("key"), "Value");
assertNull(event1.get("key2"));
}

public void testAutoTrackingIP() {
TDHttpHandler tdHttpHandler = (TDHttpHandler) client.getHttpHandler();

assertFalse(tdHttpHandler.isTrackingIPEnabled);

td.enableAutoTrackingIP();
assertTrue(tdHttpHandler.isTrackingIPEnabled);

td.disableAutoTrackingIP();
assertFalse(tdHttpHandler.isTrackingIPEnabled);
}
}

0 comments on commit 2f6463c

Please sign in to comment.