This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial fix for #82: Add analytics for Ghost version and login events.
- Loading branch information
1 parent
26f7434
commit 6932add
Showing
9 changed files
with
301 additions
and
45 deletions.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
app/src/main/java/me/vickychijwani/spectre/analytics/AnalyticsService.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,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; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/me/vickychijwani/spectre/event/GhostVersionLoadedEvent.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,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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/me/vickychijwani/spectre/event/LoadGhostVersionEvent.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,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; | ||
} | ||
|
||
} |
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
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
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
47 changes: 47 additions & 0 deletions
47
app/src/main/java/me/vickychijwani/spectre/network/JSONObjectCallback.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,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); | ||
} | ||
|
||
} |
Oops, something went wrong.