Raincoat

Additional

Language
Kotlin
Version
N/A
Created
Dec 26, 2017
Updated
Dec 30, 2017
Owner
Matteo Crippa (matteocrippa)
Contributor
Matteo Crippa (matteocrippa)
1
Activity
Badge
Generate
Download
Source code

Announcement

???? Raincoat

Analytics abstraction layer for Kotlin inspired by Umbrella.

Table of Contents

Getting Started

As you can see it is really simple to log with different analytics with a simple function:

val analytics = Raincoat<MyEvent>()
analytics.register(FirebaseProvider(context))
analytics.register(MixpanelProvider(context))
analytics.log(MyEvent.SignUp("username"))

Example

Using Kotlin sealed class we can easily setup all things that we will have to track:

sealed class MyEvent : EventType {

    class SignUp(username: String) : MyEvent()
    class Register(username: String, password: String) : MyEvent()

    override fun name(provider: ProviderType): String? {
        return when (this) {
            is SignUp -> "signUp"
            is Register -> "register"
        }
    }

    override fun parameters(provider: ProviderType): HashMap<String, Any>? {
        return when (this) {
            is SignUp -> {
                hashMapOf()
            }
            is Register -> {
                hashMapOf("username" to this.username, "password" to this.password))
            }
        }
    }
}

Providers

At this very moment we developed the following built-in providers:

In order to use them, you have to add to your project the library of the provider, otherwise the build will fail.

Custom Provider

You can easily create your own providers, you can use this for reference:

class MixpanelProvider(private val context: Context, private val projectToken: String) : ProviderType {

    override var className = Class.forName("com.mixpanel.android.mpmetrics.MixpanelAPI")
    override var classInstance = className.getDeclaredMethod("getInstance", Context::class.java, String::class.java)
    override var classFunction = className.getDeclaredMethod("track", String::class.java, JSONObject::class.java)

    init {
        classInstance?.let {
            // get context
            try {
                it.invoke(className, context, projectToken)
            } catch (e: Exception) {
                Log.e("Raincoat", e.localizedMessage)
            }
        }
    }

    override fun log(eventName: String, parameters: HashMap<String, Any>?) {
        try {
            classFunction.invoke(classInstance, eventName, JSONObject(parameters))
        } catch (e: Exception) {
            Log.e("Raincoat", e.localizedMessage)
        }
    }
}

Installation

Add to your build.gradle:

allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
 }

then add the repository

compile 'com.github.matteocrippa:Raincoat:master-SNAPSHOT'

License

Raincoat is under MIT license. See the LICENSE file for more info.