Goals
- Add & remove analytics services quickly
- Add & remove events quickly
- Able to integrate quickly into any project
Analytics Market
Increasingly, organizations have stringent requirements from the time data is generated, to the time actionable insights are delivered to the users. Therefore, data needs to be collected, stored, processed, and analyzed within relatively short windows — ranging from daily to real-time.
Do you know who your users are? What motivates them to buy? What will make them uninstall your app? Growing your audience requires having the tools and process to answer these questions.
Top 5 data trends that shaped mobile app marketing in 2020 |
onurakpolat/awesome-analytics |
First, we can define Analytics.kt interface which has only one method, a track that receives EventBuilder.()-> but what it means?
Note that the class AnalyticsImpl doesn’t need to receive Android context, only a list of Tracker objects.
Simple Tracker interface
How to init your analytics inside Android Application class
There are a couple of ways to make Analytics a singleton, and it’ll rely on how big is your project and the DI mechanism.
DI With Koin
DI Using Singleton Pattern
Kotlin singletons with argument |
How to create a singleton class in Kotlin? |
Is noteworthy that we can use the same strategy of hiding the implementation behind an interface (Facade) and compose different behaviors like:
- HTTP Dispatcher with OkHttp or Retrofit or Ktor
- Crash Report with Crashlytics or BugSnag or AppCenter)
Simple CrashReporter interface which implements Tracker as well
- Logging(Firebase,New Relic, ELK, Snowplow)
Remote logging with Timber and Firebase Realtime Database. |
AndroidLogTracker which does logging to Logcat console using Android Log
Generic Unique Api Key
Note the use of UUID.randomUUID() , I’ve shared a couple of ideas regarding that subject here , please checkout and give your feedback
Sealed Classes and Hierarchies
Events as simple data classes and sealed class hierarchy is a good fit once the same approach drives MVVM in regular Android apps
Trackable interface, Event sealed class with EventTrack/CrashTrack children and EventName using StringDef constants
Lambda With Receivers / Builders Kotlin / Apply/ Extensions
Builder class using @DslAnnotation and a couple of syntax sugars
Sample usage inside your project
Conclusion
The complete solution is pretty much a couple of tiny files, that you can check out here.
After adding a few simple Kotlin files, when you need to remove or add “yet another manager tracking tool” you just need to add the dependency to build.gradle file and create a class that implements Tracker and implements it accordingly to the tracker documentation.
Like the library? see my profile and give me a clap on medium and a star on GitHub. :)
https://www.linkedin.com/in/williamgouvea/
Last but not least, Comments/Suggestions are welcome as always. Keep learning and keep sharing.
References
- https://github.com/sofakingforever/solid-kotlin-analytics
- https://github.com/amsterdatech/snitch
- https://proandroiddev.com/basic-android-app-analytics-in-60min-cdd1ac04356b
- https://medium.com/@Actine/organize-your-analytics-code-be9e5ff91543
- https://medium.com/wantedly-engineering/better-analytics-in-android-with-annotation-processing-and-kotlinpoet-bffca3f24c37
- https://medium.com/@nadavfima/how-to-build-better-analytics-with-kotlin-60ab50ce25ac
- https://proandroiddev.com/anatomy-of-analytics-from-google-e107fff107ab
- https://movile.blog/automatizando-a-geracao-de-eventos-de-analytics/