3333import com .mapbox .services .android .telemetry .service .TelemetryService ;
3434import com .mapbox .services .android .telemetry .utils .TelemetryUtils ;
3535
36+ import org .json .JSONObject ;
37+
3638import java .io .IOException ;
3739import java .math .BigDecimal ;
3840import 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}
0 commit comments