### Initialize RudderStack SDK and Add Facebook Integration in Kotlin Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/facebook/README.md Initialize the RudderStack SDK with your write key, application context, and data plane URL. Then, add the FacebookIntegration to the initialized SDK. This setup is typically done in your Application class. ```kotlin import com.rudderstack.integration.kotlin.facebook.FacebookIntegration import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add Facebook integration analytics.add(FacebookIntegration()) } } ``` -------------------------------- ### Install RudderStack Android SDK via Gradle Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Add the RudderStack Android SDK to your project by including the implementation dependency in your Gradle file. Replace '' with the desired SDK version found in the releases. ```kotlin dependencies { implementation("com.rudderstack.sdk.kotlin:android:") } ``` -------------------------------- ### Initialize RudderStack SDK with Firebase Integration (Kotlin) Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/firebase/README.md Initializes the RudderStack SDK with your write key and data plane URL, then adds the Firebase integration. This setup is typically done in your Application class. ```kotlin import com.rudderstack.integration.kotlin.firebase.FirebaseIntegration import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add Firebase integration analytics.add(FirebaseIntegration()) } } ``` -------------------------------- ### Install RudderStack JVM SDK via Gradle Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Integrate the RudderStack JVM SDK into your Kotlin JVM project by adding the core implementation dependency to your Gradle file. Ensure '' is updated to the current release version. ```kotlin dependencies { implementation("com.rudderstack.sdk.kotlin:core:") } ``` -------------------------------- ### Add Firebase Integration Dependencies (Kotlin) Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/firebase/README.md Installs the RudderStack Android SDK, the Firebase integration, and required Firebase dependencies. Ensure you replace `` with the appropriate version numbers. ```kotlin dependencies { // Add the RudderStack Android SDK implementation("com.rudderstack.sdk.kotlin:android:") // Add the Firebase integration implementation("com.rudderstack.integration.kotlin:firebase:") // Required Firebase dependencies implementation(platform("com.google.firebase:firebase-bom:")) implementation("com.google.firebase:firebase-analytics") } ``` -------------------------------- ### Automatic Navigation Tracking in Kotlin (Android) Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Automatically tracks navigation events in Android applications using NavController integration. Supports both Fragment-based and Compose-based navigation setups. Requires providing the NavController and Activity context. ```kotlin import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment import androidx.appcompat.app.AppCompatActivity // Fragment-based navigation class MainActivity : AppCompatActivity() { private lateinit var navController: NavController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navHostFragment = supportFragmentManager .findFragmentById(R.id.nav_host_fragment) as NavHostFragment navController = navHostFragment.navController // Enable automatic screen tracking for navigation analytics.setNavigationDestinationsTracking(navController, this) } } // Compose-based navigation import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.navigation.compose.rememberNavController @Composable fun MyApp(analytics: Analytics, activity: ComponentActivity) { val navController = rememberNavController() LaunchedEffect("navigation_tracking") { analytics.setNavigationDestinationsTracking(navController, activity) } NavHost(navController = navController, startDestination = "home") { composable("home") { HomeScreen() } composable("details") { DetailsScreen() } } } ``` -------------------------------- ### Initialize RudderStack SDK for Kotlin JVM Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Initializes the RudderStack SDK for server-side or Kotlin JVM applications. Requires a write key and data plane URL. Supports optional control plane URL and Gzip compression configuration. ```kotlin import com.rudderstack.sdk.kotlin.core.Analytics import com.rudderstack.sdk.kotlin.core.Configuration fun main() { val analytics = Analytics( configuration = Configuration( writeKey = "your_write_key_here", dataPlaneUrl = "https://your-dataplane-url.com", controlPlaneUrl = "https://api.rudderlabs.com", gzipEnabled = false ) ) // Use analytics instance analytics.track("Application Started") } ``` -------------------------------- ### Initialize RudderStack SDK for Android Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Initializes the RudderStack SDK for Android applications. Requires a write key, data plane URL, and application context. Configures application lifecycle event tracking, deep link tracking, and device ID collection. ```kotlin import android.app.Application import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() analytics = Analytics( configuration = Configuration( writeKey = "your_write_key_here", application = this, dataPlaneUrl = "https://your-dataplane-url.com", trackApplicationLifecycleEvents = true, trackDeepLinks = true, trackActivities = false, collectDeviceId = true ) ) } } ``` -------------------------------- ### Initialize RudderStack Android SDK Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Initialize the RudderStack Android SDK in your application's entry point, typically the 'onCreate' method. Provide your write key and data plane URL for configuration. This sets up the SDK for tracking events. ```kotlin import android.app.Application import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() initializeAnalytics(this) } private fun initializeAnalytics(application: Application) { analytics = Analytics( configuration = Configuration( writeKey = "", application = application, dataPlaneUrl = "", ) ) } } ``` -------------------------------- ### Initialize RudderStack SDK with Adjust Integration in Kotlin Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/adjust/README.md This code demonstrates how to initialize the RudderStack SDK and add the Adjust integration within your Android application's onCreate method. You need to provide your write key, application context, and data plane URL. The Adjust integration is added using `analytics.add(AdjustIntegration())`. ```kotlin import com.rudderstack.integration.kotlin.adjust.AdjustIntegration import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add Adjust integration analytics.add(AdjustIntegration()) } } ``` -------------------------------- ### Initialize RudderStack SDK with Integrations in Kotlin Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Demonstrates how to initialize the RudderStack SDK and add multiple integrations in a Kotlin Android application. Ensure the integration dependencies are added to your build.gradle.kts file before running this code. ```kotlin class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add integrations analytics.add(FirebaseIntegration()) analytics.add(BrazeIntegration()) // Add more integrations as needed } } ``` -------------------------------- ### Add Device-Mode Integrations in Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Integrates third-party analytics platforms using device-mode plugins within an Android application. Requires initializing the SDK with a write key, application context, and data plane URL. ```kotlin import com.rudderstack.integration.kotlin.firebase.FirebaseIntegration import com.rudderstack.integration.kotlin.braze.BrazeIntegration import com.rudderstack.integration.kotlin.appsflyer.AppsFlyerIntegration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize analytics analytics = Analytics( configuration = Configuration( writeKey = "your_write_key", application = this, dataPlaneUrl = "https://your-dataplane-url.com" ) ) // Add Firebase integration analytics.add(FirebaseIntegration()) // Add Braze integration analytics.add(BrazeIntegration()) // Add AppsFlyer integration analytics.add(AppsFlyerIntegration()) } } ``` -------------------------------- ### E-Commerce Event Tracking with Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Track standardized e-commerce events using predefined event names like PRODUCT_VIEWED, PRODUCT_ADDED, CHECKOUT_STARTED, and ORDER_COMPLETED. Supports detailed event properties. ```kotlin import com.rudderstack.sdk.kotlin.core.ecommerce.ECommerceEvents import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import kotlinx.serialization.json.putJsonArray import kotlinx.serialization.json.putJsonObject // Product viewed analytics.track( event = ECommerceEvents.PRODUCT_VIEWED, properties = buildJsonObject { put("product_id", "prod_123") put("name", "Wireless Headphones") put("price", 99.99) put("category", "Electronics") } ) // Product added to cart analytics.track( event = ECommerceEvents.PRODUCT_ADDED, properties = buildJsonObject { put("product_id", "prod_123") put("quantity", 1) put("price", 99.99) } ) // Checkout started analytics.track( event = ECommerceEvents.CHECKOUT_STARTED, properties = buildJsonObject { put("order_id", "ORD-789") put("value", 149.98) put("currency", "USD") putJsonArray("products") { addJsonObject { put("product_id", "prod_123") put("quantity", 1) } } } ) // Order completed analytics.track( event = ECommerceEvents.ORDER_COMPLETED, properties = buildJsonObject { put("order_id", "ORD-789") put("total", 149.98) put("revenue", 149.98) put("shipping", 10.00) put("tax", 12.50) put("currency", "USD") } ) ``` -------------------------------- ### Initialize RudderStack SDK with Braze Integration in Kotlin Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/braze/README.md Initialize the RudderStack SDK within your Application class and add the Braze integration. This sets up the connection to send event data to Braze. ```kotlin import com.rudderstack.integration.kotlin.braze.BrazeIntegration import com.rudderstack.sdk.kotlin.android.Analytics import com.rudderstack.sdk.kotlin.android.Configuration class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add Braze integration analytics.add(BrazeIntegration()) } } ``` -------------------------------- ### Create and Manage Custom Plugins in Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Extends SDK functionality by implementing custom plugins adhering to the Plugin interface. Supports pre-processing events, filtering, and enrichment. Plugins can be added and removed dynamically. ```kotlin import com.rudderstack.sdk.kotlin.core.internals.plugins.Plugin import com.rudderstack.sdk.kotlin.core.internals.models.Event import com.rudderstack.sdk.kotlin.core.Analytics class CustomEventFilterPlugin : Plugin { override val pluginType = Plugin.PluginType.PreProcess override lateinit var analytics: Analytics override suspend fun intercept(event: Event): Event? { // Filter out events based on custom logic if (event.type == "track" && event.name == "Spam Event") { return null // Drop this event } // Enrich event with custom data event.context?.let { context -> context["custom_flag"] = true context["processed_at"] = System.currentTimeMillis() } return event } override fun teardown() { // Cleanup resources } } // Add custom plugin to analytics analytics.add(CustomEventFilterPlugin()) // Remove plugin when needed val customPlugin = CustomEventFilterPlugin() analytics.add(customPlugin) // ... later analytics.remove(customPlugin) ``` -------------------------------- ### Initialize AppsFlyer and RudderStack SDKs (Kotlin) Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/appsflyer/README.md Initialize the AppsFlyer SDK in your Application class before initializing the RudderStack SDK. Then, add the AppsFlyerIntegration to the RudderStack SDK configuration. This ensures event data is correctly sent to AppsFlyer. ```kotlin import com.appsflyer.AppsFlyerLib import com.appsflyer.AFLogger class MyApplication : Application() { lateinit var analytics: Analytics override fun onCreate() { super.onCreate() // Initialize AppsFlyer SDK AppsFlyerLib.getInstance().init("", null, this) AppsFlyerLib.getInstance().setLogLevel(AFLogger.LogLevel.DEBUG) AppsFlyerLib.getInstance().start(this) // Initialize RudderStack SDK analytics = Analytics( configuration = Configuration( writeKey = "", application = this, dataPlaneUrl = "", ) ) // Add AppsFlyer integration analytics.add(AppsFlyerIntegration()) } } ``` -------------------------------- ### Java Compatibility for RudderStack SDK Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Integrate and use the RudderStack SDK from Java code using a provided compatibility layer. This allows Java applications to leverage the SDK's tracking, identify, and configuration features through a Java-friendly builder pattern. ```java import com.rudderstack.sdk.kotlin.android.javacompat.JavaAnalytics; import com.rudderstack.sdk.kotlin.android.javacompat.ConfigurationBuilder; import kotlinx.serialization.json.JsonObject; import kotlinx.serialization.json.JsonObjectBuilder; public class MyJavaApplication extends Application { private JavaAnalytics analytics; @Override public void onCreate() { super.onCreate(); // Initialize with Java-friendly builder analytics = new JavaAnalytics( new ConfigurationBuilder() .writeKey("your_write_key") .dataPlaneUrl("https://your-dataplane-url.com") .application(this) .trackApplicationLifecycleEvents(true) .build() ); // Track event from Java analytics.track("Button Clicked", builder -> { builder.put("button_name", "submit"); builder.put("screen", "checkout"); return null; }); // Identify user from Java analytics.identify("user_123", builder -> { builder.put("email", "user@example.com"); builder.put("name", "John Doe"); return null; }); } } ``` -------------------------------- ### Track Screen Views with Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Record screen view events to track user navigation within mobile applications. Supports basic screen names, categories, and custom properties. ```kotlin import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put // Track basic screen view analytics.screen(screenName = "Home Screen") // Track screen view with category analytics.screen( screenName = "Product Details", category = "Shopping" ) // Track screen view with properties analytics.screen( screenName = "Checkout", category = "Commerce", properties = buildJsonObject { put("cart_value", 150.50) put("item_count", 3) put("payment_method", "credit_card") } ) ``` -------------------------------- ### Add Adjust Integration Dependency to Kotlin Project Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/adjust/README.md This snippet shows how to add the necessary dependencies for the RudderStack Kotlin SDK and the Adjust integration to your Android project's build.gradle file. Ensure you replace `` with the actual latest version of the SDKs. ```kotlin dependencies { // Add the RudderStack Android SDK implementation("com.rudderstack.sdk.kotlin:android:") // Add the Adjust integration implementation("com.rudderstack.sdk.kotlin:adjust:") } ``` -------------------------------- ### Track User Actions with Properties Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Capture user actions and events using the 'track' API. This function allows you to record specific events along with relevant properties, such as revenue and currency for an 'Order Completed' event. ```kotlin analytics.track( event = "Order Completed", properties = buildJsonObject { put("revenue", 30) put("currency", "USD") } ) ``` -------------------------------- ### Configure Custom Flush Policies - Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Control the timing of event delivery to the server using custom flush policies. This includes flushing based on event count, time intervals, and upon SDK startup. GZIP compression for network requests can also be enabled. ```kotlin import com.rudderstack.sdk.kotlin.core.internals.policies.CountFlushPolicy import com.rudderstack.sdk.kotlin.core.internals.policies.FrequencyFlushPolicy import com.rudderstack.sdk.kotlin.core.internals.policies.StartupFlushPolicy val configuration = Configuration( writeKey = "your_write_key", application = this, dataPlaneUrl = "https://your-dataplane-url.com", flushPolicies = listOf( CountFlushPolicy(flushQueueSize = 50), // Flush when 50 events queued FrequencyFlushPolicy(flushIntervalInMillis = 60_000), // Flush every 60 seconds StartupFlushPolicy() // Flush on SDK startup ), gzipEnabled = true // Enable GZIP compression for network requests ) val analytics = Analytics(configuration) ``` -------------------------------- ### Track Custom Events with RudderStack SDK Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Tracks custom events with optional properties and options. Events can be simple strings or include structured JSON properties for detailed context. Supports custom RudderOption objects. ```kotlin import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import com.rudderstack.sdk.kotlin.core.internals.models.RudderOption // Track simple event analytics.track("Button Clicked") // Track event with properties analytics.track( event = "Order Completed", properties = buildJsonObject { put("order_id", "ORD-12345") put("revenue", 99.99) put("currency", "USD") put("items", 3) put("shipping_method", "express") } ) // Track event with custom options analytics.track( event = "Video Played", properties = buildJsonObject { put("video_id", "vid_789") put("duration", 120) put("quality", "HD") }, options = RudderOption() ) ``` -------------------------------- ### Session Management with Kotlin (Android) Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Manage user sessions, including automatic or manual session tracking and configuration of session timeouts. This applies to Android applications using the SDK. ```kotlin // Access current session ID val currentSessionId = analytics.sessionId // Manually start a new session analytics.startSession() // Start session with custom session ID analytics.startSession(sessionId = 1234567890123) // End current session analytics.endSession() // Configure session timeout in initialization val configuration = Configuration( writeKey = "your_write_key", application = this, dataPlaneUrl = "https://your-dataplane-url.com", sessionConfiguration = SessionConfiguration( automaticSessionTracking = true, sessionTimeoutInMillis = 300_000L // 5 minutes ) ) ``` -------------------------------- ### Add Braze Integration Dependencies to Kotlin Project Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/braze/README.md Include the RudderStack Android SDK and the Braze integration library in your project's dependencies. Ensure you use the latest versions for both. ```kotlin dependencies { // Add the RudderStack Android SDK implementation("com.rudderstack.sdk.kotlin:android:") // Add the Braze integration implementation("com.rudderstack.sdk.kotlin:braze:") } ``` -------------------------------- ### Add AppsFlyer Integration Dependencies (Kotlin) Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/appsflyer/README.md Add the RudderStack Android SDK, AppsFlyer integration, and AppsFlyer's Android SDK to your application's build.gradle.kts file. Ensure you use the latest compatible versions. ```kotlin dependencies { // Add the RudderStack Android SDK implementation("com.rudderstack.sdk.kotlin:android:") // Add the AppsFlyer integration implementation("com.rudderstack.integration.kotlin:appsflyer:") // AppsFlyer Android SDK implementation ("com.appsflyer:af-android-sdk:") } ``` -------------------------------- ### Identify Users with RudderStack SDK Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Associates users with a unique identifier and their traits. Supports identifying users by ID only, with both ID and traits, or anonymously with traits. Allows access to current user ID, traits, and anonymous ID. ```kotlin import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put // Identify with user ID only analytics.identify(userId = "user_123") // Identify with user ID and traits analytics.identify( userId = "user_123", traits = buildJsonObject { put("name", "Alex Keener") put("email", "alex@example.com") put("age", 28) put("plan", "premium") put("company", "Acme Inc") } ) // Identify with anonymous user (traits only) analytics.identify( traits = buildJsonObject { put("newsletter_subscribed", true) put("device_type", "mobile") } ) // Access user information val currentUserId = analytics.userId val currentTraits = analytics.traits val anonymousId = analytics.anonymousId ``` -------------------------------- ### Group Users with Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Associate users with specific groups or organizations. This function allows adding users to a group identified by `groupId` and can include custom traits. ```kotlin import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put // Add user to group analytics.group(groupId = "company_456") // Add user to group with traits analytics.group( groupId = "company_456", traits = buildJsonObject { put("name", "Acme Corporation") put("plan", "enterprise") put("employees", 500) put("industry", "Technology") put("website", "https://acme.com") } ) ``` -------------------------------- ### Alias User Identities with Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Merge multiple user identities into a single, unified profile. This is commonly used to link anonymous user activity to a registered user account. ```kotlin // Alias with new ID (uses previous user ID or anonymous ID automatically) analytics.alias(newId = "user_registered_123") // Alias with explicit previous ID analytics.alias( newId = "user_registered_123", previousId = "anonymous_xyz_789" ) // Typical use case: converting anonymous user to registered user // Step 1: Anonymous user tracking analytics.track("Product Viewed") // tracked with anonymous ID // Step 2: User registers analytics.alias(newId = "user_new_456") // links anonymous ID to registered ID // Step 3: Identify registered user analytics.identify( userId = "user_new_456", traits = buildJsonObject { put("email", "newuser@example.com") put("name", "New User") } ) ``` -------------------------------- ### Add Facebook Integration Dependency to Kotlin Project Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/integrations/facebook/README.md Add the RudderStack Android SDK and the Facebook integration dependency to your project's build.gradle file. This enables sending event data to Facebook. ```kotlin dependencies { // Add the RudderStack Android SDK implementation("com.rudderstack.sdk.kotlin:android:") // Add the Facebook integration implementation("com.rudderstack.sdk.kotlin:facebook:") } ``` -------------------------------- ### Identify User with Traits Source: https://github.com/rudderlabs/rudder-sdk-kotlin/blob/main/README.md Use the 'identify' API to associate user traits with a unique user ID. This function helps in recognizing and segmenting users based on their profile information. ```kotlin analytics.identify( userId = "1hKOmRA4el9Zt1WSfVJIVo4GRlm", traits = buildJsonObject { put("name", "Alex Keener") put("email", "alex@example.com") } ) ``` -------------------------------- ### Shutdown Analytics Instance - Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Properly shut down the analytics instance to clean up resources. This is an irreversible operation. All events queued before shutdown are persisted and will be flushed upon the next SDK initialization. This is often implemented in the application's termination lifecycle method for testing or specific scenarios. ```kotlin // Shutdown analytics (irreversible operation) analytics.shutdown() // Typical use case: Shutdown when no longer needed class MyApplication : Application() { private lateinit var analytics: Analytics override fun onTerminate() { super.onTerminate() // Note: onTerminate() is rarely called in production // but useful for testing analytics.shutdown() } } // All events queued before shutdown are written to disk // They will be flushed on next SDK initialization ``` -------------------------------- ### Reset User Identity in Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Clears user data and optionally generates a new anonymous ID. Supports selective data reset through ResetOptions and ResetEntries. Useful for managing user sessions and privacy. ```kotlin import com.rudderstack.sdk.kotlin.android.models.reset.ResetEntries import com.rudderstack.sdk.kotlin.android.models.reset.ResetOptions // Reset all user data (default behavior) analytics.reset() // Selective reset with custom options analytics.reset( options = ResetOptions( entries = ResetEntries( anonymousId = true, // Generate new anonymous ID userId = true, // Clear user ID traits = true, // Clear traits session = true // Refresh session (Android only) ) ) ) // Reset only specific data analytics.reset( options = ResetOptions( entries = ResetEntries( anonymousId = false, // Keep current anonymous ID userId = true, // Clear user ID traits = true, // Clear traits session = false // Keep current session ) ) ) ``` -------------------------------- ### Manually Flush Events - Kotlin Source: https://context7.com/rudderlabs/rudder-sdk-kotlin/llms.txt Force an immediate flush of queued events to the data plane. This is typically used before application termination or when specific timing is required. The SDK supports configuring automatic flush policies based on event count or time intervals. ```kotlin // Flush all pending events immediately analytics.flush() // Typical use case: Flush before app termination override fun onDestroy() { super.onDestroy() analytics.flush() } // Configure automatic flush policies import com.rudderstack.sdk.kotlin.core.internals.policies.CountFlushPolicy import com.rudderstack.sdk.kotlin.core.internals.policies.FrequencyFlushPolicy val configuration = Configuration( writeKey = "your_write_key", application = this, dataPlaneUrl = "https://your-dataplane-url.com", flushPolicies = listOf( CountFlushPolicy(flushQueueSize = 30), // Flush after 30 events FrequencyFlushPolicy(flushIntervalInMillis = 30_000) // Flush every 30 seconds ) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.