Skip to content

Commit

Permalink
feat: add option to opt out non sdk metadata (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
weihao-statsig authored Jun 26, 2024
1 parent 1f5be3c commit d756651
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Sources/Statsig/DeviceEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,28 @@ struct DeviceEnvironment {
static func get(_ overrideStableID: String? = nil) -> [String: String?] {
return instance.get(overrideStableID)
}

static func getSDKMetadata(_ overrideStableID: String? = nil) -> [String: String?] {
return instance.getSDKMetadata(overrideStableID)
}

static func explicitGet(_ overrideStableID: String? = nil) -> [String: String] {
return instance.get(overrideStableID).mapValues { val in
return val ?? ""
}
}

private func getSDKMetadata(_ overrideStableID: String? = nil) -> [String: String?] {
lock.lock()
defer { lock.unlock() }

return [
"sdkType": DeviceEnvironment.sdkType,
"sdkVersion": DeviceEnvironment.sdkVersion,
"sessionID": sessionID,
"stableID": getStableID(overrideStableID)
]
}

private func get(_ overrideStableID: String? = nil) -> [String: String?] {
lock.lock()
Expand Down
21 changes: 19 additions & 2 deletions Sources/Statsig/StatsigUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public struct StatsigUser {
Any value you wish to use in evaluation, but not have logged with events can be stored in this field.
*/
public let privateAttributes: [String: StatsigUserCustomTypeConvertible]?

/**
Controls whether non-SDK-type SDK version metadata should be excluded or included.
Setting this option to `true` will exclude metadata related to non-SDK-type SDK versions. By default, this option is set to `false`, meaning all metadata is included.
*/
public var optOutNonSdkMetadata: Bool?

/**
Any Custom IDs to associated with the user.
Expand All @@ -62,6 +68,7 @@ public struct StatsigUser {
appVersion: String? = nil,
custom: [String: StatsigUserCustomTypeConvertible]? = nil,
privateAttributes: [String: StatsigUserCustomTypeConvertible]? = nil,
optOutNonSdkMetadata: Bool? = false,
customIDs: [String: String]? = nil)
{
self.userID = userID
Expand All @@ -71,6 +78,7 @@ public struct StatsigUser {
self.locale = locale
self.appVersion = appVersion
self.customIDs = customIDs
self.optOutNonSdkMetadata = optOutNonSdkMetadata

if let custom = custom, JSONSerialization.isValidJSONObject(custom) {
self.custom = custom
Expand All @@ -89,11 +97,20 @@ public struct StatsigUser {
}
self.privateAttributes = nil
}
self.deviceEnvironment = DeviceEnvironment.get()

if (self.optOutNonSdkMetadata ?? false) {
self.deviceEnvironment = DeviceEnvironment.getSDKMetadata()
} else {
self.deviceEnvironment = DeviceEnvironment.get()
}
}

mutating func setStableID(_ overrideStableID: String) {
self.deviceEnvironment = DeviceEnvironment.get(overrideStableID)
if (self.optOutNonSdkMetadata ?? false) {
self.deviceEnvironment = DeviceEnvironment.getSDKMetadata(overrideStableID)
} else {
self.deviceEnvironment = DeviceEnvironment.get(overrideStableID)
}
}

func toDictionary(forLogging: Bool) -> [String: Any?] {
Expand Down
11 changes: 11 additions & 0 deletions Tests/StatsigTests/StatsigUserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class StatsigUserSpec: BaseSpec {
expect(validEmptyUser.deviceEnvironment).toNot(beNil())
expect(validEmptyUser.customIDs).to(beNil())
}

it("only return sdk related medatada if opt out non sdk metadata") {
let validUserOptOutNonSdkMetadata = StatsigUser(optOutNonSdkMetadata: true)

let deviceEnvironment = validUserOptOutNonSdkMetadata.deviceEnvironment
expect(deviceEnvironment["sdkVersion"]) == "1.44.0"
expect(deviceEnvironment["sdkType"]) == "ios-client"
expect(deviceEnvironment["sessionID"]).toNot(beNil())
expect(deviceEnvironment["stableID"]).toNot(beNil())
expect(deviceEnvironment.count) == 4
}

it("is a valid user with ID provided") {
let validUserWithID = StatsigUser(userID: "12345")
Expand Down

0 comments on commit d756651

Please sign in to comment.