Installation

Add this code to your Android project to initialize Hardal and start sending events:

import org.json.JSONObject
import java.net.URL
import javax.net.ssl.HttpsURLConnection

// Simple Hardal track implementation for Android
fun trackHardalEvent(signalId: String, eventName: String, eventData: Map<String, Any>) {
    Thread {
        try {
            val url = URL("https://$signalId-signal.usehardal.com/push/hardal")
            val connection = url.openConnection() as HttpsURLConnection
            
            connection.requestMethod = "POST"
            connection.setRequestProperty("Content-Type", "application/json")
            connection.doOutput = true
            
            // Create payload structure
            val payload = JSONObject().apply {
                put("website", signalId) // Must be provided
                put("data", JSONObject(eventData))
            }
            
            // Create body with required fields
            val body = JSONObject().apply {
                put("event_name", eventName)
                put("type", "event") // Must be provided
                put("payload", payload)
            }
            
            // Send the request
            connection.outputStream.use { os ->
                os.write(body.toString().toByteArray())
            }
            
            connection.responseCode // Get response but do nothing with it
            
        } catch (e: Exception) {
            // Silent failure - no error handling
        }
    }.start()
}

// Usage:
// val signalId = "your-hardal-signal-id"
// val eventName = "purchase" // Or any name you would like to use
// 
// // You can add any data here
// val eventData = mapOf<String, Any>()
//
// trackHardalEvent(signalId, eventName, eventData)

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

// Simple example of tracking a purchase event with Hardal in Android
val signalId = "your-hardal-signal-id"
val eventName = "purchase"

// Example purchase data
val purchaseData = mapOf(
    "transaction_id" to "TX-12345",
    "currency" to "TRY",
    "value" to 149.99,
    "items" to listOf(
        mapOf(
            "product_id" to "SKU001",
            "name" to "Premium Headphones",
            "price" to 149.99,
            "quantity" to 1
        )
    )
)

// Track the event
Thread {
    try {
        val url = URL("https://$signalId-signal.usehardal.com/push/hardal")
        val connection = url.openConnection() as HttpsURLConnection
        
        connection.requestMethod = "POST"
        connection.setRequestProperty("Content-Type", "application/json")
        connection.doOutput = true
        
        val payload = JSONObject().apply {
            put("website", signalId)
            put("data", JSONObject(purchaseData))
        }
        
        val body = JSONObject().apply {
            put("event_name", eventName)
            put("type", "event")
            put("payload", payload)
        }
        
        connection.outputStream.use { os ->
            os.write(body.toString().toByteArray())
        }
        
        connection.responseCode
        
    } catch (e: Exception) {
        // Silent failure
    }
}.start()

Troubleshooting

Validation

To validate your Android integration:

  1. Monitor Logcat in Android Studio
  2. Use the Network Profiler to inspect requests
  3. Check for successful HTTP responses (200 status code)
  4. Verify events appear in your Hardal dashboard

For development, you can enable detailed logging by setting the log level to VERBOSE in your application’s debug configuration.