Skip to content

Commit

Permalink
Merge pull request #74 from plaidev/ios/0.6.0
Browse files Browse the repository at this point in the history
[ios] Support custom user properties and bump up to 0.6.0
  • Loading branch information
t-jimbo authored Feb 13, 2025
2 parents e388d79 + 1c33587 commit d39087c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Nativebrik.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Nativebrik'
s.version = '0.5.10'
s.version = '0.6.0'
s.summary = 'Nativebrik SDK'
s.description = <<-DESC
Nativebrik SDK for iOS.
Expand Down
26 changes: 25 additions & 1 deletion ios/Nativebrik/Nativebrik/data/user.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ private let USER_SEED_MAX: Int = 100000000

public class NativebrikUser {
private var properties: [String: String]
private var customProperties: [String: String]
private var lastBootTime: Double = getCurrentDate().timeIntervalSince1970
internal var userDB: UserDefaults

init() {
if !isNativebrikAvailable {
self.properties = [:]
self.customProperties = [:]
self.userDB = UserDefaults.standard
return
}

let suiteName = "\(Bundle.main.bundleIdentifier ?? "app").nativebrik.com"
self.userDB = UserDefaults(suiteName: suiteName) ?? UserDefaults.standard
self.properties = [:]
self.customProperties = [:]

// userId := uuid by default
let userId = self.userDB.object(forKey: NativebrikUserDefaultsKeys.USER_ID.rawValue) as? String ?? UUID().uuidString
Expand Down Expand Up @@ -127,12 +130,28 @@ public class NativebrikUser {
}
}

// This is an alias of NativebrikUser.setProperties
public func set(_ properties: [String: String]) {
for (key, value) in properties {
self.properties[key] = value
if key == BuiltinUserProperty.userId.rawValue {
// overwrite userId
self.properties[BuiltinUserProperty.userId.rawValue] = value
continue
}
self.customProperties[key] = value
}
}

public func setProperties(_ properties: [String: String]) {
self.set(properties)
}

public func getProperties() -> [String:String] {
var props = self.customProperties
props[BuiltinUserProperty.userId.rawValue] = self.properties[BuiltinUserProperty.userId.rawValue]
return props
}

/**
print user properties for debug
*/
Expand Down Expand Up @@ -263,6 +282,11 @@ public class NativebrikUser {
eventProps.append(eventProp)
}
}

for (key, value) in self.customProperties {
let eventProp = UserProperty(name: key, value: value, type: .STRING)
eventProps.append(eventProp)
}

return eventProps
}
Expand Down
9 changes: 6 additions & 3 deletions ios/Nativebrik/Nativebrik/data/variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ func _createVariableForTemplate(
arguments: Any? = nil,
projectId: String? = nil
) -> Any {
let userData: [String:Any] = (user != nil) ? [
"id": user?.id ?? "",
] : [:]
var userData: [String: Any] = [:]
if let user = user {
userData["id"] = user.id
user.getProperties().forEach { userData[$0.key] = $0.value }
}

let formData: [String:Any]? = form
var propertiesData: [String:Any] = [:]
if let properties = properties {
Expand Down
2 changes: 1 addition & 1 deletion ios/Nativebrik/Nativebrik/sdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Combine
// for development
public var nativebrikTrackUrl = "https://track.nativebrik.com/track/v1"
public var nativebrikCdnUrl = "https://cdn.nativebrik.com"
public let nativebrikSdkVersion = "0.5.10"
public let nativebrikSdkVersion = "0.6.0"

public let isNativebrikAvailable: Bool = {
if #available(iOS 15.0, *) {
Expand Down

0 comments on commit d39087c

Please sign in to comment.