Skip to content

Commit 0ff0cc0

Browse files
authored
Merge pull request #5626 from wordpress-mobile/issue/check-null-analytics-events
Check null analytics events
2 parents db22b62 + 23e354a commit 0ff0cc0

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.wordpress.android.analytics;
2+
3+
public class AnalyticsException extends Exception {
4+
public AnalyticsException(String message) {
5+
super(message);
6+
}
7+
}

libs/analytics/WordPressAnalytics/src/main/java/org/wordpress/android/analytics/AnalyticsTrackerMixpanel.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public void track(AnalyticsTracker.Stat stat) {
6161

6262
@Override
6363
public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
64+
if (!isValidEvent(stat)) {
65+
return;
66+
}
67+
6468
AnalyticsTrackerMixpanelInstructionsForStat instructions = instructionsForStat(stat);
6569

6670
if (instructions == null) {
@@ -242,8 +246,10 @@ public void clearAllData() {
242246
}
243247
}
244248

245-
private AnalyticsTrackerMixpanelInstructionsForStat instructionsForStat(
246-
AnalyticsTracker.Stat stat) {
249+
private AnalyticsTrackerMixpanelInstructionsForStat instructionsForStat(AnalyticsTracker.Stat stat) {
250+
if (!isValidEvent(stat)) {
251+
return null;
252+
}
247253
AnalyticsTrackerMixpanelInstructionsForStat instructions;
248254
switch (stat) {
249255
case APPLICATION_OPENED:

libs/analytics/WordPressAnalytics/src/main/java/org/wordpress/android/analytics/AnalyticsTrackerNosara.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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";

libs/analytics/WordPressAnalytics/src/main/java/org/wordpress/android/analytics/Tracker.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ String getWordPressComUserName() {
7474
void setWordPressComUserName(String userName) {
7575
mWpcomUserName = userName;
7676
}
77+
78+
public static boolean isValidEvent(Stat stat) {
79+
if (stat == null) {
80+
try {
81+
throw new AnalyticsException("Trying to track a null stat event!");
82+
} catch (AnalyticsException e) {
83+
AppLog.e(AppLog.T.STATS, e);
84+
}
85+
return false;
86+
}
87+
return true;
88+
}
7789
}

0 commit comments

Comments
 (0)