Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Partial fix for #82: Add analytics for Ghost version and login events.
Browse files Browse the repository at this point in the history
  • Loading branch information
vickychijwani committed May 19, 2016
1 parent 26f7434 commit 6932add
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.otto.DeadEvent;
Expand All @@ -26,8 +27,10 @@
import java.util.concurrent.TimeUnit;

import io.fabric.sdk.android.Fabric;
import me.vickychijwani.spectre.analytics.AnalyticsService;
import me.vickychijwani.spectre.event.ApiErrorEvent;
import me.vickychijwani.spectre.event.BusProvider;
import me.vickychijwani.spectre.event.LoadGhostVersionEvent;
import me.vickychijwani.spectre.network.NetworkService;
import retrofit.RetrofitError;
import retrofit.client.Response;
Expand All @@ -45,10 +48,13 @@ public class SpectreApplication extends Application {
protected OkHttpClient mOkHttpClient = null;
protected Picasso mPicasso = null;

@SuppressWarnings("FieldCanBeLocal")
private AnalyticsService mAnalyticsService = null;

@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
Fabric.with(this, new Crashlytics(), new Answers());
Crashlytics.log(Log.DEBUG, TAG, "APP LAUNCHED");
BusProvider.getBus().register(this);
sInstance = this;
Expand All @@ -57,6 +63,9 @@ public void onCreate() {
initOkHttpClient();
initPicasso();
new NetworkService().start(this, mOkHttpClient);

mAnalyticsService = new AnalyticsService(BusProvider.getBus());
mAnalyticsService.start();
}

private void setupFonts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package me.vickychijwani.spectre.analytics;

import android.support.annotation.Nullable;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import com.crashlytics.android.answers.LoginEvent;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import me.vickychijwani.spectre.event.GhostVersionLoadedEvent;
import me.vickychijwani.spectre.event.LoadGhostVersionEvent;
import me.vickychijwani.spectre.event.LoginDoneEvent;
import me.vickychijwani.spectre.event.LoginErrorEvent;

public class AnalyticsService {

private static final String TAG = AnalyticsService.class.getSimpleName();

private Bus mEventBus;

public AnalyticsService(Bus eventBus) {
mEventBus = eventBus;
}

public void start() {
getBus().register(this);
getBus().post(new LoadGhostVersionEvent(true));
}

public void stop() {
getBus().unregister(this);
}

@Subscribe
public void onLoginDoneEvent(LoginDoneEvent event) {
if (event.wasInitiatedByUser) {
String blogType = getBlogTypeFromUrl(event.blogUrl);
logLogin(blogType, true);

// user just logged in, now's a good time to check this
getBus().post(new LoadGhostVersionEvent(true));
}
}

@Subscribe
public void onLoginErrorEvent(LoginErrorEvent event) {
if (event.wasInitiatedByUser) {
String blogType = getBlogTypeFromUrl(event.blogUrl);
logLogin(blogType, true);
}
}

@Subscribe
public void onGhostVersionLoadedEvent(GhostVersionLoadedEvent event) {
logGhostVersion(event.version);
}

public static void logGhostVersion(@Nullable String ghostVersion) {
if (ghostVersion == null) {
ghostVersion = "Unknown";
}
Crashlytics.log(Log.DEBUG, TAG, "GHOST VERSION = " + ghostVersion);
Answers.getInstance().logCustom(new CustomEvent("Ghost Version")
.putCustomAttribute("version", ghostVersion));
}

public static void logLogin(String blogType, boolean success) {
String successStr = success ? "SUCCEEDED" : "FAILED";
Crashlytics.log(Log.DEBUG, TAG, "LOGIN " + successStr + ", blog type = " + blogType);
Answers.getInstance().logLogin(new LoginEvent()
.putMethod(blogType)
.putSuccess(success));
}

private String getBlogTypeFromUrl(@Nullable String blogUrl) {
if (blogUrl == null) {
return "Unknown";
} else if (blogUrl.matches("ghost.io")) {
return "Ghost Pro";
} else {
return "Self-hosted";
}
}

private Bus getBus() {
return mEventBus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.vickychijwani.spectre.event;

import android.support.annotation.NonNull;

public class GhostVersionLoadedEvent {

public final String version;

public GhostVersionLoadedEvent(@NonNull String version) {
this.version = version;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.vickychijwani.spectre.event;

public class LoadGhostVersionEvent implements ApiCallEvent {

public final boolean forceNetworkCall;
public boolean loadCachedData = false;

public LoadGhostVersionEvent(boolean forceNetworkCall) {
this.forceNetworkCall = forceNetworkCall;
}

@Override
public void loadCachedData() {
loadCachedData = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ public class LoginDoneEvent {
public final String blogUrl;
public final String username;
public final String password;
public final boolean wasInitiatedByUser;

public LoginDoneEvent(String blogUrl, String username, String password) {
public LoginDoneEvent(String blogUrl, String username, String password,
boolean wasInitiatedByUser) {
this.blogUrl = blogUrl;
this.username = username;
this.password = password;
this.wasInitiatedByUser = wasInitiatedByUser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ public class LoginErrorEvent {

public final RetrofitError error;
public final String blogUrl;
public final boolean wasInitiatedByUser;

public LoginErrorEvent(RetrofitError error, @Nullable String blogUrl) {
public LoginErrorEvent(RetrofitError error, @Nullable String blogUrl,
boolean wasInitiatedByUser) {
this.error = error;
this.blogUrl = blogUrl;
this.wasInitiatedByUser = wasInitiatedByUser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import me.vickychijwani.spectre.model.SettingsList;
import me.vickychijwani.spectre.model.UserList;
import retrofit.Callback;
import retrofit.ResponseCallback;
import retrofit.client.Response;
import retrofit.http.Body;
import retrofit.http.GET;
Expand All @@ -36,7 +35,7 @@ interface GhostApiService {
void refreshAuthToken(@Body RefreshReqBody credentials, Callback<AuthToken> cb);

@POST("/authentication/revoke")
void revokeAuthToken(@Body RevokeReqBody revoke, ResponseCallback cb);
void revokeAuthToken(@Body RevokeReqBody revoke, JSONObjectCallback cb);

// users
@GET("/users/me/?include=roles&status=all")
Expand All @@ -61,6 +60,9 @@ void getPosts(@Header("If-None-Match") String etag, @Query("limit") int numPosts
@GET("/configuration/")
void getConfiguration(@Header("If-None-Match") String etag, Callback<ConfigurationList> cb);

@GET("/configuration/about/")
void getVersion(JSONObjectCallback cb);

// file upload
@Multipart
@POST("/uploads")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.vickychijwani.spectre.network;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import retrofit.ResponseCallback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/*package*/ abstract class JSONObjectCallback extends ResponseCallback {

public abstract void onSuccess(JSONObject json, Response response);

public abstract void onFailure(RetrofitError error);

@Override
public void success(Response response) {
try {
InputStream istream = response.getBody().in();
BufferedReader reader = new BufferedReader(new InputStreamReader(istream));
StringBuilder out = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(newLine);
}
JSONObject json = new JSONObject(out.toString());
onSuccess(json, response);
} catch (IOException e) {
onFailure(RetrofitError.unexpectedError(response.getUrl(), e));
} catch (JSONException e) {
onFailure(RetrofitError.unexpectedError(response.getUrl(), e));
}
}

@Override
public void failure(RetrofitError error) {
onFailure(error);
}

}
Loading

0 comments on commit 6932add

Please sign in to comment.