Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUM-4295: Add methods to set user info, tracking consent and clear data #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.datadog.android.core.configuration.UploadFrequency as UploadFrequency
import com.datadog.android.privacy.TrackingConsent as TrackingConsentAndroid

actual object Datadog {

actual var verbosity: LogLevel?
get() = DatadogAndroid.getVerbosity().toLogLevel
set(value) = DatadogAndroid.setVerbosity(value.native)
Expand All @@ -34,6 +35,23 @@ actual object Datadog {
requireNotNull(context)
DatadogAndroid.initialize(context as Context, configuration.native, trackingConsent.native)
}

actual fun setTrackingConsent(consent: TrackingConsent) {
DatadogAndroid.setTrackingConsent(consent.native)
}

actual fun setUserInfo(
id: String?,
name: String?,
email: String?,
extraInfo: Map<String, Any?>
) {
DatadogAndroid.setUserInfo(id, name, email, extraInfo)
}

actual fun clearAllData() {
DatadogAndroid.clearAllData()
}
}

private val LogLevel?.native: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,33 @@ expect object Datadog {
configuration: Configuration,
trackingConsent: TrackingConsent
)

/**
* Sets the tracking consent regarding the data collection for this instance of the Datadog SDK.
*
* @param consent which can take one of the values
* ([TrackingConsent.PENDING], [TrackingConsent.GRANTED], [TrackingConsent.NOT_GRANTED])
*/
fun setTrackingConsent(consent: TrackingConsent)

/**
* Sets the user information.
*
* @param id (nullable) a unique user identifier (relevant to your business domain)
* @param name (nullable) the user name or alias
* @param email (nullable) the user email
* @param extraInfo additional information. An extra information can be
* nested up to 8 levels deep. Keys using more than 8 levels will be sanitized by SDK.
*/
fun setUserInfo(
id: String? = null,
name: String? = null,
email: String? = null,
extraInfo: Map<String, Any?> = emptyMap()
)

/**
* Clears all unsent data in all registered features.
*/
fun clearAllData()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* Copyright 2016-Present Datadog, Inc.
*/

@file:OptIn(ExperimentalForeignApi::class)

package com.datadog.kmp

import cocoapods.DatadogObjc.DDBatchProcessingLevelHigh
Expand All @@ -31,10 +29,10 @@ import com.datadog.kmp.core.configuration.BatchSize
import com.datadog.kmp.core.configuration.Configuration
import com.datadog.kmp.core.configuration.UploadFrequency
import com.datadog.kmp.privacy.TrackingConsent
import kotlinx.cinterop.ExperimentalForeignApi
import cocoapods.DatadogObjc.DDDatadog as DatadogIOS

actual object Datadog {

actual var verbosity: LogLevel?
get() = DatadogIOS.verbosityLevel().toLogLevel
set(value) = DatadogIOS.setVerbosityLevel(value.native)
Expand All @@ -47,6 +45,33 @@ actual object Datadog {
) {
DatadogIOS.initializeWithConfiguration(configuration.native, trackingConsent.native)
}

actual fun setTrackingConsent(consent: TrackingConsent) {
DatadogIOS.setTrackingConsentWithConsent(consent.native)
}

actual fun setUserInfo(
id: String?,
name: String?,
email: String?,
extraInfo: Map<String, Any?>
) {
DatadogIOS.setUserInfoWithId(
id,
name,
email,
extraInfo.mapKeys {
// in reality in ObjC it is extraInfo: [String: Any], but KMP generates the
// signature extraInfo: Map<kotlin.Any?, *>, erasing String type
@Suppress("USELESS_CAST")
it.key as Any
}
)
}

actual fun clearAllData() {
DatadogIOS.clearAllData()
}
}

private val LogLevel?.native: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.datadog.kmp.LogLevel
import com.datadog.kmp.core.configuration.Configuration
import com.datadog.kmp.privacy.TrackingConsent

@Suppress("MagicNumber")
fun initDatadog(context: Any? = null) {
Datadog.verbosity = LogLevel.DEBUG

Expand All @@ -20,4 +21,10 @@ fun initDatadog(context: Any? = null) {
).build()

Datadog.initialize(context = context, configuration = configuration, trackingConsent = TrackingConsent.GRANTED)

Datadog.setUserInfo(
name = "Random User",
email = "user@example.com",
extraInfo = mapOf("age" to 42, "location" to "universe")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ private fun Project.applyKotlinMultiplatformConfig(configExtension: DatadogBuild
iosArm64()
iosSimulatorArm64()

sourceSets.all {
if (name.startsWith("ios")) {
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
}
}

targets.all {
compilations.all {
kotlinOptions {
Expand Down