Skip to content

Instantly share code, notes, and snippets.

@tomdwipo
Created June 30, 2025 05:10
Show Gist options
  • Select an option

  • Save tomdwipo/602ab7524d37e04c985fffa9b36932de to your computer and use it in GitHub Desktop.

Select an option

Save tomdwipo/602ab7524d37e04c985fffa9b36932de to your computer and use it in GitHub Desktop.
logging analytics on kmm (kotlin multi platform)
/**
* Defines the interface for analytics logging.
*/
interface AnalyticsInterface {
/**
* Logs an event with the given name and parameters.
*
* @param eventName The name of the event to log.
* @param params A map of parameters associated with the event. Can be null.
*/
fun logEvent(eventName: String, params: Map<String, Any>?)
}
/**
* Provides the platform-specific implementation of [AnalyticsInterface].
*/
expect fun getAnalytics(): AnalyticsInterface
import FirebaseAnalytics
/**
* `FirebaseLoggingCallback` is an iOS-specific implementation of `FirebaseIosCallback`
* from the shared module. It provides functionality to log events to Firebase Analytics.
*/
class FirebaseLoggingCallback: FirebaseIosCallback {
/**
* Logs a Firebase event with the given event ID and parameters.
*
* @param eventId The ID of the event to log.
* @param params A string containing key-value pairs of parameters, separated by commas and colons (e.g., "key1:value1,key2:value2").
*/
func logEvent(eventId: String, params: String) {
let dict = splitStringToDictionary(params, ",", ":")
Analytics.logEvent(eventId, parameters: dict)
}
/**
* Splits a string into a dictionary based on provided delimiters.
*
* @param input The input string to split.
* @param pairDelimiter The character used to separate key-value pairs.
* @param keyValueDelimiter The character used to separate keys and values within a pair.
* @return A dictionary containing the parsed key-value pairs.
*/
func splitStringToDictionary(_ input: String, _ pairDelimiter: Character, _ keyValueDelimiter: Character) -> [String: String] {
var result = [String: String]()
let pairs = input.split(separator: pairDelimiter)
for pair in pairs {
let keyValueArray = pair.split(separator: keyValueDelimiter, maxSplits: 1).map { String($0) }
if keyValueArray.count == 2 {
let key = keyValueArray[0].trimmingCharacters(in: .whitespacesAndNewlines)
let value = keyValueArray[1].trimmingCharacters(in: .whitespacesAndNewlines)
result[key] = value
}
}
return result
}
}
import UIKit
import shared
import FirebaseCore
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
AnalyticsFirebaseKotlinKt.firebaseCallback(callback: FirebaseLoggingCallback())
window = UIWindow(frame: UIScreen.main.bounds)
let mainViewController = KotlinController.companion.mainViewController()
window?.rootViewController = mainViewController
window?.makeKeyAndVisible()
return true
}
}
/**
* iOS-specific implementation of `AnalyticsInterface` for Firebase Analytics.
* This class provides methods to log events to Firebase on iOS platforms.
*/
class AnalyticsFirebaseKotlin : AnalyticsInterface {
/**
* Logs an event to Firebase Analytics for iOS.
*
* @param eventName The name of the event to log.
* @param params A map of parameters to associate with the event.
*/
override fun logEvent(
eventName: String,
params: Map<String, Any>?
) {
firebaseIosCallback?.logEvent(eventName, params.toString())
}
private fun Map<String, Any>?.toString(): String {
if (this == null) return ""
val sb = StringBuilder()
this.forEach {
sb.append("${it.key}:${it.value},")
}
return sb.toString()
}
}
/**
* Provides the iOS-specific implementation of [AnalyticsInterface].
*/
actual fun getAnalytics(): AnalyticsInterface = AnalyticsFirebaseKotlin()
/**
* Interface for Firebase iOS callbacks.
*/
interface FirebaseIosCallback {
/**
* Logs an event to Firebase.
*
* @param eventId The ID of the event.
* @param params The parameters of the event as a string.
*/
fun logEvent(eventId: String, params: String)
}
private var firebaseIosCallback: FirebaseIosCallback? = null
/**
* Sets the Firebase iOS callback.
*
* @param callback The callback to be set.
*/
@Suppress("unused")
fun firebaseCallback(callback: FirebaseIosCallback) {
firebaseIosCallback = callback
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment