All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.networking.analytics.AmplitudeClient.kt Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.deque.networking.analytics

import com.deque.networking.models.ResultCallAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

internal class AmplitudeClient(
    private val apiKey: String,
    private val baseUrl: String
) {
    private val amplitudeInterface: AmplitudeInterface =
        Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(
                MoshiConverterFactory.create(
                    Moshi.Builder()
                        .addLast(KotlinJsonAdapterFactory())
                        .build()
                )
            )
            .addCallAdapterFactory(ResultCallAdapterFactory())
            .build()
            .create(AmplitudeInterface::class.java)

    private val sessionId by lazy { System.currentTimeMillis() }

    suspend fun sendEvent(
        eventType: AmplitudeEventType,
        eventInfoProps: AmplitudeEventInfoProps? = null,
        eventProps: AmplitudeEventProps? = null
    ): Result {
        return amplitudeInterface.sendEvent(
            AmplitudeData(
                api_key = apiKey,
                events = listOf(
                    AmplitudeEvent(
                        user_id = AnalyticsService.distinctId,
                        event_type = eventType.eventName,
                        session_id = sessionId,
                        event_properties = eventProps,
                        os_name = eventInfoProps?.os_name,
                        os_version = eventInfoProps?.os_version,
                        device_brand = eventInfoProps?.device_brand,
                        device_manufacturer = eventInfoProps?.device_manufacturer,
                        device_model = eventInfoProps?.device_model,
                        time = eventInfoProps?.time ?: System.currentTimeMillis()
                    )
                )
            )
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy