SDK integration

To use the Varioqub SDK, add and initialize the AppMetrica iOS library. For more information on how to do this, see the AppMetrica SDK documentation.

App requirements:

  • iOS version 12 or later.
  • Xcode 14.1 or later.

General integration recommendations

  1. When starting a new session, call the activateConfigAndWait() method to get the stored flag values.
  2. Run fetchConfig in the background to export the new flag values from the server and activate them when the next session starts.
  3. We don't recommend calling the activateConfigAndWait() method mid-session because it can cause inconsistent app behavior.

Step 1. Connect the library to your project

The library can work with the following dependency managers:

To connect the library, add the following dependencies to the project's Podfile:

pod 'Varioqub', '~> 0.8'
pod 'Varioqub/MetricaAdapterReflection', '~> 0.8'

With this method, make sure AppMetrica SDK connects explicitly as in

pod 'AppMetricaAnalytics', '~> 5.10.0'

or implicitly via other pods like YandexMobileAds.

To connect the library, add the following dependencies to the project's Podfile:

pod 'Varioqub', '~> 0.8'

To connect the library, add the following dependencies to the project's Podfile:

pod 'Varioqub/MetricaAdapter', '~> 0.8'

If connecting results in a [!] The 'Pods-proj' target has frameworks with conflicting names: yandexmobilemetrica.xcframework conflict, connect the library using CocoaPods (without an explicit dependency).

To enable the library, follow these steps:

  1. Download the YandexMobileMetrica, MetricaAdapter, Varioqub, and VQSwiftProtobuf libraries.

  2. Add YandexMobileMetrica.framework, MetricaAdapter.framework, and Varioqub.framework to the project.

  3. (Optional) To enable crash handling, add YandexMobileMetricaCrashes.framework

Step 2. Initialize the library

To initialize the library, use

initialize(clientId: String, config: VarioqubConfig = .default, idProvider: VarioqubIdProvider?, reporter: VarioqubReporter?)

It takes the following parameters:

  • clientId: String. Your project ID specified as appmetrica.XXXXXX, where XXXXXX is the app ID from the AppMetrica interface. For example, clientId: "appmetrica.1234567".

    Tip

    You can get the application ID from the AppMetrica Settings page: copy it in General settings → Application ID.

  • config: VarioqubConfig. Default Varioqub configuration. In addition, you can pass a list of custom parameters with clientFeatures. For example, you can pass a flag indicating if the user is subscribed to newsletters.

  • idProvider: VarioqubIdProvider. Adapter for collecting statistics and sending them to an analytics system, such as AppMetrica. If you don't want to collect statistics, pass nil.

  • reporter: VarioqubReporter. Adapter for collecting statistics and sending them to an analytics system, such as AppMetrica. If you don't want to collect statistics, pass nil.

    Warning

    reporter is stored as a weak link in Varioqub. If you're passing an object, make sure it isn't eliminated too early.

    import SwiftUI
    import YandexMobileMetrica
    import YandexMobileMetricaCrashes
    import Varioqub
    import MetricaAdapter
    
    let adapter = AppmetricaAdapter()
    
    @main
    struct ExampleApp: App {
        init() {
        // Configure Varioqub SDK
    
        var vqCfg = VarioqubConfig.default
    
        // Add client parameters
    
        vqCfg.clientFeatures["feature1"] = "value1"
    
        // Initialize Varioqub SDK
    
        VarioqubFacade.shared.initialize(clientId: "appmetrica.1234567", config: vqCfg, idProvider: adapter, reporter: adapter)
        }
    }
    

VarioqubConfig configuration

The configuration uses default values.

public struct VarioqubConfig {
    public var baseURL: URL?
    public var settings: VarioqubSettingsProtocol?
    public var network: NetworkRequestCreator?
    public var fetchThrottle: TimeInterval?
    public var clientFeatures: [String: String] = [:]
    public var varioqubQueue: DispatchQueue?
    public var outputQueue: DispatchQueue = .main

    public static var `default`: VarioqubConfig = VarioqubConfig()
}

You can also pass:

  • clientFeatures: List of custom parameters. For example, you can pass a flag indicating if the user is subscribed to newsletters.

    vqCfg.clientFeatures["feature1"] = "value1"
    
  • fetchThrottle: Limit on the frequency of config updates in seconds. This is only recommended for testing purposes.

    var vqCfg = VarioqubConfig.default
    vqCfg.fetchThrottle = 2 // seconds
    

Step 3. Set the default configuration using an XML file

You can set up a default configuration to have your app use your preferred parameter values before it connects to a remotely configured server.

  1. Download the XML configuration file from the Flag configuration page and save it to your Xcode project.

  2. Pass the loaded XML configuration file using the loadXml method.

let path = URL(fileURLWithPath: "/full/path/to/my/file/defaults.xml")
VarioqubFacade.shared.loadXml(at: path, callback: nil)

Note

The file name defaults.xml and the file path are placeholders used for demonstration purposes.

Step 4. Run a background update of the flag configuration

To use the most recent flag configuration from the interface, run a background configuration update using the fetchConfig method.

Note

Each time you receive the latest version of the configuration (most often this occurs when starting a new session), you need to activate it using the activateConfigAndWait() method.

VarioqubFacade.shared.fetchConfig({ status in
    switch status {
        case .success: print("success")
        case .throttled, .cached: print("already latest")
        case .error(let e): print("error: \(e)")
    }
})

Step 5. Activate the configuration

Tip

Main use case:

Regularly download the configuration and activate it when the app is launched. That guarantees your flags won't change during user sessions. We recommend launching activation as soon as possible.

Activate the configuration using the activateConfigAndWait() method.

VarioqubFacade.shared.activateConfigAndWait()

Use the activateConfigAndWait() method every time you want to activate a received/loaded configuration (for example, when starting a new session).

Getting flags from the interface

Flag values are retrieved and returned in the following order:

  1. Flag is present in the experiment.
  2. Flag is present in the loaded configuration.
  3. Flag is present in the default configuration.

Flag getters:

  • VarioqubFacade.shared.getString(flag, defaultValue).

Code example:

extension VarioqubFlag {
    static let myFlag = VarioqubFlag(rawValue: "myFlag")
}

let flag = VarioqubFacade.shared.getString(flag: .myFlag, defaultValue: "flag_value")

Obtaining the device ID to test the experiment

To get the device ID, use the VarioqubFacade.varioqubId method:

let varioqubId = VarioqubFacade.shared.varioqubId

Note

The Varioqub.getId() method can return an empty response before the first successful fetchConfig result.

To learn more about creating and testing experiments, see Creating an experiment.