Skip to main content

Analytics

Overview

The LikeMinds SDK provides a set of predefined user events that you might want to track for your chat application.

Refer to Analytics Events to see the list of events that are being tracked.

Step-by-Step Guide

1. Create Your Custom Callback Class

  1. Create a new Swift file in your project (e.g., LMChatCoreAnalyticsCallback.swift).
  2. Import the modules you need (LikeMindsChatCore).
  3. Conform to LMChatCoreCallback.
  4. Implement any required methods—especially onEventTriggered for analytics.
import Foundation
import LikeMindsChatCore

/// A custom class that implements LMChatCoreCallback to handle analytics events.
class MyCoreCallback: LMChatCoreCallback {

// Called when the access token has expired and been successfully refreshed.
func onAccessTokenExpiredAndRefreshed(accessToken: String, refreshToken: String) {
// Handle updated tokens if necessary.
}

// Called when the refresh token has expired.
func onRefreshTokenExpired(
_ completionHandler: (((accessToken: String, refreshToken: String)?) -> Void)?
) {
// Provide new tokens or handle logout flow.
}

// Called when a user clicks on a profile within LMChat.
func userProfileViewHandle(withRoute route: String) {
// Handle your navigation to the user's profile screen if needed.
}

// MARK: - Main Analytics Method
// Called when an analytics event is triggered in LMChat.
func onEventTriggered(
eventName: LMChatAnalyticsEventName,
eventProperties: [String: AnyHashable]
) {
// Your custom event handling here:
print("[MyCoreCallback] Event Triggered: \(eventName.rawValue)")
print("[MyCoreCallback] Properties: \(eventProperties)")

// Forward to your analytics provider, e.g.:
// Analytics.logEvent(eventName.rawValue, parameters: eventProperties)
}
}

2. Register the Callback in Your App

To ensure that LMChat uses your new custom class, set your class as the callback on LMChatCore.shared. You can do this anywhere you set up your chat environment. A common approach is in your AppDelegate or SceneDelegate, but you could do it in your own initialization manager as well—wherever your code initializes LMChat.

Example in AppDelegate or Initialization Flow

import UIKit
import LikeMindsChatCore

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {

// 1. Initialize LMChatCore
LMChatCore.shared.setupChat(deviceId: nil)

// 2. Create an instance of your custom callback
let myCallback = MyCoreCallback()

// 3. Register it with LMChatCore
LMChatCore.shared.setCallback(myCallback)

return true
}
}