Skip to content

Commit

Permalink
def UserProperty instead of EventProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Oct 20, 2023
1 parent 2df7c17 commit 117f561
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
6 changes: 3 additions & 3 deletions ios/Nativebrik/Classes/experiment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func extractExperimentVariant(config: ExperimentConfig, normalizedUsrRnd: Double
return nil
}

func extractExperimentConfigMatchedToProperties(configs: ExperimentConfigs, properties: (_ seed: Int) -> [EventProperty], records: (_ experimentId: String) -> [ExperimentHistoryRecord]) -> ExperimentConfig? {
func extractExperimentConfigMatchedToProperties(configs: ExperimentConfigs, properties: (_ seed: Int) -> [UserProperty], records: (_ experimentId: String) -> [ExperimentHistoryRecord]) -> ExperimentConfig? {
guard let configs = configs.configs else {
return nil
}
Expand All @@ -84,7 +84,7 @@ func extractExperimentConfigMatchedToProperties(configs: ExperimentConfigs, prop
}
}

func isInDistribution(distribution: [ExperimentCondition], properties: [EventProperty]) -> Bool {
func isInDistribution(distribution: [ExperimentCondition], properties: [UserProperty]) -> Bool {
let props = Dictionary(uniqueKeysWithValues: properties.map({ property in
return (property.name, property)
}))
Expand Down Expand Up @@ -131,7 +131,7 @@ func isInFrequency(experimentId: String, frequency: ExperimentFrequency?, record
}
}

func comparePropWithConditionValue(prop: EventProperty, value: String, op: ConditionOperator) -> Bool {
func comparePropWithConditionValue(prop: UserProperty, value: String, op: ConditionOperator) -> Bool {
let values = value.split(separator: ",")
switch prop.type {
case .INTEGER:
Expand Down
3 changes: 1 addition & 2 deletions ios/Nativebrik/Classes/sdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public enum EventPropertyType {
case INTEGER
case STRING
case TIMESTAMPZ
case SEMVER
case UNKNOWN
}

Expand All @@ -123,7 +122,7 @@ public struct ComponentEvent {

public struct TriggerEvent {
public let name: String
init(_ name: String) {
public init(_ name: String) {
self.name = name
}
}
Expand Down
43 changes: 29 additions & 14 deletions ios/Nativebrik/Classes/user.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import UIKit

typealias ExperimentHistoryRecord = TimeInterval

enum UserPropertyType {
case INTEGER
case STRING
case TIMESTAMPZ
case SEMVER
case UNKNOWN
}

struct UserProperty {
public let name: String
public let value: String
public let type: UserPropertyType
}


enum NativebrikUserDefaultsKeys: String {
case USER_ID = "NATIVEBRIK_SDK_USER_ID"
case USER_RND = "NATIVEBRIK_SDK_USER_RND"
Expand All @@ -18,7 +33,7 @@ enum NativebrikUserDefaultsKeys: String {
case RETENTION_PERIOD_COUNT = "NATIVEBRIK_RETENTION_PERIOD_COUNT"
}

func nativebrikUserPropType(key: BuiltinUserProperty) -> EventPropertyType {
func nativebrikUserPropType(key: BuiltinUserProperty) -> UserPropertyType {
switch key {
case .userId:
return .STRING
Expand Down Expand Up @@ -187,57 +202,57 @@ public class NativebrikUser {
return Double(seededRnd) / 100.0
}

func toEventProperties(seed: Int) -> [EventProperty] {
func toEventProperties(seed: Int) -> [UserProperty] {
let now = getCurrentDate()
var eventProps: [EventProperty] = []
var eventProps: [UserProperty] = []

// set properties that depend on current time.
eventProps.append(EventProperty(
eventProps.append(UserProperty(
name: BuiltinUserProperty.currentTime.rawValue,
value: formatToISO8601(now),
type: .TIMESTAMPZ
))

let bootingTime = now.timeIntervalSince1970 - self.lastBootTime
eventProps.append(EventProperty(
eventProps.append(UserProperty(
name: BuiltinUserProperty.bootingTime.rawValue,
value: String(Int(bootingTime)),
type: .INTEGER
))

let localDates = getLocalDateComponent(now)
eventProps.append(contentsOf: [
EventProperty(
UserProperty(
name: BuiltinUserProperty.localYear.rawValue,
value: String(localDates.year),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localMonth.rawValue,
value: String(localDates.month),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localDay.rawValue,
value: String(localDates.day),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localHour.rawValue,
value: String(localDates.hour),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localMinute.rawValue,
value: String(localDates.minute),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localSecond.rawValue,
value: String(localDates.second),
type: .INTEGER
),
EventProperty(
UserProperty(
name: BuiltinUserProperty.localWeekday.rawValue,
value: localDates.weekday.rawValue,
type: .INTEGER
Expand All @@ -246,14 +261,14 @@ public class NativebrikUser {

for (key, value) in self.properties {
if key == BuiltinUserProperty.userRnd.rawValue {
let eventProp = EventProperty(
let eventProp = UserProperty(
name: key,
value: String(getSeededUserRnd(seed: seed)),
type: nativebrikUserPropType(key: BuiltinUserProperty(rawValue: key) ?? .unknown)
)
eventProps.append(eventProp)
} else {
let eventProp = EventProperty(name: key, value: value, type: nativebrikUserPropType(key: BuiltinUserProperty(rawValue: key) ?? .unknown))
let eventProp = UserProperty(name: key, value: value, type: nativebrikUserPropType(key: BuiltinUserProperty(rawValue: key) ?? .unknown))
eventProps.append(eventProp)
}
}
Expand Down

0 comments on commit 117f561

Please sign in to comment.