Skip to content

Commit

Permalink
Merge branch 'develop' into feature/update-to-gradle-2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Aug 25, 2015
2 parents a559239 + eda33fd commit bdf0f76
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 128 deletions.
2 changes: 1 addition & 1 deletion WordPressAnalytics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

dependencies {
compile 'com.automattic:tracks:1.0.4'
compile 'com.automattic:tracks:1.0.5'
compile 'com.mixpanel.android:mixpanel-android:4.3.0@aar'
compile 'org.wordpress:utils:1.3.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public final class AnalyticsTracker {
public enum Stat {
APPLICATION_OPENED,
APPLICATION_CLOSED,
APPLICATION_STARTED,
APPLICATION_UPGRADED,
THEMES_ACCESSED_THEMES_BROWSER,
THEMES_CHANGED_THEME,
Expand Down Expand Up @@ -57,10 +56,6 @@ public enum Stat {
EDITOR_CLOSED_POST,
EDITOR_PUBLISHED_POST,
EDITOR_SAVED_DRAFT,
EDITOR_PUBLISHED_POST_WITH_PHOTO,
EDITOR_PUBLISHED_POST_WITH_VIDEO,
EDITOR_PUBLISHED_POST_WITH_CATEGORIES,
EDITOR_PUBLISHED_POST_WITH_TAGS,
EDITOR_TAPPED_BLOCKQUOTE,
EDITOR_TAPPED_BOLD,
EDITOR_TAPPED_IMAGE,
Expand Down Expand Up @@ -111,16 +106,6 @@ public enum Stat {
NOTIFICATION_SETTINGS_UPDATED,
}

public interface Tracker {
void track(Stat stat);
void track(Stat stat, Map<String, ?> properties);
void endSession();
void refreshMetadata(boolean isUserConnected,boolean isWordPressComUser, boolean isJetpackUser,
int sessionCount, int numBlogs, int versionCode, String username, String email);
void clearAllData();
void registerPushNotificationToken(String regId);
}

private static final List<Tracker> TRACKERS = new ArrayList<Tracker>();

private AnalyticsTracker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import java.util.Iterator;
import java.util.Map;

public class AnalyticsTrackerMixpanel implements AnalyticsTracker.Tracker {
public class AnalyticsTrackerMixpanel extends Tracker {
public static final String SESSION_COUNT = "sessionCount";

private Context mContext;
private MixpanelAPI mMixpanel;
private EnumMap<AnalyticsTracker.Stat, JSONObject> mAggregatedProperties;
private static final String MIXPANEL_PLATFORM = "platform";
Expand All @@ -32,11 +31,12 @@ public class AnalyticsTrackerMixpanel implements AnalyticsTracker.Tracker {
private static final String MIXPANEL_NUMBER_OF_BLOGS = "number_of_blogs";
private static final String VERSION_CODE = "version_code";
private static final String APP_LOCALE = "app_locale";
private static final String MIXPANEL_ANON_ID = "mixpanel_user_anon_id";

public AnalyticsTrackerMixpanel(Context context, String token) {
public AnalyticsTrackerMixpanel(Context context, String token) throws IllegalArgumentException {
super(context);
mAggregatedProperties = new EnumMap<AnalyticsTracker.Stat, JSONObject>(AnalyticsTracker.Stat.class);
mMixpanel = MixpanelAPI.getInstance(context, token);
mContext = context;
}

@SuppressWarnings("deprecation")
Expand All @@ -57,6 +57,10 @@ public static void showNotification(Context context, PendingIntent intent, int n
nm.notify(0, notification);
}

String getAnonIdPrefKey() {
return MIXPANEL_ANON_ID;
}

@Override
public void track(AnalyticsTracker.Stat stat) {
track(stat, null);
Expand All @@ -79,6 +83,13 @@ private void trackMixpanelDataForInstructions(AnalyticsTrackerMixpanelInstructio
return;
}

// Just a security check we're tracking the correct user
if (getWordPressComUserName() == null && getAnonID() == null) {
this.clearAllData();
generateNewAnonID();
mMixpanel.identify(getAnonID());
}

trackMixpanelEventForInstructions(instructions, properties);
trackMixpanelPropertiesForInstructions(instructions);
}
Expand Down Expand Up @@ -181,17 +192,41 @@ public void refreshMetadata(boolean isUserConnected, boolean isWordPressComUser,
AppLog.e(AppLog.T.UTILS, e);
}


if (isUserConnected && isWordPressComUser) {
setWordPressComUserName(username);
// Re-unify the user
if (getAnonID() != null) {
mMixpanel.alias(getWordPressComUserName(), getAnonID());
clearAnonID();
} else {
mMixpanel.identify(username);
}
} else {
// Not wpcom connected. Check if anonID is already present
setWordPressComUserName(null);
if (getAnonID() == null) {
generateNewAnonID();
}
mMixpanel.identify(getAnonID());
}

// Application opened and start.
if (isUserConnected) {
mMixpanel.identify(username);
try {
mMixpanel.getPeople().identify(username);
String userID = getWordPressComUserName() != null ? getWordPressComUserName() : getAnonID();
if (userID == null) {
// This should not be an option here
return;
}

mMixpanel.getPeople().identify(userID);
JSONObject jsonObj = new JSONObject();
jsonObj.put("$username", username);
jsonObj.put("$username", userID);
if (email != null) {
jsonObj.put("$email", email);
}
jsonObj.put("$first_name", username);
jsonObj.put("$first_name", userID);
mMixpanel.getPeople().set(jsonObj);
} catch (JSONException e) {
AppLog.e(AppLog.T.UTILS, e);
Expand All @@ -203,6 +238,7 @@ public void refreshMetadata(boolean isUserConnected, boolean isWordPressComUser,

@Override
public void clearAllData() {
super.clearAllData();
mMixpanel.clearSuperProperties();
try {
mMixpanel.getPeople().clearPushRegistrationId();
Expand All @@ -215,11 +251,6 @@ private AnalyticsTrackerMixpanelInstructionsForStat instructionsForStat(
AnalyticsTracker.Stat stat) {
AnalyticsTrackerMixpanelInstructionsForStat instructions = null;
switch (stat) {
case APPLICATION_STARTED:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsForEventName("Application Started");
instructions.setSuperPropertyToIncrement("Application Started");
break;
case APPLICATION_OPENED:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsForEventName("Application Opened");
Expand Down Expand Up @@ -380,30 +411,6 @@ private AnalyticsTrackerMixpanelInstructionsForStat instructionsForStat(
instructions.setSuperPropertyAndPeoplePropertyToIncrement("number_of_times_editor_scheduled_post");
instructions.setCurrentDateForPeopleProperty("last_time_scheduled_post");
break;
case EDITOR_PUBLISHED_POST_WITH_PHOTO:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsWithSuperPropertyAndPeoplePropertyIncrementor(
"number_of_posts_published_with_photos");
instructions.setCurrentDateForPeopleProperty("last_time_published_post_with_photo");
break;
case EDITOR_PUBLISHED_POST_WITH_VIDEO:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsWithSuperPropertyAndPeoplePropertyIncrementor(
"number_of_posts_published_with_videos");
instructions.setCurrentDateForPeopleProperty("last_time_published_post_with_video");
break;
case EDITOR_PUBLISHED_POST_WITH_CATEGORIES:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsWithSuperPropertyAndPeoplePropertyIncrementor(
"number_of_posts_published_with_categories");
instructions.setCurrentDateForPeopleProperty("last_time_published_post_with_categories");
break;
case EDITOR_PUBLISHED_POST_WITH_TAGS:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsWithSuperPropertyAndPeoplePropertyIncrementor(
"number_of_posts_published_with_tags");
instructions.setCurrentDateForPeopleProperty("last_time_published_post_with_tags");
break;
case EDITOR_TAPPED_BLOCKQUOTE:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsForEventName("Editor - Tapped Blockquote Button");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.wordpress.android.analytics;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.automattic.android.tracks.TracksClient;

Expand All @@ -12,31 +10,26 @@

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class AnalyticsTrackerNosara implements AnalyticsTracker.Tracker {
public class AnalyticsTrackerNosara extends Tracker {

private static final String JETPACK_USER = "jetpack_user";
private static final String NUMBER_OF_BLOGS = "number_of_blogs";
private static final String TRACKS_ANON_ID = "nosara_tracks_anon_id";

private static final String EVENTS_PREFIX = "wpandroid_";

private String mWpcomUserName = null;
private String mAnonID = null; // do not access this variable directly. Use methods.

private TracksClient mNosaraClient;
private Context mContext;

public AnalyticsTrackerNosara(Context context) {
if (null == context) {
mNosaraClient = null;
return;
}
mContext = context;
public AnalyticsTrackerNosara(Context context) throws IllegalArgumentException {
super(context);
mNosaraClient = TracksClient.getClient(context);
}

String getAnonIdPrefKey() {
return TRACKS_ANON_ID;
}

@Override
public void track(AnalyticsTracker.Stat stat) {
track(stat, null);
Expand All @@ -52,9 +45,6 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
String eventName;

switch (stat) {
case APPLICATION_STARTED:
eventName = "application_started";
break;
case APPLICATION_OPENED:
eventName = "application_opened";
break;
Expand Down Expand Up @@ -141,22 +131,6 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
case EDITOR_SCHEDULED_POST:
eventName = "editor_post_scheduled";
break;
case EDITOR_PUBLISHED_POST_WITH_PHOTO:
eventName = "editor_post_published";
predefinedEventProperties.put("with_photos", true);
break;
case EDITOR_PUBLISHED_POST_WITH_VIDEO:
eventName = "editor_post_published";
predefinedEventProperties.put("with_videos", true);
break;
case EDITOR_PUBLISHED_POST_WITH_CATEGORIES:
eventName = "editor_post_published";
predefinedEventProperties.put("with_categories", true);
break;
case EDITOR_PUBLISHED_POST_WITH_TAGS:
eventName = "editor_post_published";
predefinedEventProperties.put("with_tags", true);
break;
case EDITOR_TAPPED_BLOCKQUOTE:
eventName = "editor_button_tapped";
predefinedEventProperties.put("button", "blockquote");
Expand Down Expand Up @@ -365,8 +339,8 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {

final String user;
final TracksClient.NosaraUserType userType;
if (mWpcomUserName != null) {
user = mWpcomUserName;
if (getWordPressComUserName() != null) {
user = getWordPressComUserName();
userType = TracksClient.NosaraUserType.WPCOM;
} else {
// This is just a security checks since the anonID is already available here.
Expand Down Expand Up @@ -410,41 +384,7 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
}
}

private void clearAnonID() {
mAnonID = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
if (preferences.contains(TRACKS_ANON_ID)) {
final SharedPreferences.Editor editor = preferences.edit();
editor.remove(TRACKS_ANON_ID);
editor.commit();
}
}

private String getAnonID() {
if (mAnonID == null) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mAnonID = preferences.getString(TRACKS_ANON_ID, null);
}
return mAnonID;
}

private String generateNewAnonID() {
String uuid = UUID.randomUUID().toString();
String[] uuidSplitted = uuid.split("-");
StringBuilder builder = new StringBuilder();
for (String currentPart : uuidSplitted) {
builder.append(currentPart);
}
uuid = builder.toString();
AppLog.d(AppLog.T.STATS, "New anon ID generated: " + uuid);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(TRACKS_ANON_ID, uuid);
editor.commit();

mAnonID = uuid;
return uuid;
}

@Override
public void endSession() {
Expand All @@ -454,6 +394,7 @@ public void endSession() {
mNosaraClient.flush();
}


@Override
public void refreshMetadata(boolean isUserConnected, boolean isWordPressComUser, boolean isJetpackUser,
int sessionCount, int numBlogs, int versionCode, String username, String email) {
Expand All @@ -462,15 +403,15 @@ public void refreshMetadata(boolean isUserConnected, boolean isWordPressComUser,
}

if (isUserConnected && isWordPressComUser) {
mWpcomUserName = username;
setWordPressComUserName(username);
// Re-unify the user
if (getAnonID() != null) {
mNosaraClient.trackAliasUser(mWpcomUserName, getAnonID());
mNosaraClient.trackAliasUser(getWordPressComUserName(), getAnonID());
clearAnonID();
}
} else {
// Not wpcom connected. Check if anonID is already present
mWpcomUserName = null;
setWordPressComUserName(null);
if (getAnonID() == null) {
generateNewAnonID();
}
Expand All @@ -489,13 +430,11 @@ public void refreshMetadata(boolean isUserConnected, boolean isWordPressComUser,

@Override
public void clearAllData() {
super.clearAllData();
if (mNosaraClient == null) {
return;
}
mNosaraClient.clearUserProperties();
// Reset the anon ID here
clearAnonID();
mWpcomUserName = null;
}

@Override
Expand Down
Loading

0 comments on commit bdf0f76

Please sign in to comment.