-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first step to extract Analytics from the main project
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
WordPress/src/main/java/org/wordpress/android/util/AnalyticsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.wordpress.android.util; | ||
|
||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.text.TextUtils; | ||
|
||
import com.android.volley.VolleyError; | ||
import com.wordpress.rest.RestRequest; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.wordpress.android.WordPress; | ||
import org.wordpress.android.analytics.AnalyticsTracker; | ||
import org.wordpress.android.analytics.AnalyticsTrackerMixpanel; | ||
import org.wordpress.android.ui.prefs.AppPrefs; | ||
import org.wordpress.android.util.AppLog.T; | ||
|
||
public class AnalyticsUtils { | ||
/** | ||
* Utility method to refresh mixpanel metadata. | ||
* | ||
* @param username WordPress.com username | ||
* @param email WordPress.com email address | ||
*/ | ||
public static void refreshMetadata(String username, String email) { | ||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(WordPress.getContext()); | ||
int sessionCount = preferences.getInt(AnalyticsTrackerMixpanel.SESSION_COUNT, 0); | ||
boolean isUserConnected = WordPress.hasValidWPComCredentials(WordPress.getContext()); | ||
boolean isJetpackUser = WordPress.wpDB.hasAnyJetpackBlogs(); | ||
int numBlogs = WordPress.wpDB.getVisibleAccounts().size(); | ||
int versionCode = PackageUtils.getVersionCode(WordPress.getContext()); | ||
|
||
retrieveAndSaveEmailAddressIfApplicable(); | ||
AnalyticsTracker.refreshMetadata(isUserConnected, isJetpackUser, sessionCount, numBlogs, versionCode, | ||
username, email); | ||
} | ||
|
||
/** | ||
* Utility method to refresh mixpanel metadata. | ||
*/ | ||
public static void refreshMetadata() { | ||
// retrieve email address if user is logged in | ||
retrieveAndSaveEmailAddressIfApplicable(); | ||
|
||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(WordPress.getContext()); | ||
int sessionCount = preferences.getInt(AnalyticsTrackerMixpanel.SESSION_COUNT, 0); | ||
boolean isUserConnected = WordPress.hasValidWPComCredentials(WordPress.getContext()); | ||
boolean isJetpackUser = WordPress.wpDB.hasAnyJetpackBlogs(); | ||
int numBlogs = WordPress.wpDB.getVisibleAccounts().size(); | ||
int versionCode = PackageUtils.getVersionCode(WordPress.getContext()); | ||
String username = preferences.getString(WordPress.WPCOM_USERNAME_PREFERENCE, null); | ||
String email = AppPrefs.getMixpanelUserEmail(); | ||
AnalyticsTracker.refreshMetadata(isUserConnected, isJetpackUser, sessionCount, numBlogs, versionCode, | ||
username, email); | ||
} | ||
|
||
/** | ||
* Fetch user email address with the REST api, will be used later to fill Mixpanel metadata. | ||
*/ | ||
private static void retrieveAndSaveEmailAddressIfApplicable() { | ||
// Once the email address is bound to a mixpanel profile, we don't need to set (and get it) a second time. | ||
if (AppPrefs.getMixpanelUserEmail() != null) { | ||
return; | ||
} | ||
RestRequest.Listener listener = new RestRequest.Listener() { | ||
@Override | ||
public void onResponse(JSONObject jsonObject) { | ||
try { | ||
if (jsonObject != null && !TextUtils.isEmpty(jsonObject.getString("email"))) { | ||
String email = jsonObject.getString("email"); | ||
AppPrefs.setMixpanelUserEmail(email); | ||
} | ||
} catch (JSONException e) { | ||
AppLog.e(T.UTILS, "Can't get email field from json response: " + jsonObject); | ||
} | ||
} | ||
}; | ||
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() { | ||
@Override | ||
public void onErrorResponse(VolleyError volleyError) { | ||
AppLog.e(T.UTILS, volleyError); | ||
} | ||
}; | ||
|
||
String path = "/me"; | ||
WordPress.getRestClientUtils().get(path, listener, errorListener); | ||
} | ||
} |