Skip to content

Commit f1cbd9b

Browse files
authored
Add sdkIdentifier + sdkversion (#574)
* 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
1 parent a58b53b commit f1cbd9b

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class MapboxEvent implements Serializable {
4949
public static final String KEY_ALTITUDE = "altitude";
5050
public static final String KEY_APPLICATION_STATE = "applicationState";
5151
public static final String KEY_HORIZONTAL_ACCURACY = "horizontalAccuracy";
52+
public static final String KEY_SDK_IDENTIFIER = "sdkIdentifier";
53+
public static final String KEY_SDK_VERSION = "sdkVersion";
5254

5355
// Gestures
5456
public static final String GESTURE_SINGLETAP = "SingleTap";

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.mapbox.services.android.telemetry.service.TelemetryService;
3434
import com.mapbox.services.android.telemetry.utils.TelemetryUtils;
3535

36+
import org.json.JSONObject;
37+
3638
import java.io.IOException;
3739
import java.math.BigDecimal;
3840
import java.util.ArrayList;
@@ -77,6 +79,8 @@ public class MapboxTelemetry implements Callback, LocationEngineListener {
7779
private Hashtable<String, Object> customTurnstileEvent = null;
7880
private int sessionIdRotationTime = TelemetryConstants.DEFAULT_SESSION_ID_ROTATION_HOURS;
7981
private boolean debugLoggingEnabled = false;
82+
private String sdkIdentifier = "";
83+
private String sdkVersion = "";
8084
private static final List<String> VALID_USER_AGENTS = new ArrayList<String>() {
8185
{
8286
add("MapboxEventsAndroid/");
@@ -111,11 +115,31 @@ public static synchronized MapboxTelemetry getInstance() {
111115
*
112116
* @param context The context associated with the application
113117
* @param accessToken The accessToken associated with the application
118+
* @param userAgent source of requests
114119
* @param locationEngine Initialize telemetry with a custom location engine
115120
*/
116-
public void initialize(@NonNull Context context, @NonNull String accessToken,
117-
@NonNull String userAgent, @NonNull LocationEngine locationEngine) {
121+
public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent,
122+
@NonNull LocationEngine locationEngine) {
118123
this.locationEngine = locationEngine;
124+
125+
initialize(context, accessToken, userAgent);
126+
}
127+
128+
/**
129+
* Initialize MapboxTelemetry - with sdkIdentifier + sdkVersion
130+
*
131+
* @param context The context associated with the application
132+
* @param accessToken The accessToken associated with the application
133+
* @param userAgent source of requests
134+
* @param sdkIdentifier Identifies which sdk is sending the event
135+
* @param sdkVersion version of the sdk sending the event
136+
*/
137+
138+
public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent,
139+
@NonNull String sdkIdentifier, @NonNull String sdkVersion) {
140+
this.sdkIdentifier = sdkIdentifier;
141+
this.sdkVersion = sdkVersion;
142+
119143
initialize(context, accessToken, userAgent);
120144
}
121145

@@ -124,6 +148,7 @@ public void initialize(@NonNull Context context, @NonNull String accessToken,
124148
*
125149
* @param context The context associated with the application
126150
* @param accessToken The accessToken associated with the application
151+
* @param userAgent source of requests
127152
*/
128153
public void initialize(@NonNull Context context, @NonNull String accessToken, @NonNull String userAgent) {
129154
if (initialized) {
@@ -134,9 +159,11 @@ public void initialize(@NonNull Context context, @NonNull String accessToken, @N
134159
this.context = context.getApplicationContext();
135160
this.accessToken = accessToken;
136161
this.userAgent = userAgent;
137-
if (this.context == null || TextUtils.isEmpty(this.accessToken) || TextUtils.isEmpty(this.userAgent)) {
162+
163+
if (this.context == null || TextUtils.isEmpty(this.accessToken) || TextUtils.isEmpty(this.userAgent)
164+
|| this.sdkIdentifier == null || this.sdkVersion == null) {
138165
throw new TelemetryException(
139-
"Please, make sure you provide a valid context, access token, and user agent. "
166+
"Please, make sure you provide a valid context, access token, user agent, sdkIdentifier and sdkVersion. "
140167
+ "For more information, please visit https://www.mapbox.com/android-sdk.");
141168
}
142169

@@ -651,6 +678,8 @@ private void pushTurnstileEvent() {
651678
event.put(MapboxEvent.KEY_CREATED, TelemetryUtils.generateCreateDate(null));
652679
event.put(MapboxEvent.KEY_USER_ID, mapboxVendorId);
653680
event.put(MapboxEvent.KEY_ENABLED_TELEMETRY, isTelemetryEnabled());
681+
event.put(MapboxEvent.KEY_SDK_IDENTIFIER, addSdkIdentifier());
682+
event.put(MapboxEvent.KEY_SDK_VERSION, addsdkVersion());
654683

655684
events.add(event);
656685
flushEventsQueueImmediately(true);
@@ -740,4 +769,26 @@ public void setAccessToken(@NonNull String accessToken) {
740769
this.accessToken = accessToken;
741770
client.setAccessToken(accessToken);
742771
}
772+
773+
/**
774+
* Check for empty strings and returns desired sdkIdentifier input for event
775+
*/
776+
private Object addSdkIdentifier() {
777+
if (sdkIdentifier.isEmpty()) {
778+
return JSONObject.NULL;
779+
}
780+
781+
return sdkIdentifier;
782+
}
783+
784+
/**
785+
* Check for empty strings and returns desired sdkVersion input for event
786+
*/
787+
private Object addsdkVersion() {
788+
if (sdkVersion.isEmpty()) {
789+
return JSONObject.NULL;
790+
}
791+
792+
return sdkVersion;
793+
}
743794
}

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/http/TelemetryClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ private void sendEventsWrapped(Vector<Hashtable<String, Object>> events, Callbac
237237
jsonObject.putOpt(MapboxEvent.KEY_SOURCE, evt.get(MapboxEvent.KEY_SOURCE));
238238
jsonObject.putOpt(MapboxEvent.KEY_SESSION_ID, evt.get(MapboxEvent.KEY_SESSION_ID));
239239
jsonObject.putOpt(MapboxEvent.KEY_LATITUDE, evt.get(MapboxEvent.KEY_LATITUDE));
240+
jsonObject.putOpt(MapboxEvent.KEY_SDK_IDENTIFIER, evt.get(MapboxEvent.KEY_SDK_IDENTIFIER));
241+
jsonObject.putOpt(MapboxEvent.KEY_SDK_VERSION, evt.get(MapboxEvent.KEY_SDK_VERSION));
240242

241243
// Make sure longitude is wrapped
242244
if (evt.containsKey(MapboxEvent.KEY_LONGITUDE)) {

0 commit comments

Comments
 (0)