Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions DemoSwiftApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// - initialize immediately with the given JSON datafile or its cached copy
// - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings

initializeOptimizelySDKWithCustomization()
//initializeOptimizelySDKWithCustomization()
Copy link
Contributor

Choose a reason for hiding this comment

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

is this method deprecated?

initializeOptimizelySDKWithClientConfiguration()
}

// MARK: - Initialization Examples
Expand Down Expand Up @@ -94,17 +95,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func initializeOptimizelySDKWithCustomization() {
// customization example (optional)

let customLogger = CustomLogger()
// 30 sec interval may be too frequent. This is for demo purpose.
// This should be should be much larger (default = 10 mins).
let customDownloadIntervalInSecs = 30

optimizely = OptimizelyClient(sdkKey: sdkKey,
logger: customLogger,
periodicDownloadInterval: customDownloadIntervalInSecs,
defaultLogLevel: logLevel)

addListeners()

// initialize SDK
optimizely!.start { result in
switch result {
case .failure(let error):
print("Optimizely SDK initiliazation failed: \(error)")
case .success:
print("Optimizely SDK initialized successfully!")
}
self.startWithRootViewController()
}
}

func initializeOptimizelySDKWithClientConfiguration() {

var clientConfig = OptimizelyClientConfig()
clientConfig.defaultLogLevel = .debug
clientConfig.periodicDownloadInterval = 10*60
clientConfig.fetchDatafileResourceTimeout = 5*60
clientConfig.fetchDatafileRequestTimeout = 60
clientConfig.customEventEndPoint = "https://google.com"
clientConfig.eventBatchInterval = 0 // disable batching
clientConfig.eventBatchSize = 20
clientConfig.eventQueueMaxSize = 1000
clientConfig.optInForCertficatePinning = false

optimizely = OptimizelyClient(sdkKey: sdkKey,
logger: customLogger,
periodicDownloadInterval: customDownloadIntervalInSecs,
defaultLogLevel: logLevel)

clientConfig: clientConfig)

addListeners()

// initialize SDK
Expand Down
5 changes: 4 additions & 1 deletion OptimizelySDK/Customization/DefaultEventDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
// timer as a atomic property.
var timer: AtomicProperty<Timer> = AtomicProperty<Timer>()

public init(batchSize: Int = 10, backingStore: DataStoreType = .file, dataStoreName: String = "OPTEventQueue", timerInterval: TimeInterval = 60*1 ) {
public init(batchSize: Int = 10,
backingStore: DataStoreType = .file,
dataStoreName: String = "OPTEventQueue",
timerInterval: TimeInterval = 60*1 ) {
self.batchSize = batchSize > 0 ? batchSize : 1
self.backingStore = backingStore
self.backingStoreName = dataStoreName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Foundation

@objcMembers public class EventForDispatch: NSObject, Codable {
static let eventEndpoint = "https://logx.optimizely.com/v1/events"
static var eventEndpoint = "https://logx.optimizely.com/v1/events"

public let url: URL
public let body: Data
Expand Down
62 changes: 61 additions & 1 deletion OptimizelySDK/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ open class OptimizelyClient: NSObject {
/// - logger: custom Logger
/// - eventDispatcher: custom EventDispatcher (optional)
/// - userProfileService: custom UserProfileService (optional)
/// - periodicDownloadInterval: custom interval for periodic background datafile download (optional. default = 10 * 60 secs)
/// - defaultLogLevel: default log level (optional. default = .info)
public init(sdkKey: String,
logger: OPTLogger? = nil,
Expand All @@ -89,6 +88,67 @@ open class OptimizelyClient: NSObject {
logger.d("SDK Version: \(version)")
}

/// Optimizely Manager
///
/// - Parameters:
/// - sdkKey: sdk key
/// - logger: custom Logger
/// - eventDispatcher: custom EventDispatcher (optional)
/// - userProfileService: custom UserProfileService (optional)
/// - clientConfig: custom SDK configuration
public init(sdkKey: String,
logger: OPTLogger? = nil,
eventDispatcher: OPTEventDispatcher? = nil,
userProfileService: OPTUserProfileService? = nil,
clientConfig: OptimizelyClientConfig) {

self.sdkKey = sdkKey

super.init()

// use default settings if no custom settings provided

let clientConfig = clientConfig ?? OptimizelyClientConfig()

// UserProfileService

let userProfileService = userProfileService ?? DefaultUserProfileService()

// Logger

let logger = logger ?? DefaultLogger()
type(of: logger).logLevel = clientConfig.defaultLogLevel
logger.d("SDK Version: \(version)")

// EventDispatcher

let eventDispatcher = eventDispatcher ?? DefaultEventDispatcher(batchSize: clientConfig.eventBatchSize,
timerInterval: clientConfig.eventBatchInterval)
if let eventEndPoint = clientConfig.customEventEndPoint {
EventForDispatch.eventEndpoint = eventEndPoint
}

// DatafileHandler

let datafileHandler = DefaultDatafileHandler()
datafileHandler.setTimer(sdkKey: sdkKey, interval: Int(clientConfig.periodicDownloadInterval))

// DecisionService

let decisionService = DefaultDecisionService(userProfileService: userProfileService)

// NotificationCenter

let notificationCenter = DefaultNotificationCenter()

self.registerServices(sdkKey: sdkKey,
logger: logger,
eventDispatcher: eventDispatcher,
datafileHandler: datafileHandler,
decisionService: decisionService,
notificationCenter: notificationCenter)
}

/// Start Optimizely SDK (Asynchronous)
///
/// If an updated datafile is available in the server, it's downloaded and the SDK is configured with
Expand Down
55 changes: 55 additions & 0 deletions OptimizelySDK/Optimizely/OptimizelyClientConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
/****************************************************************************
* Copyright 2019, 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. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

import Foundation

public struct OptimizelyClientConfig {

/// Default logger level (.error, .warning, .info, .debug)
public var defaultLogLevel: OptimizelyLogLevel = .info

/// Custom interval for periodic background datafile download (seconds)
/// If set to 0, background polling is disabled
public var periodicDownloadInterval: TimeInterval = 10*60
/// Timeout for datafile download completion (seconds)
/// If not set, `URLSessionConfiguration.timeoutIntervalForResource` default value is used (7 days)
public var fetchDatafileResourceTimeout: TimeInterval?
/// Timout for datafile request (seconds)
/// If not set, `URLSessionConfiguration.timeoutIntervalForRequest` default value is used (60 secs)
public var fetchDatafileRequestTimeout: TimeInterval?
/// This is for debugging purposes when you don't want to download the datafile.
/// In practice, you should allow the background thread to update the cache copy
public var doFetchDatafileBackground: Bool = false

/// URL endpoint for event dispatch
/// If not overriden, all events are sent to the default optimizely server url (https://logx.optimizely.com/v1/events).
public var customEventEndPoint: String?
/// Custom Interval for periodic dispatch of batched events (seconds)
/// If set to 0, events are dispatched immediately (no batching)
public var eventBatchInterval: TimeInterval = 1*60
/// Maximum number of events to be batched into a single batch event
public var eventBatchSize: Int = 10
/// Maximum number of events that can be queued
/// If overflowed, the oldest events are discarded
public var eventQueueMaxSize: Int = 30000

/// Enable certficate pinning for extra security
public var optInForCertficatePinning: Bool = false

public init() {}

}
Loading