Skip to content

Commit

Permalink
Merge pull request #87 from amardeshbd/feature/4_add_analytics
Browse files Browse the repository at this point in the history
Feature/4 add analytics
  • Loading branch information
hossain-khan authored Oct 29, 2016
2 parents ad8d1ee + 67b44b8 commit 0869b0c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import android.app.Application;
import android.content.Context;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.squareup.leakcanary.LeakCanary;

import info.hossainkhan.android.core.analytics.AnalyticsReporter;
import info.hossainkhan.android.core.dagger.components.AppComponent;
import info.hossainkhan.android.core.dagger.components.DaggerAppComponent;
import info.hossainkhan.android.core.dagger.modules.InteractorsModule;
Expand All @@ -43,11 +45,16 @@ public class CoreApplication extends Application {

private static AppComponent sAppComponent;
private static final boolean ENABLE_LOGGING = true;
private static CoreApplication sContext;
private static AnalyticsReporter sAnalyticsReporter;

@Override
public void onCreate() {
super.onCreate();

sContext = this;
sAnalyticsReporter = new AnalyticsReporter(FirebaseAnalytics.getInstance(sContext));

initLeakCanary();
initAppComponent();
initLogger();
Expand Down Expand Up @@ -95,4 +102,12 @@ public static AppComponent getAppComponent() {
public static CoreApplication getCoreApplication(final Context context) {
return (CoreApplication) context.getApplicationContext();
}

/**
* Provides application analytics reporter.
* @return analytics reporter instance.
*/
public static AnalyticsReporter getAnalyticsReporter() {
return sAnalyticsReporter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* MIT License
*
* Copyright (c) 2016 Hossain Khan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package info.hossainkhan.android.core.analytics;

import android.os.Bundle;

import com.google.firebase.analytics.FirebaseAnalytics;

import info.hossainkhan.android.core.model.CardItem;

public class AnalyticsReporter {

private static final String EVENT_NAME_HEADLINE_LOADING_ERROR = "headline_load_failed";
private static final String EVENT_NAME_HEADLINE_DETAILS_LOAD = "details_content";
private static final String EVENT_NAME_SETTINGS_LOAD = "application_settings";

private FirebaseAnalytics mAnalytics;

/**
* Creates analytics reporter with firebase backend reporting.
* @param analytics Firebase analytics instance.
*/
public AnalyticsReporter(FirebaseAnalytics analytics) {
mAnalytics = analytics;
}


public void reportHeadlineSelectedEvent(final CardItem card) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, card.contentUrl());
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, card.title());
bundle.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, card.category());
mAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
}

public void reportHeadlineLoadingError() {
mAnalytics.logEvent(EVENT_NAME_HEADLINE_LOADING_ERROR, null);
}

public void reportHeadlineDetailsLoadedEvent(CardItem card) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, card.contentUrl());
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, card.title());
bundle.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, card.category());
mAnalytics.logEvent(EVENT_NAME_HEADLINE_DETAILS_LOAD, bundle);
}

public void reportSettingsScreenLoadedEvent(String settingsName) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, settingsName);
bundle.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "app_settings");
mAnalytics.logEvent(EVENT_NAME_SETTINGS_LOAD, bundle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void onError(Throwable e) {
getView().toggleLoadingIndicator(false);

getView().showDataLoadingError();
CoreApplication.getAnalyticsReporter().reportHeadlineLoadingError();
FirebaseCrash.report(e);
}

Expand Down Expand Up @@ -181,6 +182,7 @@ public void openHeadlineDetails(@NonNull final CardItem cardItem) {

@Override
public void onHeadlineItemSelected(@NonNull final CardItem cardItem) {
CoreApplication.getAnalyticsReporter().reportHeadlineSelectedEvent(cardItem);
if (cardItem.imageUrl() !=null) {
getView().showHeadlineBackdropBackground(cardItem.getImageURI());
} else {
Expand All @@ -194,11 +196,13 @@ public void onHeadlineItemClicked(@NonNull final CardItem cardItem) {
CardItem.Type type = cardItem.type();
if (type == CardItem.Type.ICON) {
if (id == R.string.settings_card_item_news_source_title) {
CoreApplication.getAnalyticsReporter().reportSettingsScreenLoadedEvent(mContext.getString(R.string.settings_card_item_news_source_title));
getView().showAppSettingsScreen();
} else {
Timber.w("Unable to handle settings item: %s", cardItem.title());
}
} else if(type == CardItem.Type.HEADLINES) {
CoreApplication.getAnalyticsReporter().reportHeadlineDetailsLoadedEvent(cardItem);
getView().showHeadlineDetailsUi(cardItem);
}
}
Expand Down

0 comments on commit 0869b0c

Please sign in to comment.