Skip to content

Commit 0249aa8

Browse files
authored
Merge pull request #2768 from DataDog/maciey/RUM-10621/clear-user-info
RUM-10621 Add clear user info API
2 parents 2e51912 + 4203b5c commit 0249aa8

File tree

18 files changed

+225
-17
lines changed

18 files changed

+225
-17
lines changed

dd-sdk-android-core/api/apiSurface

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ object com.datadog.android.Datadog
1010
fun setUserInfo(String, String? = null, String? = null, Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
1111
DEPRECATED fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
1212
fun addUserProperties(Map<String, Any?>, com.datadog.android.api.SdkCore = getInstance())
13+
fun clearUserInfo(com.datadog.android.api.SdkCore = getInstance())
1314
fun clearAllData(com.datadog.android.api.SdkCore = getInstance())
1415
fun setAccountInfo(String, String? = null, Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
1516
fun addAccountExtraInfo(Map<String, Any?> = emptyMap(), com.datadog.android.api.SdkCore = getInstance())
@@ -60,6 +61,7 @@ interface com.datadog.android.api.SdkCore
6061
fun setTrackingConsent(com.datadog.android.privacy.TrackingConsent)
6162
fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap())
6263
fun addUserProperties(Map<String, Any?>)
64+
fun clearUserInfo()
6365
fun clearAllData()
6466
fun setAccountInfo(String, String? = null, Map<String, Any?> = emptyMap())
6567
fun addAccountExtraInfo(Map<String, Any?> = emptyMap())

dd-sdk-android-core/api/dd-sdk-android-core.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public final class com/datadog/android/Datadog {
2525
public static final fun clearAllData ()V
2626
public static final fun clearAllData (Lcom/datadog/android/api/SdkCore;)V
2727
public static synthetic fun clearAllData$default (Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
28+
public static final fun clearUserInfo ()V
29+
public static final fun clearUserInfo (Lcom/datadog/android/api/SdkCore;)V
30+
public static synthetic fun clearUserInfo$default (Lcom/datadog/android/api/SdkCore;ILjava/lang/Object;)V
2831
public static final fun getInstance ()Lcom/datadog/android/api/SdkCore;
2932
public static final fun getInstance (Ljava/lang/String;)Lcom/datadog/android/api/SdkCore;
3033
public static synthetic fun getInstance$default (Ljava/lang/String;ILjava/lang/Object;)Lcom/datadog/android/api/SdkCore;
@@ -135,6 +138,7 @@ public abstract interface class com/datadog/android/api/SdkCore {
135138
public abstract fun addUserProperties (Ljava/util/Map;)V
136139
public abstract fun clearAccountInfo ()V
137140
public abstract fun clearAllData ()V
141+
public abstract fun clearUserInfo ()V
138142
public abstract fun getName ()Ljava/lang/String;
139143
public abstract fun getService ()Ljava/lang/String;
140144
public abstract fun getTime ()Lcom/datadog/android/api/context/TimeInfo;

dd-sdk-android-core/src/main/kotlin/com/datadog/android/Datadog.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,32 @@ object Datadog {
305305
sdkCore.addUserProperties(extraInfo)
306306
}
307307

308+
/**
309+
* Clear the current user information.
310+
*
311+
* User information will be set to null.
312+
* After calling this api, Logs, Traces, RUM Events will not include the user information anymore.
313+
*
314+
* Any active RUM Session, active RUM View at the time of call will have their `usr` attribute cleared.
315+
*
316+
* If you want to retain the current `usr` on the active RUM session,
317+
* you need to stop the session first by using `GlobalRumMonitor.get().stopSession()`
318+
*
319+
* If you want to retain the current `usr` on the active RUM views,
320+
* you need to stop the view first by using `GlobalRumMonitor.get().stopView()`.
321+
*
322+
* @param sdkCore SDK instance to clear user info. If not provided,
323+
* default SDK instance will be used.
324+
*/
325+
@JvmStatic
326+
@JvmOverloads
327+
@AnyThread
328+
fun clearUserInfo(
329+
sdkCore: SdkCore = getInstance()
330+
) {
331+
sdkCore.clearUserInfo()
332+
}
333+
308334
/**
309335
* Clears all unsent data in all registered features.
310336
*

dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ interface SdkCore {
7676
@AnyThread
7777
fun addUserProperties(extraInfo: Map<String, Any?>)
7878

79+
/**
80+
* Clear the current user information.
81+
*
82+
* User information will be set to null.
83+
* After calling this api, Logs, Traces, RUM Events will not include the user information anymore.
84+
*
85+
* Any active RUM Session, active RUM View at the time of call will have their `usr` attribute cleared.
86+
*
87+
* If you want to retain the current `usr` on the active RUM session,
88+
* you need to stop the session first by using `GlobalRumMonitor.get().stopSession()`
89+
*
90+
* If you want to retain the current `usr` on the active RUM views,
91+
* you need to stop the view first by using `GlobalRumMonitor.get().stopView()`
92+
*/
93+
@AnyThread
94+
fun clearUserInfo()
95+
7996
/**
8097
* Clears all unsent data in all registered features.
8198
*/

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/DatadogCore.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ internal class DatadogCore(
176176
coreFeature.userInfoProvider.addUserProperties(extraInfo)
177177
}
178178

179+
/** @inheritDoc */
180+
@AnyThread
181+
override fun clearUserInfo() {
182+
coreFeature.userInfoProvider.clearUserInfo()
183+
}
184+
179185
/** @inheritDoc */
180186
@AnyThread
181187
override fun clearAllData() {

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/NoOpInternalSdkCore.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ internal object NoOpInternalSdkCore : InternalSdkCore {
8888

8989
override fun addUserProperties(extraInfo: Map<String, Any?>) = Unit
9090

91+
override fun clearUserInfo() = Unit
92+
9193
override fun clearAllData() = Unit
9294

9395
override fun setAccountInfo(

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/user/DatadogUserInfoProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ internal class DatadogUserInfoProvider(
4141
)
4242
}
4343

44+
override fun clearUserInfo() {
45+
internalUserInfo = UserInfo(internalUserInfo.anonymousId)
46+
}
47+
4448
override fun getUserInfo(): UserInfo {
4549
return internalUserInfo
4650
}

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/user/MutableUserInfoProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ internal interface MutableUserInfoProvider : UserInfoProvider {
2121
fun setAnonymousId(id: String?)
2222

2323
fun addUserProperties(properties: Map<String, Any?>)
24+
25+
fun clearUserInfo()
2426
}

dd-sdk-android-core/src/test/kotlin/com/datadog/android/DatadogTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ internal class DatadogTest {
540540
verify(mockSdkCore).addUserProperties(fakeUserProperties)
541541
}
542542

543+
@Test
544+
fun `M clear user info W clearUserInfo()`() {
545+
// Given
546+
val mockSdkCore = mock<SdkCore>()
547+
548+
// When
549+
Datadog.clearUserInfo(mockSdkCore)
550+
551+
// Then
552+
verify(mockSdkCore).clearUserInfo()
553+
}
554+
543555
@Test
544556
fun `M clear all data W clearAllData()`() {
545557
// Given

dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/DatadogCoreTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ internal class DatadogCoreTest {
291291
)
292292
}
293293

294+
@Test
295+
fun `M clear user info W clearUserInfo() is called`() {
296+
// Given
297+
val mockUserInfoProvider = mock<MutableUserInfoProvider>()
298+
testedCore.coreFeature.userInfoProvider = mockUserInfoProvider
299+
300+
// When
301+
testedCore.clearUserInfo()
302+
303+
// Then
304+
verify(mockUserInfoProvider).clearUserInfo()
305+
}
306+
294307
@Test
295308
fun `M update accountInfoProvider W setAccountInfo()`(
296309
@StringForgery(type = StringForgeryType.HEXADECIMAL) id: String,

0 commit comments

Comments
 (0)