To make it easier for you to add the Analytics SDK to your project, I've created an analytics library that can manage the SDKs and send events to them.
- Add 'Analytics' repository URL
allprojects {
repositories {
maven {
url "https://maven.pkg.github.com/DmitriyCanishev/AnalyticsLibrary"
credentials {
username = "GITHUB_USERNAME"
password = "GITHUB_TOKEN"
}
}
}
}- Add 'Analytics' dependency for Android or Unity projects
implementation ("com.analytics:base:+")- Add dependency for analytics sdk what you will need in projects, but first of all you need add Analytics dependency
implementation("com.analytics:appsflyer-sdk:+")
implementation("com.analytics:firebase-sdk:+")
implementation("com.analytics:appmetrica-sdk:+")For using Firebase Analytics you need add google-services.json to project folder:
- For Android: app/
- For Unity: Assets/
For using Firebase Analytics in Unity use Unity 2021 LTS or Later.
Other Analytics services work fine and on Unity 2020.
For Unity writes a separate class for manage analytics sdk services : UnityAnalyticsService
Example project in Unity to demonstrate using library(See)
- Declare variable of AnalyticsService
private lateinit var _analyticsService: AnalyticsService- Create instance of AnalyticsService
_analyticsService = AnalyticsService()- Create analytics sdk instances
val concreteAnalytics = IAnalyticsServiceImpl().also {
it.init(
activity,
"apiKey", // For FirebaseAnalytics use another Init method without this param
object : ServiceCallback { // This param has a default value, so you can skip it if you don't need it
override fun success() {
AnalyticsLogger.Logger.e("ConcreteAnalytics:", "Init Success")
}
override fun error(message: String?) {
AnalyticsLogger.Logger.e("ConcreteAnalytics:", "Init failed: $message")
}
}
)
}- Add Concrete Analytics SDK into a list to AnalyticsService for their managing
_analyticsService.init(listOf()) // Here, Example: listOf(appMetricaAnalytics, firebaseAnalytics)- For sending events to Analytics SDK
_analyticsService.logEvent(
context,
AnalyticsEvent(
eventName = "FirstCustomEvent",
params = mapOf("EventParam" to "EventParamValue")), //params:Map<String, Any>? can be null
object : ServiceCallback { // This param has a default value, so you can skip it if you don't need it
override fun success() {
AnalyticsLogger.Logger.e("Event:", "Send Success")
}
override fun error(message: String?) {
AnalyticsLogger.Logger.e("Event:", "Send failed: $message")
}
}
) // As parameter, use the model class 'AnalyticsEvent' which is suitable for most Analytics SDK-
Use a custom Logger to sort Logs by tag - "AnalyticsService"
-
Debug variants
AnalyticsLogger.Logger.d("$message")
AnalyticsLogger.Logger.d("$message", "$message")- Error variants
AnalyticsLogger.e("$message")
AnalyticsLogger.Logger.e("$message", "$message")- AppsFlyer : com.appsflyer:af-android-sdk:6.12.1
- Firebase : com.google.firebase:firebase-analytics:22.2.0
- AppMetrica : io.appmetrica.analytics:analytics:7.6.0