Skip to content

Commit

Permalink
GaEvents replaced and optimized for Firebase Analytics.
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrambhia committed Oct 12, 2019
1 parent 253b6e8 commit d382f7b
Show file tree
Hide file tree
Showing 40 changed files with 325 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import android.view.ViewGroup
import com.fenchtose.movieratings.R
import com.fenchtose.movieratings.analytics.ga.GaCategory
import com.fenchtose.movieratings.analytics.ga.GaScreens
import com.fenchtose.movieratings.analytics.ga.AppScreens
import com.fenchtose.movieratings.base.BaseFragment
import com.fenchtose.movieratings.base.RouterPath
import com.fenchtose.movieratings.model.preferences.SettingsPreferences
Expand All @@ -22,7 +22,7 @@ class DebugOptionsFragment: BaseFragment() {

override fun canGoBack() = true
override fun getScreenTitle() = R.string.debug_screen_title
override fun screenName() = GaScreens.DEBUGGING
override fun screenName() = AppScreens.DEBUGGING

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.debug_options_screen, container, false)
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/fenchtose/movieratings/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.core.os.ConfigurationCompat
import androidx.appcompat.app.AlertDialog
import android.util.Log
import android.view.ViewGroup
import com.fenchtose.movieratings.analytics.events.toGaEvent
import com.fenchtose.movieratings.analytics.ga.GaEvents
import com.fenchtose.movieratings.analytics.events.toFaEvent
import com.fenchtose.movieratings.analytics.ga.AppEvents
import com.fenchtose.movieratings.analytics.ga.GaLabels
import com.fenchtose.movieratings.base.RouterBaseActivity
import com.fenchtose.movieratings.base.router.Router
Expand Down Expand Up @@ -58,7 +58,6 @@ class MainActivity : RouterBaseActivity() {
), 0)

addListener { position, item, reselected ->
GaEvents.SELECT_BOTTOM_TAB.withLabelArg(item.eventLabel).track()
getRouter()?.switchRoot(item.root, reselected)
}
}
Expand Down Expand Up @@ -129,8 +128,8 @@ class MainActivity : RouterBaseActivity() {
history = Router.History(historyBundle)
}

if (intent.hasExtra("ga_event")) {
intent.getBundleExtra("ga_event").toGaEvent()?.track()
if (intent.hasExtra("fa_event")) {
intent.getBundleExtra("fa_event").toFaEvent()?.track()
}

root = Router.ROOT_SEARCH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.view.accessibility.AccessibilityNodeInfo
import com.fenchtose.movieratings.analytics.ga.GaCategory
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import com.fenchtose.movieratings.analytics.ga.GaEvents
import com.fenchtose.movieratings.analytics.ga.AppEvents
import com.fenchtose.movieratings.analytics.ga.GaLabels
import com.fenchtose.movieratings.display.RatingDisplayer
import com.fenchtose.movieratings.features.tts.Speaker
Expand Down Expand Up @@ -284,7 +284,6 @@ class NetflixReaderService : AccessibilityService() {
private fun getMovieInfo(request: RatingRequest) {
initResources()

GaEvents.GET_RATINGS.withLabelArg(request.appName).track()
val useFlutterApi = preferences?.isAppEnabled(UserPreferences.USE_FLUTTER_API) ?: true
val provider = provider ?: return

Expand All @@ -296,7 +295,9 @@ class NetflixReaderService : AccessibilityService() {
.doOnNext { historyPublisher.onNext(request) }
.subscribe(::showRating) { error ->
if (error is HttpException && error.code() == 404) {
GaEvents.RATING_NOT_FOUND.withLabelArg(if (useFlutterApi) GaLabels.FLUTTER_API else GaLabels.OMDB_API).track()
AppEvents.ratingNotFound(
if (useFlutterApi) GaLabels.FLUTTER_API else GaLabels.OMDB_API
).track()
update404(request.title, request.year)
}

Expand Down Expand Up @@ -362,7 +363,7 @@ class NetflixReaderService : AccessibilityService() {
return
}

GaEvents.NOTIFICATION_BLOCKED.withLabel(GaLabels.NOTIFICATION_SUPPORT_APP).track()
AppEvents.notificationBlocked(GaLabels.NOTIFICATION_SUPPORT_APP).track()
}

private fun showRateAppPrompt() {
Expand All @@ -372,13 +373,13 @@ class NetflixReaderService : AccessibilityService() {
return
}

GaEvents.NOTIFICATION_BLOCKED.withLabel(GaLabels.NOTIFICATION_RATE_APP).track()
AppEvents.notificationBlocked(GaLabels.NOTIFICATION_RATE_APP).track()
}

private fun showRating(rating: MovieRating) {
displayer?.showRatingWindow(rating)
if (preferences?.isSettingEnabled(UserPreferences.TTS_AVAILABLE) == true && preferences?.isSettingEnabled(UserPreferences.USE_TTS) == true) {
GaEvents.SPEAK_RATING.track()
AppEvents.SPEAK_RATING.track()
speaker?.talk(rating)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,66 @@ package com.fenchtose.movieratings.analytics.events

import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.fenchtose.movieratings.BuildConfig
import com.fenchtose.movieratings.MovieRatingsApplication
import com.fenchtose.movieratings.util.emptyAsNull

interface Event {
fun track() {
MovieRatingsApplication.analyticsDispatcher.sendEvent(this)
}
}

data class GaEvent(val category: String, val action: String, val label: String,
val nonInteractive: Boolean = false): Event {

fun withLabel(label: String): GaEvent {
return GaEvent(category, action, label)
}

fun withLabelArg(arg: String): GaEvent {
return GaEvent(category, action, String.format(label, arg))
class FaEvent(val name: String, val params: HashMap<String, Any> = hashMapOf()) : Event {
fun params(key: String, value: Any) : FaEvent {
params[key] = value
return this
}

fun withLabelArg(arg: Int): GaEvent {
return GaEvent(category, action, String.format(label, arg))
}

fun withCategory(category: String?): GaEvent {
return GaEvent(category?: "", action, label)
}

fun withAction(action: String): GaEvent {
return GaEvent(category, action, label)
fun withBundle(bundle: Bundle): FaEvent {
bundle.keySet().forEach { key ->
bundle[key]?.let {
params[key] = it
}
}
return this
}

fun toBundle(): Bundle {
fun bundle(): Bundle {
return Bundle().apply {
putString("category", category)
putString("action", action)
putString("label", label)
params.entries.forEach { entry ->
val value = entry.value
when (value) {
is Int -> putInt(entry.key, value)
is String -> putString(entry.key, value)
is Float -> putFloat(entry.key, value)
is Long -> putLong(entry.key, value)
is Boolean -> putBoolean(entry.key, value)
else -> {
if (BuildConfig.DEBUG) {
Log.e(
"FaEvent",
"params not supported: ${entry.key} => ${entry.value}"
)
}
}
}
}
}
}
}

fun Bundle.toGaEvent(): GaEvent? {
if (containsKey("action") && containsKey("label") && containsKey("category")) {
val action = getString("action", "")
val category = getString("category", "")
val label = getString("label", "")
if (!action.isEmpty() && !category.isEmpty() && !label.isEmpty()) {
return GaEvent(category, action, label)
fun parcel(): Bundle {
return Bundle().apply {
putString("name", name)
putBundle("data", bundle())
}
}
}

return null
fun Bundle.toFaEvent(): FaEvent? {
val name = getString("name", "").emptyAsNull() ?: return null
return FaEvent(name).withBundle(getBundle("data") ?: Bundle())
}

class ScreenView(val activity: Activity, val name: String, val classname: String): Event
140 changes: 140 additions & 0 deletions app/src/main/java/com/fenchtose/movieratings/analytics/ga/AppEvents.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.fenchtose.movieratings.analytics.ga

import com.fenchtose.movieratings.analytics.events.FaEvent
import com.fenchtose.movieratings.model.entity.Sort

class AppEvents {
companion object {

fun search(source: String) = FaEvent("search").params("source", source)

fun searchMore(page: Int, source: String) =
FaEvent("search_more").params("source", source).params("page", page)

fun clearSearch(source: String) = FaEvent("search_clear").params("source", source)
fun openMovie(source: String) = FaEvent("open_movie").params("source", source)
fun openSettings(source: String) = FaEvent("open_settings").params("source", source)
fun reportBug(source: String) = FaEvent("open_bug_report").params("source", source)
fun openPrivacyPolicy(source: String) =
FaEvent("privacy_policy").params("source", source)

fun rateApp(source: String) = FaEvent("rate_policy").params("source", source)
fun shareApp(source: String) = FaEvent("share_app").params("source", source)
fun openSupportApp(source: String) = FaEvent("open_support_app").params("source", source)
fun like(source: String?, status: Boolean) =
FaEvent(if (status) "like" else "unlike").params("source", source ?: "unknown")

fun selectTrendingTab(tab: String) = FaEvent("select_trending").params("tab", tab)
fun sort(order: Sort, source: String?) =
FaEvent("sort").params("order", order).params("source", source ?: "unknown")

fun openCollection(source: String?) =
FaEvent("collection_open").params("source", source ?: "unknown")

fun addToCollection(source: String?) =
FaEvent("collection_add").params("source", source ?: "unknown")

fun togglePlot(action: String, source: String?) =
FaEvent("toggle_plot").params("action", action).params("source", source ?: "unknown")

fun openImdb(source: String?) = FaEvent("imdb_open").params("source", source ?: "unknown")
fun startPurchase(sku: String) = FaEvent("purchase_start").params("sku", sku)
fun completePurchase(sku: String) = FaEvent("purchase_complete").params("sku", sku)

fun ratingNotFound(server: String) = FaEvent("rating_not_found").params("server", server)
fun showRating(source: String) = FaEvent("rating_shown").params("source", source)
fun sendNotification(source: String, type: String) =
FaEvent("notification_sent").params("source", source).params("type", type)
fun notificationBlocked(type: String) = FaEvent("notification_blocked").params("type", type)
fun openNotification(type: String) = FaEvent("notification_opened").params("type", type)
fun openMovieFromRatings(source: String) = FaEvent("rating_open").params("source", source)

fun showBanner(banner: String) = FaEvent("banner_show").params("banner", banner)
fun dismissBanner(banner: String) = FaEvent("banner_dismiss").params("banner", banner)
fun bannerTapped(banner: String) = FaEvent("banner_tapped").params("banner", banner)

val ACTIVATE_FLUTTER = FaEvent("activate_flutter")

val TAP_CREATE_COLLECTION = FaEvent("collection_new")
val CREATE_COLLECTION = FaEvent("collection_create")
val SHARE_COLLECTIONS = FaEvent("collection_share_multi")
val SELECT_COLLECTION = FaEvent("collection_select")
val DELETE_COLLECTION = FaEvent("collection_delete")

val REMOVE_MOVIE = FaEvent("collection_remove")
val SHARE_COLLECTION = FaEvent("collection_share")

val SELECT_SEASON = FaEvent("season_select")
val OPEN_EPISODE = FaEvent("episode_open")
val SELECT_EPISODE = FaEvent("episode_select")

val DISMISS_RATING = FaEvent("rating_dismiss")
val SPEAK_RATING = FaEvent("rating_tts")
}
}

class GaCategory {
companion object {
const val ACCESSIBILITY = "accessibility"
const val APP_INFO = "app info"
const val COLLECTION = "collection"
const val COLLECTION_LIST = "collection list"
const val COLLECTION_SEARCH = "collection search"
const val EPISODE = "episode"
const val LIKES = "likes"
const val MOVIE = "movie"
const val RECENTLY_BROWSED = "recently browsed"
const val SEARCH = "search"
const val SEASON = "season"
const val SERVICE = "service"
const val SETTINGS = "settings"
const val SUPPORT_APP = "support app"
const val TRENDING = "trending"
const val DEBUGGING = "debugging"
}
}

class AppScreens {
companion object {
const val ACCESS_INFO = "access info"
const val APP_INFO = "app info"
const val COLLECTION = "collection"
const val COLLECTION_LIST = "collection list"
const val COLLECTION_SEARCH = "collection search"
const val IMPORT_DATA = "import data"
const val LIKES = "likes"
const val MOVIE = "movie"
const val RECENTLY_BROWSED = "recently browsed"
const val SEARCH = "search"
const val SEASON = "season"
const val SETTINGS = "settings"
const val SETTINGS_APPS_SECTION = "settings apps section"
const val SETTINGS_DATA_SECTION = "settings data section"
const val SETTINGS_MISC_SECTION = "settings misc section"
const val SETTINGS_RATING_SECTION = "settings rating section"
const val SETTINGS_TTS_SECTION = "settings tts section"
const val SUPPORT_APP = "support app"
const val TRENDING = "trending"
const val DEBUGGING = "debugging"
const val BATTERY_OPTIMIZATION_INFO = "battery optimization info"
const val SETTINGS_NOTIFICATION_SECTION = "settings notification section"
}
}

class GaLabels {
companion object {
const val NOTIFICATION_SUPPORT_APP = "support app"
const val NOTIFICATION_RATE_APP = "rate app"
const val TOAST = "toast"
const val BUBBLE_BIG = "bubble big"
const val BUBBLE_SMALL = "bubble small"
const val OMDB_API = "omdb"
const val FLUTTER_API = "flutter"

const val ITEM_SEARCH = "search"
const val ITEM_PERSONAL = "personal"
const val ITEM_COLLECTIONS = "collections"
const val ITEM_INFO = "info"
const val RATING_404 = "rating 404"
}
}
Loading

0 comments on commit d382f7b

Please sign in to comment.