Android - Kotlin
Use okhttp3 to Send HTTP in Kotlin
Add Internet Permission
In AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Create a class EvntalyClient
EvntalyClient
import okhttp3.*
import org.json.JSONObject
import java.io.IOException
class EvntalyClient(private val projectToken: String, private val developerSecret: String) {
private val baseUrl = "https://app.evntaly.com/prod"
private val client = OkHttpClient()
}
Track Event
fun sendEvent() {
val url = "$baseUrl/api/v1/register/event"
val payload = JSONObject().apply {
put("title", "Account License Upgraded")
put("description", "User upgraded their license to Pro")
put("message", "take care.")
put("data", JSONObject().apply {
put("user_id", "67890")
put("timestamp", "2025-01-08T09:30:00Z")
put("referrer", "social_media")
put("email_verified", false)
})
put("tags", JSONArray())
put("notify", true)
put("icon", "⤴️")
put("apply_rule_only", false)
put("user", JSONObject().apply {
put("id", "0f6934fd-99c0-41ca-84f4")
})
put("type", "Page View")
put("sessionID", "20750ebc-dabf-4fd4-9498-443bf30d6095_bsd")
put("feature", "Backend API")
put("topic", "@api")
}
val body = RequestBody.create("application/json".toMediaTypeOrNull(), payload.toString())
val request = Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("pat", projectToken)
.addHeader("secret", developerSecret)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
println("Event send failed: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
println("Event sent: ${response.isSuccessful}")
}
})
}
Identify User
fun identifyUser() {
val url = "$baseUrl/api/v1/register/user"
val payload = JSONObject().apply {
put("id", "0f6934fd-99c0-41ca-3333")
put("email", "Alameer@evntaly.com")
put("full_name", "Alameer Ashraf")
put("organization", "Evntaly")
put("data", JSONObject().apply {
put("id", "Alameer")
put("email", "Alameer@Decaud.com")
put("Location", "Egypt")
put("timezone", "Africa/Cairo")
})
}
val body = RequestBody.create("application/json".toMediaTypeOrNull(), payload.toString())
val request = Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("pat", projectToken)
.addHeader("secret", developerSecret)
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
println("User identify failed: ${e.message}")
}
override fun onResponse(call: Call, response: Response) {
println("User identified: ${response.isSuccessful}")
}
})
}
For additional help, contact support at support@evntaly.com.
Last updated