Created
May 11, 2016 18:36
-
-
Save dGorod/c01396bf23b13aa7d093eeb01eafa30f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.dgorod.tourbag.presentation.analytics; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| /** | |
| * Created by Dmytro Gorodnytskyi on 30-Apr-16. | |
| */ | |
| public interface AbstractAnalyticsHelper { | |
| void trackScreen(@NonNull String screenName); | |
| void trackEvent(@NonNull String category, @NonNull String event); | |
| void trackEvent(@NonNull String category, @NonNull String event, @Nullable String label); | |
| } |
This file contains hidden or 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
| package com.dgorod.tourbag.presentation.analytics; | |
| import android.content.Context; | |
| import android.content.pm.PackageInfo; | |
| import android.content.pm.PackageManager; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| import android.text.TextUtils; | |
| import android.util.DisplayMetrics; | |
| import android.view.WindowManager; | |
| import com.crashlytics.android.answers.Answers; | |
| import com.crashlytics.android.answers.ContentViewEvent; | |
| import com.crashlytics.android.answers.CustomEvent; | |
| import com.dgorod.tourbag.R; | |
| import com.dgorod.tourbag.commons.Preconditions; | |
| import com.dgorod.tourbag.data.util.Logger; | |
| import com.google.android.gms.analytics.GoogleAnalytics; | |
| import com.google.android.gms.analytics.HitBuilders; | |
| import com.google.android.gms.analytics.Tracker; | |
| /** | |
| * Singleton to track application analytics with Google Analytics and Crashlytics Answers. | |
| * Class works like abstract factory and provides real or dummy implementations. | |
| * If not initialized by {@link #init(Context)} method it will return dummy implementation. | |
| * Such approach allows to use this class tracking methods in application without changes for | |
| * cases when analytics should not be gathered. For example, in debug builds. | |
| * | |
| * Created by Dmytro Gorodnytskyi on 29-Apr-16. | |
| */ | |
| public class AnalyticsHelper implements AbstractAnalyticsHelper { | |
| private static final String CATEGORY = "category"; | |
| private static final String LABEL = "label"; | |
| private static Tracker googleTracker; | |
| private static Answers fabricTracker; | |
| private static AnalyticsHelper instance; | |
| private static DummyAnalyticsHelper dummyInstance; | |
| static { | |
| dummyInstance = new DummyAnalyticsHelper(); | |
| } | |
| private AnalyticsHelper(@NonNull Context context) { | |
| fabricTracker = Answers.getInstance(); | |
| String trackerID = context.getString(R.string.ga_tracker_id); | |
| String appName = context.getString(R.string.app_name); | |
| WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
| DisplayMetrics metrics = new DisplayMetrics(); | |
| wm.getDefaultDisplay().getMetrics(metrics); | |
| googleTracker = GoogleAnalytics.getInstance(context).newTracker(trackerID); | |
| googleTracker.enableAdvertisingIdCollection(true); | |
| googleTracker.setUseSecure(true); | |
| googleTracker.setAppName(appName); | |
| googleTracker.setScreenResolution(metrics.widthPixels, metrics.heightPixels); | |
| try { | |
| PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); | |
| googleTracker.setAppVersion(pInfo.versionName); | |
| } | |
| catch (PackageManager.NameNotFoundException ex) { | |
| Logger.e(ex); | |
| } | |
| } | |
| public static synchronized void init(@NonNull Context context) { | |
| Preconditions.checkNotNull(context); | |
| if (instance == null) { | |
| instance = new AnalyticsHelper(context.getApplicationContext()); | |
| } | |
| } | |
| @NonNull | |
| public static synchronized AbstractAnalyticsHelper getInstance() { | |
| return instance == null ? dummyInstance : instance; | |
| } | |
| public void trackScreen(@NonNull String screenName) { | |
| Preconditions.checkNotNullOrEmpty(screenName); | |
| fabricTracker.logContentView(new ContentViewEvent().putContentName(screenName)); | |
| googleTracker.setScreenName(screenName); | |
| googleTracker.send(new HitBuilders.ScreenViewBuilder().build()); | |
| } | |
| public void trackEvent(@NonNull String category, @NonNull String event) { | |
| trackEvent(category, event, null); | |
| } | |
| public void trackEvent(@NonNull String category, @NonNull String event, @Nullable String label) { | |
| Preconditions.checkNotNullOrEmpty(category); | |
| Preconditions.checkNotNullOrEmpty(event); | |
| CustomEvent fabricEvent = new CustomEvent(event); | |
| fabricEvent.putCustomAttribute(CATEGORY, category); | |
| HitBuilders.EventBuilder googleEvent = new HitBuilders.EventBuilder(); | |
| googleEvent.setCategory(category); | |
| googleEvent.setAction(event); | |
| if (!TextUtils.isEmpty(label)) { | |
| fabricEvent.putCustomAttribute(LABEL, label); | |
| googleEvent.setLabel(label); | |
| } | |
| fabricTracker.logCustom(fabricEvent); | |
| googleTracker.setScreenName(null); | |
| googleTracker.send(googleEvent.build()); | |
| } | |
| } |
This file contains hidden or 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
| package com.dgorod.tourbag.presentation.analytics; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| /** | |
| * Analytics tracker without implementation. Use for cases when analytics should not be gathered. | |
| * | |
| * Created by Dmytro Gorodnytskyi on 30-Apr-16. | |
| */ | |
| public class DummyAnalyticsHelper implements AbstractAnalyticsHelper { | |
| @Override | |
| public void trackScreen(@NonNull String screenName) { } | |
| @Override | |
| public void trackEvent(@NonNull String category, @NonNull String event) { } | |
| @Override | |
| public void trackEvent(@NonNull String category, @NonNull String event, @Nullable String label) { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment