Skip to main content

Mobile application integration

Data can be streamed from mobile applications without the need for integrating an SDK. Streaming application events to mHub Cloud allows you to remove measurement SDKs belonging to individual platforms, thereby improving performance. Events should be bucketed and sent to the server in larger batches, rather than individually, to ensure optimal performance.

Request setup

Below are Kotlin and Swift examples showing how to implement the request in a mobile application. The request sent to the collect API endpoint is a POST request containing one or more events in the payload.

Request body

The request body is a JSON object with the following parameters: events and device. events is an array of objects containing individual events. device is an object consisting of parameters which are shared by the events being sent, most notably attributes such as user or device ID. User-Agent and IP addresses will be collected automatically.

The structure of each event may vary significantly depending on the type of data being collected. A general minimal example of a submitted event can be found here. Nevertheless, at minimum, the event name must always be identified in event.code, and an event timestamp must be provided.

{
    "device": {
        "deviceId": "2b5227e1-545e-4693-96d2-cb13b27c597b"
    },
    "events": [
        {
            "event": {
                "code": "button_click"
            },
            ...
            "timestamp": 1769510343000
        },
        {...}
    ]
}

Endpoint

The endpoint uses the domain which you set up during instance initialisation (see Initialising the collect function). For example, if your collection domain is example.com, the endpoint your request should call will be mhubc.example.com/api/se.

Implementation examples

Kotlin

Example of Kotlin request implementation
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException

fun sendPostRequest(domain: String, jsonBody: String) {
val client = OkHttpClient()

val url = "https://mhubc.$domain/api/se"

val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
val body = jsonBody.toRequestBody(mediaType)

val request = Request.Builder()
.url(url)
.post(body)
.build()

try {
client.newCall(request).execute().use { response ->
println("HTTP Status Code: ${response.code}")
println("Response: ${response.body?.string()}")
}
} catch (e: IOException) {
println("Request failed with error: ${e.localizedMessage}")
e.printStackTrace()
}
}

Swift

Example of Swift request implementation
import Foundation

func sendPostRequest(domain: String, textBody: String) {
let urlString = "https://mhubc.\(domain)/api/se"

guard let url = URL(string: urlString) else {
print("Invalid URL")
return
}

var request = URLRequest(url: url)
request.httpMethod = "POST"

request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

request.httpBody = textBody.data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Request failed with error: \(error.localizedDescription)")
return
}

if let httpResponse = response as? HTTPURLResponse {
print("HTTP Status Code: \(httpResponse.statusCode)")
}

if let data = data,
let responseString = String(data: data, encoding: .utf8) {
print("Response: \(responseString)")
}
}

task.resume()
}