Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Sources/Implementation/Events/BatchEventBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019, 2021-2022, Optimizely, Inc. and contributors
// Copyright 2019, 2021-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,8 @@ class BatchEventBuilder {
attributes: OptimizelyAttributes?,
flagKey: String,
ruleType: String,
enabled: Bool) -> Data? {
enabled: Bool,
clientName: String? = nil) -> Data? {

let metaData = DecisionMetadata(ruleType: ruleType, ruleKey: experiment?.key ?? "", flagKey: flagKey, variationKey: variation?.key ?? "", enabled: enabled)

Expand All @@ -46,7 +47,8 @@ class BatchEventBuilder {
userId: userId,
attributes: attributes,
decisions: [decision],
dispatchEvents: [dispatchEvent])
dispatchEvents: [dispatchEvent],
clientName: clientName)
}

// MARK: - Converison Event
Expand All @@ -55,7 +57,8 @@ class BatchEventBuilder {
eventKey: String,
userId: String,
attributes: OptimizelyAttributes?,
eventTags: [String: Any]?) -> Data? {
eventTags: [String: Any]?,
clientName: String? = nil) -> Data? {

guard let event = config.getEvent(key: eventKey) else {
return nil
Expand All @@ -76,7 +79,8 @@ class BatchEventBuilder {
userId: userId,
attributes: attributes,
decisions: nil,
dispatchEvents: [dispatchEvent])
dispatchEvents: [dispatchEvent],
clientName: clientName)
}

// MARK: - Create Event
Expand All @@ -85,19 +89,22 @@ class BatchEventBuilder {
userId: String,
attributes: OptimizelyAttributes?,
decisions: [Decision]?,
dispatchEvents: [DispatchEvent]) -> Data? {
dispatchEvents: [DispatchEvent],
clientName: String? = nil) -> Data? {
let snapShot = Snapshot(decisions: decisions, events: dispatchEvents)

let eventAttributes = getEventAttributes(config: config, attributes: attributes)

let visitor = Visitor(attributes: eventAttributes, snapshots: [snapShot], visitorID: userId)

let fClientName = clientName ?? Utils.swiftSdkClientName

let batchEvent = BatchEvent(revision: config.project.revision,
accountID: config.project.accountId,
clientVersion: Utils.sdkVersion,
visitors: [visitor],
projectID: config.project.projectId,
clientName: Utils.swiftSdkClientName,
clientName: fClientName,
anonymizeIP: config.project.anonymizeIP,
enrichDecisions: true)

Expand Down
5 changes: 3 additions & 2 deletions Sources/Optimizely+Decide/OptimizelyClient+Decide.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021-2022, Optimizely, Inc. and contributors
// Copyright 2021-2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -112,7 +112,8 @@ extension OptimizelyClient {
attributes: attributes,
flagKey: feature.key,
ruleType: ruleType,
enabled: enabled)
enabled: enabled,
clientName: clientName)
decisionEventDispatched = true
}
}
Expand Down
42 changes: 26 additions & 16 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ open class OptimizelyClient: NSObject {
var logger: OPTLogger!
var eventDispatcher: OPTEventDispatcher?
public var datafileHandler: OPTDatafileHandler?

// MARK: - Default Services

var decisionService: OPTDecisionService!
public var notificationCenter: OPTNotificationCenter?
public var odpManager: OdpManager!
let sdkSettings: OptimizelySdkSettings
var clientName: String?

// MARK: - Public interfaces

Expand All @@ -76,6 +77,7 @@ open class OptimizelyClient: NSObject {
/// - defaultLogLevel: default log level (optional. default = .info)
/// - defaultDecisionOptions: default decision options (optional)
/// - settings: SDK configuration (optional)
/// - clientName: clientName for impression and conversion events (optional)
public init(sdkKey: String,
logger: OPTLogger? = nil,
eventDispatcher: OPTEventDispatcher? = nil,
Expand All @@ -84,12 +86,13 @@ open class OptimizelyClient: NSObject {
odpManager: OdpManager? = nil,
defaultLogLevel: OptimizelyLogLevel? = nil,
defaultDecideOptions: [OptimizelyDecideOption]? = nil,
settings: OptimizelySdkSettings? = nil) {
settings: OptimizelySdkSettings? = nil,
clientName: String? = nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this new option into OptimizelySdkSettings? It's not a common configuration usage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to config "sdkVersion" as well.
We can use "sdkName" and "sdkVersion" to be consistent with android-sdk?


self.sdkKey = sdkKey
self.sdkSettings = settings ?? OptimizelySdkSettings()
self.defaultDecideOptions = defaultDecideOptions ?? []

super.init()

self.odpManager = odpManager ?? OdpManager(sdkKey: sdkKey,
Expand All @@ -98,6 +101,7 @@ open class OptimizelyClient: NSObject {
cacheTimeoutInSecs: sdkSettings.segmentsCacheTimeoutInSecs,
timeoutForSegmentFetchInSecs: sdkSettings.timeoutForSegmentFetchInSecs,
timeoutForEventDispatchInSecs: sdkSettings.timeoutForOdpEventInSecs)
self.clientName = clientName
let userProfileService = userProfileService ?? DefaultUserProfileService()
let logger = logger ?? DefaultLogger()
type(of: logger).logLevel = defaultLogLevel ?? .info
Expand Down Expand Up @@ -208,7 +212,7 @@ open class OptimizelyClient: NSObject {
func configSDK(datafile: Data) throws {
do {
self.config = try ProjectConfig(datafile: datafile)

datafileHandler?.startUpdates(sdkKey: self.sdkKey) { data in
// new datafile came in
self.updateConfigFromBackgroundFetch(data: data)
Expand Down Expand Up @@ -285,7 +289,8 @@ open class OptimizelyClient: NSObject {
attributes: attributes,
flagKey: "",
ruleType: Constants.DecisionSource.experiment.rawValue,
enabled: true)
enabled: true,
clientName: clientName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before. If we change Utils.sdkVersion, no need for this change.


return variation.key
}
Expand Down Expand Up @@ -419,7 +424,8 @@ open class OptimizelyClient: NSObject {
attributes: attributes,
flagKey: featureKey,
ruleType: source,
enabled: featureEnabled)
enabled: featureEnabled,
clientName: clientName)
}

sendDecisionNotification(userId: userId,
Expand Down Expand Up @@ -744,7 +750,7 @@ open class OptimizelyClient: NSObject {
throw OptimizelyError.eventKeyInvalid(eventKey)
}

sendConversionEvent(eventKey: eventKey, userId: userId, attributes: attributes, eventTags: eventTags)
sendConversionEvent(eventKey: eventKey, userId: userId, attributes: attributes, eventTags: eventTags, clientName: self.clientName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can change Utils.sdkVersion and Utils.swiftSdkClientName to update when customize. In that way, we do not change these apis.

}

/// Read a copy of project configuration data model.
Expand All @@ -762,7 +768,7 @@ open class OptimizelyClient: NSObject {

return OptimizelyConfigImp(projectConfig: config)
}

}

// MARK: - Send Events
Expand All @@ -780,7 +786,8 @@ extension OptimizelyClient {
attributes: OptimizelyAttributes? = nil,
flagKey: String,
ruleType: String,
enabled: Bool) {
enabled: Bool,
clientName: String? = nil) {

// non-blocking (event data serialization takes time)
eventLock.async {
Expand All @@ -793,7 +800,8 @@ extension OptimizelyClient {
attributes: attributes,
flagKey: flagKey,
ruleType: ruleType,
enabled: enabled) else {
enabled: enabled,
clientName: clientName) else {
self.logger.e(OptimizelyError.eventBuildFailure(DispatchEvent.activateEventKey))
return
}
Expand All @@ -819,7 +827,8 @@ extension OptimizelyClient {
func sendConversionEvent(eventKey: String,
userId: String,
attributes: OptimizelyAttributes? = nil,
eventTags: OptimizelyEventTags? = nil) {
eventTags: OptimizelyEventTags? = nil,
clientName: String? = nil) {

// non-blocking (event data serialization takes time)
eventLock.async {
Expand All @@ -829,7 +838,8 @@ extension OptimizelyClient {
eventKey: eventKey,
userId: userId,
attributes: attributes,
eventTags: eventTags) else {
eventTags: eventTags,
clientName: clientName) else {
self.logger.e(OptimizelyError.eventBuildFailure(eventKey))
return
}
Expand Down Expand Up @@ -943,9 +953,9 @@ extension OptimizelyClient {
identifiers: [String: String] = [:],
data: [String: Any?] = [:]) throws {
try odpManager.sendEvent(type: type,
action: action,
identifiers: identifiers,
data: data)
action: action,
identifiers: identifiers,
data: data)
}

/// the device vuid (read only)
Expand All @@ -959,7 +969,7 @@ extension OptimizelyClient {

func fetchQualifiedSegments(userId: String,
options: [OptimizelySegmentOption],
completionHandler: @escaping ([String]?, OptimizelyError?) -> Void) {
completionHandler: @escaping ([String]?, OptimizelyError?) -> Void) {
odpManager.fetchQualifiedSegments(userId: userId,
options: options,
completionHandler: completionHandler)
Expand Down
Loading