Skip to content

Commit

Permalink
Add dynamic config feature and adjust default endpoint to api2 (#247)
Browse files Browse the repository at this point in the history
* Add dynamic config feature and adjust default endpoint to api2

* Revert change on test file

* Minor format change
  • Loading branch information
haoliu-amp authored Aug 11, 2020
1 parent e0be8af commit 9af032b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/main/java/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public class AmplitudeClient {
private boolean flushEventsOnClose = true;
private String libraryName = Constants.LIBRARY;
private String libraryVersion = Constants.VERSION;
private boolean useDynamicConfig = false;

private AtomicBoolean updateScheduled = new AtomicBoolean(false);
/**
Expand Down Expand Up @@ -269,6 +270,15 @@ public synchronized AmplitudeClient initialize(final Context context, final Stri
AmplitudeClient.upgradePrefs(context);
AmplitudeClient.upgradeSharedPrefsToDB(context);
}

if (useDynamicConfig) {
ConfigManager.getInstance().refresh(new ConfigManager.RefreshListener() {
@Override
public void onFinished() {
url = ConfigManager.getInstance().getIngestionEndpoint();
}
});
}
httpClient = new OkHttpClient();
deviceInfo = new DeviceInfo(context, this.locationListening);
deviceId = initializeDeviceId();
Expand Down Expand Up @@ -671,6 +681,19 @@ public AmplitudeClient trackSessionEvents(boolean trackingSessionEvents) {
return this;
}

/**
* Turning this flag on will find the best server url automatically based on users' geo location.
* Note:
* 1. If you have your own proxy server and use `setServerUrl` API, please leave this off.
* 2. If you have users in China Mainland, we suggest you turn this on.
*
* @param useDynamicConfig whether to enable dynamic config
* @return the AmplitudeClient
*/
public AmplitudeClient setUseDynamicConfig(boolean useDynamicConfig) {
this.useDynamicConfig = useDynamicConfig;
return this;
}
/**
* Set foreground tracking to true.
*/
Expand Down Expand Up @@ -1329,6 +1352,14 @@ public void run() {
if (Utils.isEmptyString(apiKey)) {
return;
}
if (useDynamicConfig) {
ConfigManager.getInstance().refresh(new ConfigManager.RefreshListener() {
@Override
public void onFinished() {
url = ConfigManager.getInstance().getIngestionEndpoint();
}
});
}
startNewSessionIfNeeded(timestamp);
inForeground = true;
}
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/amplitude/api/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.amplitude.api;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ConfigManager {
private static String KEY_INGESTION_ENDPOINT = "ingestionEndpoint";

private static ConfigManager instance = null;

private String ingestionEndpoint = Constants.EVENT_LOG_URL;

public String getIngestionEndpoint() {
return ingestionEndpoint;
}

private ConfigManager() {
}

public void refresh(RefreshListener listener) {
try {
URL obj = new URL(Constants.DYNAMIC_CONFIG_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

int responseCode = con.getResponseCode();

if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

JSONObject json = new JSONObject(response.toString());
if (json.has(KEY_INGESTION_ENDPOINT)) {
this.ingestionEndpoint = "https://" + json.getString(KEY_INGESTION_ENDPOINT);
}
}
} catch (MalformedURLException e) {

} catch (IOException e) {

} catch (JSONException e) {

}

listener.onFinished();
}

public static ConfigManager getInstance() {
if (instance == null) {
instance = new ConfigManager();
}

return instance;
}

interface RefreshListener {
void onFinished();
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/amplitude/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Constants {
public static final String VERSION_UNKNOWN = "unknown-version";
public static final String PLATFORM = "Android";

public static final String EVENT_LOG_URL = "https://api.amplitude.com/";
public static final String EVENT_LOG_URL = "https://api2.amplitude.com/";
public static final String DYNAMIC_CONFIG_URL = "https://regionconfig.amplitude.com/";

public static final String PACKAGE_NAME = "com.amplitude.api";

Expand Down

0 comments on commit 9af032b

Please sign in to comment.