Skip to main content

Mobile application integration

Data can also be streamed from mobile applications, without the need for integrating an SDK. Additionally, streaming application events to your server allows you to remove measurement SDKs belonging to individual platforms, thereby improving performance.

Request Set Up

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.

Kotlin

Example of Kotlin request implementation
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

fun sendPostRequest(url: String, jsonBody: String) {
val client = OkHttpClient()
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("Response: ${response.body?.string()}")
}
} catch (e: IOException) {
e.printStackTrace()
}
}

Swift

Example of Swift request implementation
import Foundation

func sendPostRequest(urlString: String, jsonBody: String) {
import Foundation

func sendPostRequest(urlString: String, textBody: String) {

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()
}

Request Body

The request body is a JSON object with the following parameters: events and user. events is an array of objects containing parameters pertaining to individual events. user is an object consisting of parameters which are shared by the events being sent, most notably user identifiers such as IP address, user agent, or device ID.

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.

{
    "session": {
        "userAgent": "Mozilla/5.0 (iPhone; model M*; *) AppleWebKit/537.36...",
        "ip": "198.51.100.5",
        "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/ba.