Skip to content

Commit

Permalink
Add sdkIdentifier + sdkversion (#574)
Browse files Browse the repository at this point in the history
* Add sdkIdentifier + sdkversion

- add parameters to initialization call
- add fields to MapboxEvent
- set sdkIdentifier and sdkVersion when pushing turnstile event
- write fallback method to preserve backwards compatability with previous versions
  • Loading branch information
electrostat authored Sep 27, 2017
1 parent a58b53b commit f1cbd9b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class MapboxEvent implements Serializable {
public static final String KEY_ALTITUDE = "altitude";
public static final String KEY_APPLICATION_STATE = "applicationState";
public static final String KEY_HORIZONTAL_ACCURACY = "horizontalAccuracy";
public static final String KEY_SDK_IDENTIFIER = "sdkIdentifier";
public static final String KEY_SDK_VERSION = "sdkVersion";

// Gestures
public static final String GESTURE_SINGLETAP = "SingleTap";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.mapbox.services.android.telemetry.service.TelemetryService;
import com.mapbox.services.android.telemetry.utils.TelemetryUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
Expand Down Expand Up @@ -77,6 +79,8 @@ public class MapboxTelemetry implements Callback, LocationEngineListener {
private Hashtable<String, Object> customTurnstileEvent = null;
private int sessionIdRotationTime = TelemetryConstants.DEFAULT_SESSION_ID_ROTATION_HOURS;
private boolean debugLoggingEnabled = false;
private String sdkIdentifier = "";
private String sdkVersion = "";
private static final List<String> VALID_USER_AGENTS = new ArrayList<String>() {
{
add("MapboxEventsAndroid/");
Expand Down Expand Up @@ -111,11 +115,31 @@ public static synchronized MapboxTelemetry getInstance() {
*
* @param context The context associated with the application
* @param accessToken The accessToken associated with the application
* @param userAgent source of requests
* @param locationEngine Initialize telemetry with a custom location engine
*/
public void initialize(@NonNull Context context, @NonNull String accessToken,
@NonNull String userAgent, @NonNull LocationEngine locationEngine) {
public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent,
@NonNull LocationEngine locationEngine) {
this.locationEngine = locationEngine;

initialize(context, accessToken, userAgent);
}

/**
* Initialize MapboxTelemetry - with sdkIdentifier + sdkVersion
*
* @param context The context associated with the application
* @param accessToken The accessToken associated with the application
* @param userAgent source of requests
* @param sdkIdentifier Identifies which sdk is sending the event
* @param sdkVersion version of the sdk sending the event
*/

public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent,
@NonNull String sdkIdentifier, @NonNull String sdkVersion) {
this.sdkIdentifier = sdkIdentifier;
this.sdkVersion = sdkVersion;

initialize(context, accessToken, userAgent);
}

Expand All @@ -124,6 +148,7 @@ public void initialize(@NonNull Context context, @NonNull String accessToken,
*
* @param context The context associated with the application
* @param accessToken The accessToken associated with the application
* @param userAgent source of requests
*/
public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent) {
if (initialized) {
Expand All @@ -134,9 +159,11 @@ public void initialize(@NonNull Context context, @NonNull String accessToken, @N
this.context = context.getApplicationContext();
this.accessToken = accessToken;
this.userAgent = userAgent;
if (this.context == null || TextUtils.isEmpty(this.accessToken) || TextUtils.isEmpty(this.userAgent)) {

if (this.context == null || TextUtils.isEmpty(this.accessToken) || TextUtils.isEmpty(this.userAgent)
|| this.sdkIdentifier == null || this.sdkVersion == null) {
throw new TelemetryException(
"Please, make sure you provide a valid context, access token, and user agent. "
"Please, make sure you provide a valid context, access token, user agent, sdkIdentifier and sdkVersion. "
+ "For more information, please visit https://www.mapbox.com/android-sdk.");
}

Expand Down Expand Up @@ -651,6 +678,8 @@ private void pushTurnstileEvent() {
event.put(MapboxEvent.KEY_CREATED, TelemetryUtils.generateCreateDate(null));
event.put(MapboxEvent.KEY_USER_ID, mapboxVendorId);
event.put(MapboxEvent.KEY_ENABLED_TELEMETRY, isTelemetryEnabled());
event.put(MapboxEvent.KEY_SDK_IDENTIFIER, addSdkIdentifier());
event.put(MapboxEvent.KEY_SDK_VERSION, addsdkVersion());

events.add(event);
flushEventsQueueImmediately(true);
Expand Down Expand Up @@ -740,4 +769,26 @@ public void setAccessToken(@NonNull String accessToken) {
this.accessToken = accessToken;
client.setAccessToken(accessToken);
}

/**
* Check for empty strings and returns desired sdkIdentifier input for event
*/
private Object addSdkIdentifier() {
if (sdkIdentifier.isEmpty()) {
return JSONObject.NULL;
}

return sdkIdentifier;
}

/**
* Check for empty strings and returns desired sdkVersion input for event
*/
private Object addsdkVersion() {
if (sdkVersion.isEmpty()) {
return JSONObject.NULL;
}

return sdkVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ private void sendEventsWrapped(Vector<Hashtable<String, Object>> events, Callbac
jsonObject.putOpt(MapboxEvent.KEY_SOURCE, evt.get(MapboxEvent.KEY_SOURCE));
jsonObject.putOpt(MapboxEvent.KEY_SESSION_ID, evt.get(MapboxEvent.KEY_SESSION_ID));
jsonObject.putOpt(MapboxEvent.KEY_LATITUDE, evt.get(MapboxEvent.KEY_LATITUDE));
jsonObject.putOpt(MapboxEvent.KEY_SDK_IDENTIFIER, evt.get(MapboxEvent.KEY_SDK_IDENTIFIER));
jsonObject.putOpt(MapboxEvent.KEY_SDK_VERSION, evt.get(MapboxEvent.KEY_SDK_VERSION));

// Make sure longitude is wrapped
if (evt.containsKey(MapboxEvent.KEY_LONGITUDE)) {
Expand Down

0 comments on commit f1cbd9b

Please sign in to comment.