Examples - mohitrajput987/analytics-sdk-android GitHub Wiki

We have prepared some examples for you to easily understand and implement this. If you haven't integrated our SDK and initialized tracker yet, read our Tutorial Document.

Mixpanel implementation

Here is the example of Mixpanel tracker implementation:

internal class MixpanelAnalyticsTracker(context: Context) : AnalyticsTracker {
    private val mixpanel: MixpanelAPI = MixpanelAPI.getInstance(context, "<PLACE_MIXPANEL_TOKEN>")

    override fun trackSuperProperties(analyticsProperty: AnalyticsProperty) {
        mixpanel.registerSuperPropertiesMap(analyticsProperty.properties)
    }

    override fun trackUser(user: AnalyticsUser) {
        if (user.isNewUser) {
            mixpanel.alias(user.identifier, mixpanel.distinctId)
        }
        mixpanel.identify(user.identifier)
        mixpanel.people.identify(user.identifier)
    }

    override fun trackUserProperties(analyticsProperty: AnalyticsProperty) {
        if (analyticsProperty.hasSystemProperties) {
            val properties = HashMap<String, Any>()
            properties.putAll(analyticsProperty.properties)
            val iterator = (analyticsProperty.properties as HashMap<String, Any>).iterator()
            while (iterator.hasNext()) {
                val entry = iterator.next()
                when (entry.key) {
                    SystemProperty.FirstName.propertyName -> {
                        properties["\$first_name"] = properties[entry.key] as Any
                        properties.remove(entry.key)
                    }
                    SystemProperty.LastName.propertyName -> {
                        properties["\$last_name"] = properties[entry.key] as Any
                        properties.remove(entry.key)
                    }
                    SystemProperty.Email.propertyName -> {
                        properties["\$email"] = properties[entry.key] as Any
                        properties.remove(entry.key)
                    }
                    SystemProperty.AndroidDevices.propertyName -> {
                        properties["\$android_devices"] = properties[entry.key] as Any
                        properties.remove(entry.key)
                    }
                }
            }
            mixpanel.people.setMap(properties)
        } else {
            mixpanel.people.setMap(analyticsProperty.properties)
        }
    }

    override fun trackUserPropertiesUnion(analyticsUnionProperty: AnalyticsUnionProperty) {
        mixpanel.people.union(
            analyticsUnionProperty.key,
            JSONArray(Gson().toJson(analyticsUnionProperty.values))
        )
    }

    override fun trackUserPropertiesOnce(analyticsProperty: AnalyticsProperty) {
        mixpanel.people.setOnceMap(analyticsProperty.properties)
    }

    override fun incrementUserProperty(analyticsIncrement: AnalyticsIncrement) {
        mixpanel.people.increment(analyticsIncrement.name, analyticsIncrement.countToIncrement)
    }

    override fun trackEvent(event: AnalyticsEvent) {
        mixpanel.trackMap(event.name, event.properties)
    }

    override fun clear() {
        mixpanel.flush()
        mixpanel.reset()
    }
}

Usage of methods

To track properties and events, you can use global object of default analytics service. Below is an example:

fun trackLogin() {
        AnalyticsApplication.analyticsService.trackEvent(AnalyticsEvent("log in"))

        val userOnceProps = HashMap<String, Any>()
        userOnceProps["First login"] = AnalyticsUtils.getUtcDate()
        AnalyticsApplication.analyticsService.trackUserPropertiesOnce(AnalyticsProperty(userOnceProps))

        val userProps = HashMap<String, Any>()
        userProps["Last login"] = AnalyticsUtils.getUtcDate()
        AnalyticsApplication.analyticsService.trackUserProperties(AnalyticsProperty(userProps))

        AnalyticsApplication.analyticsService.incrementUserProperty(AnalyticsIncrement("# of logins", 1.toDouble()))
    }

You can see detailed examples of code in this repo.