Last active
December 28, 2021 02:28
-
-
Save jimlyas/fa43c6370eb4f676390aa9ef004be294 to your computer and use it in GitHub Desktop.
Gist for Analytics Medium
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
val analyticsModule = module { | |
single<BaseAnalytics> { get<Context>().getAnalytics() } | |
} |
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
abstract class BaseAnalytics { | |
abstract fun logEvent(eventName: String, params: Bundle?) | |
abstract fun setUserProperty(name: String, value: String) | |
abstract fun setUserId(id: String) | |
abstract fun clearData() | |
abstract fun instanceId(listener: (String) -> Unit) | |
} |
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
fun Context.getAnalytics(): BaseAnalytics = | |
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { | |
GMSAnalytics() | |
} else { | |
HMSAnalytics() | |
} |
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
class GMSAnalytics : BaseAnalytics() { | |
private val analytics = Firebase.analytics | |
override fun logEvent(eventName: String, params: Bundle?) { | |
analytics.logEvent(eventName, params) | |
} | |
override fun setUserProperty(name: String, value: String) { | |
analytics.setUserProperty(name, value) | |
} | |
override fun setUserId(id: String) { | |
analytics.setUserId(id) | |
} | |
override fun clearData() { | |
analytics.resetAnalyticsData() | |
} | |
override fun instanceId(listener: (String) -> Unit) { | |
analytics.appInstanceId.addOnCompleteListener { if (it.isSuccessful) listener.invoke(it.result) } | |
} | |
} |
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
class HMSAnalytics(ctx : Context) : BaseAnalytics() { | |
private val analytics = HiAnalytics.getInstance(ctx) | |
override fun logEvent(eventName: String, params: Bundle?) { | |
analytics.onEvent(eventName, params) | |
} | |
override fun setUserProperty(name: String, value: String) { | |
analytics.setUserProfile(name, value) | |
} | |
override fun setUserId(id: String) { | |
analytics.setUserId(id) | |
} | |
override fun clearData() { | |
analytics.clearCachedData() | |
} | |
override fun instanceId(listener: (String) -> Unit) { | |
analytics.aaid.addOnCompleteListener { if (it.isSuccessful) listener.invoke(it.result) } | |
} | |
} |
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
private val analytics by inject<BaseAnalytics>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment