🧥 Analytics abstraction layer for Kotlin
Switch branches/tags
Nothing to show
Clone or download
Permalink
Failed to load latest commit information.
.idea fix badge Dec 30, 2017
app add appcenter provider Dec 26, 2017
gradle/wrapper base library Dec 26, 2017
.gitignore Initial commit Dec 26, 2017
LICENSE Initial commit Dec 26, 2017
README.md fix badge Dec 30, 2017
build.gradle first release Dec 26, 2017
gradle.properties base library Dec 26, 2017
gradlew base library Dec 26, 2017
gradlew.bat base library Dec 26, 2017
settings.gradle first release Dec 26, 2017

README.md

🧥 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.