Skip to content

Commit

Permalink
chore: expose user obj constructor with both userid and customID (#360)
Browse files Browse the repository at this point in the history
create a user object with a userid and customids

More:
https://statsigconnect.slack.com/archives/C07ETLMDKC4/p1725876639552919
  • Loading branch information
weihao-statsig authored Sep 11, 2024
1 parent 51c9206 commit 8a1451a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 71 deletions.
113 changes: 42 additions & 71 deletions src/main/kotlin/com/statsig/sdk/StatsigUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import java.util.TreeMap
*
* userID or at least a customID is expected: learn more https://docs.statsig.com/messages/serverRequiredUserID
*
* @property userID a unique identifier for the user.
* @property customIDs a map of key-value pairs representing the ID type and value for the user
* @property userID a unique identifier for the user.
* @property customIDs a map of key-value pairs representing the ID type and value for the user
* @property email an email associated with the current user
* @property ip the ip address of the requests for the user
* @property ip the IP address of the requests for the user
* @property userAgent the user agent of the requests for this user
* @property country the country location of the user
* @property locale the locale for the user
Expand All @@ -28,51 +28,54 @@ data class StatsigUser private constructor(

@SerializedName("customIDs")
var customIDs: Map<String, String>?,
) {
constructor(userID: String) : this(userID, null)
constructor(customIDs: Map<String, String>) : this(null, customIDs)

@SerializedName("email")
var email: String? = null
var email: String? = null,

@SerializedName("ip")
var ip: String? = null
var ip: String? = null,

@SerializedName("userAgent")
var userAgent: String? = null
var userAgent: String? = null,

@SerializedName("country")
var country: String? = null
var country: String? = null,

@SerializedName("locale")
var locale: String? = null
var locale: String? = null,

@SerializedName("appVersion")
var appVersion: String? = null
var appVersion: String? = null,

@SerializedName("custom")
var custom: Map<String, Any>? = null
var custom: Map<String, Any>? = null,

@SerializedName("privateAttributes")
var privateAttributes: Map<String, Any>? = null
var privateAttributes: Map<String, Any>? = null,

@SerializedName("statsigEnvironment")
internal var statsigEnvironment: Map<String, String>? = null
) {
constructor(userID: String) : this(userID = userID, null)

constructor(customIDs: Map<String, String>) : this(null, customIDs = customIDs)

constructor(userID: String, customIDs: Map<String, String>) : this(userID = userID, customIDs = customIDs, null)

internal fun getCopyForLogging(): StatsigUser {
val userCopy = StatsigUser(userID, customIDs)
userCopy.email = email
userCopy.ip = ip
userCopy.userAgent = userAgent
userCopy.country = country
userCopy.locale = locale
userCopy.appVersion = appVersion
userCopy.custom = custom
userCopy.statsigEnvironment = statsigEnvironment
// DO NOT copy privateAttributes to the logging copy!
userCopy.privateAttributes = null

return userCopy
return StatsigUser(
userID = this.userID,
customIDs = this.customIDs,
email = this.email,
ip = this.ip,
userAgent = this.userAgent,
country = this.country,
locale = this.locale,
appVersion = this.appVersion,
custom = this.custom,
statsigEnvironment = this.statsigEnvironment
// DO NOT copy privateAttributes to the logging copy!
)
}

fun getHashWithoutStableID(): String {
Expand Down Expand Up @@ -105,50 +108,18 @@ data class StatsigUser private constructor(
override fun toString(): String {
val sb = StringBuilder()
sb.append("StatsigUser {\n")
sb.append("\tuserID: ")
sb.append(userID)
sb.append("\n")

sb.append("\temail: ")
sb.append(email)
sb.append("\n")

sb.append("\tip: ")
sb.append(ip)
sb.append("\n")

sb.append("\tuserAgent: ")
sb.append(userAgent)
sb.append("\n")

sb.append("\tcountry: ")
sb.append(country)
sb.append("\n")

sb.append("\tlocale: ")
sb.append(locale)
sb.append("\n")

sb.append("\tappVersion: ")
sb.append(appVersion)
sb.append("\n")

sb.append("\tcustom: ")
sb.append(custom)
sb.append("\n")

sb.append("\tstatsigEnvironment: ")
sb.append(statsigEnvironment)
sb.append("\n}")

sb.append("\tprivateAttributes: ")
sb.append(privateAttributes)
sb.append("\n")

sb.append("\tcustomIDs: ")
sb.append(customIDs)
sb.append("\n")

sb.append("\tuserID: ").append(userID).append("\n")
sb.append("\temail: ").append(email).append("\n")
sb.append("\tip: ").append(ip).append("\n")
sb.append("\tuserAgent: ").append(userAgent).append("\n")
sb.append("\tcountry: ").append(country).append("\n")
sb.append("\tlocale: ").append(locale).append("\n")
sb.append("\tappVersion: ").append(appVersion).append("\n")
sb.append("\tcustom: ").append(custom).append("\n")
sb.append("\tstatsigEnvironment: ").append(statsigEnvironment).append("\n")
sb.append("\tprivateAttributes: ").append(privateAttributes).append("\n")
sb.append("\tcustomIDs: ").append(customIDs).append("\n")
sb.append("}")
return sb.toString()
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/statsig/sdk/StatsigUserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class StatsigUserTest {
assertNull(user.customIDs)
}

@Test
fun testInitializeWithUserIDAndCustomID() {
val userID = "abc123"
val customIDs = mapOf<String, String>("customID" to "abc123")
val user = StatsigUser(userID, customIDs)
assertEquals(user.userID, "abc123")
assertEquals(user.customIDs, customIDs)
}

@Test
fun testInitializeCustomID() {
val customIDs = mapOf<String, String>("customID" to "abc123")
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/statsig/sdk/StatsigUserTestJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.statsig.sdk;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class StatsigUserTestJava {
@Test
public void testInitializeUserIDAndCustomID() {
String userId = "test123";
Map<String, String> customIds = new HashMap<>();
customIds.put("k1", "v1");
customIds.put("k2", "v2");

StatsigUser user = new StatsigUser(userId, customIds);

assertEquals(userId, user.getUserID());
assertEquals(customIds, user.getCustomIDs());
}
}

0 comments on commit 8a1451a

Please sign in to comment.