@@ -41,6 +41,10 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
4141 return ;
4242 }
4343
44+ if (!isValidEvent (stat )) {
45+ return ;
46+ }
47+
4448 String eventName = getEventNameForStat (stat );
4549 if (eventName == null ) {
4650 AppLog .w (AppLog .T .STATS , "There is NO match for the event " + stat .name () + "stat" );
@@ -156,27 +160,43 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
156160 userType = TracksClient .NosaraUserType .ANON ;
157161 }
158162
163+ // It seems that we're tracking some events with user = null. Make sure we're catching the error here.
164+ if (user == null ) {
165+ try {
166+ throw new AnalyticsException ("Trying to track analytics with an null user!" );
167+ // TODO add CrashlyticsUtils.logException or track this error in Nosara by using a special test user.
168+ } catch (AnalyticsException e ) {
169+ AppLog .e (AppLog .T .STATS , e );
170+ }
171+ return ;
172+ }
159173
160174 // create the merged JSON Object of properties
161175 // Properties defined by the user have precedence over the default ones pre-defined at "event level"
162- final JSONObject propertiesToJSON ;
176+ JSONObject propertiesToJSON = null ;
163177 if (properties != null && properties .size () > 0 ) {
164- propertiesToJSON = new JSONObject (properties );
165- for (String key : predefinedEventProperties .keySet ()) {
166- try {
167- if (propertiesToJSON .has (key )) {
168- AppLog .w (AppLog .T .STATS , "The user has defined a property named: '" + key + "' that will override" +
169- "the same property pre-defined at event level. This may generate unexpected behavior!!" );
170- AppLog .w (AppLog .T .STATS , "User value: " + propertiesToJSON .get (key ).toString () + " - pre-defined value: " +
171- predefinedEventProperties .get (key ).toString ());
172- } else {
173- propertiesToJSON .put (key , predefinedEventProperties .get (key ));
178+ try {
179+ propertiesToJSON = new JSONObject (properties );
180+ for (String key : predefinedEventProperties .keySet ()) {
181+ try {
182+ if (propertiesToJSON .has (key )) {
183+ AppLog .w (AppLog .T .STATS , "The user has defined a property named: '" + key + "' that will override" +
184+ "the same property pre-defined at event level. This may generate unexpected behavior!!" );
185+ AppLog .w (AppLog .T .STATS , "User value: " + propertiesToJSON .get (key ).toString () + " - pre-defined value: " +
186+ predefinedEventProperties .get (key ).toString ());
187+ } else {
188+ propertiesToJSON .put (key , predefinedEventProperties .get (key ));
189+ }
190+ } catch (JSONException e ) {
191+ AppLog .e (AppLog .T .STATS , "Error while merging user-defined properties with pre-defined properties" , e );
174192 }
175- } catch (JSONException e ) {
176- AppLog .e (AppLog .T .STATS , "Error while merging user-defined properties with pre-defined properties" , e );
177193 }
194+ } catch (NullPointerException e ) {
195+ AppLog .e (AppLog .T .STATS , "A property passed to the event " + eventName + " has null key!" , e );
178196 }
179- } else {
197+ }
198+
199+ if (propertiesToJSON == null ) {
180200 propertiesToJSON = new JSONObject (predefinedEventProperties );
181201 }
182202
@@ -187,8 +207,6 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
187207 }
188208 }
189209
190-
191-
192210 @ Override
193211 public void endSession () {
194212 this .flush ();
@@ -251,6 +269,10 @@ public void registerPushNotificationToken(String regId) {
251269 }
252270
253271 public static String getEventNameForStat (AnalyticsTracker .Stat stat ) {
272+ if (!isValidEvent (stat )) {
273+ return null ;
274+ }
275+
254276 switch (stat ) {
255277 case APPLICATION_OPENED :
256278 return "application_opened" ;
0 commit comments