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

// Hardal event tracking implementation for Android (Kotlin)
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 {
                    // Create URL
                    val url = URL("https://$hardalCustomUrl/push/hardal")
                    
                    // Open connection
                    val connection = url.openConnection() as HttpURLConnection
                    connection.requestMethod = "POST"
                    connection.setRequestProperty("Content-Type", "application/json")
                    connection.doOutput = true
                    
                    // Create payload
                    val payloadJson = JSONObject().apply {
                        put("name", eventName)
                        put("website", signalId)
                        put("data", JSONObject(eventData))
                    }
                    
                    // Create body
                    val bodyJson = JSONObject().apply {
                        put("type", "event")
                        put("payload", payloadJson)
                    }
                    
                    // Write body to connection
                    connection.outputStream.use { os ->
                        os.write(bodyJson.toString().toByteArray())
                    }
                    
                    // Get response
                    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

// 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:

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

Enable verbose logging in debug builds to get detailed information about event tracking and network requests.