Installation
Add this code to your Android project to initialize Hardal and start sending events:
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
class HardalTracker {
companion object {
private const val TAG = "HardalTracker"
fun sendEvent(
hardalCustomUrl: String,
signalId: String,
eventName: String,
eventData: Map<String, Any> = emptyMap()
) {
GlobalScope.launch(Dispatchers.IO) {
try {
val url = URL("https://$hardalCustomUrl/push/hardal")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.doOutput = true
val payloadJson = JSONObject().apply {
put("name", eventName)
put("website", signalId)
put("data", JSONObject(eventData))
}
val bodyJson = JSONObject().apply {
put("type", "event")
put("payload", payloadJson)
}
connection.outputStream.use { os ->
os.write(bodyJson.toString().toByteArray())
}
val responseCode = connection.responseCode
if (responseCode in 200..299) {
Log.d(TAG, "Event sent successfully")
} else {
Log.e(TAG, "Failed to send event: $responseCode")
}
connection.disconnect()
} catch (e: Exception) {
Log.e(TAG, "Exception when sending event: ${e.message}", e)
}
}
}
}
}
Configuration Options
For Android integration, you can customize the following:
- Custom event properties
- Event batching
- Retry logic for failed requests
- Background thread management
Example Usage
HardalTracker.sendEvent(
hardalCustomUrl = "example.usehardal.com",
signalId = "cm5v9x9f80003tivvx7nr2bjh",
eventName = "purchase",
eventData = mapOf("productId" to "123", "price" to 99.99)
)
Troubleshooting
Validation
To validate your Android integration:
- Use the Logcat in Android Studio
- Monitor network requests in the network profiler
- Check for successful HTTP responses (200 status code)
- Verify events appear in your Hardal dashboard
Enable verbose logging in debug builds to get detailed information about event tracking and network requests.