nativebrik

NativebrikExperiment

Definition
public class NativebrikExperiment {
    // dispatching an event to the experiment
    public func dispatch(_ event: NativebrikEvent)

    // embedding a component for swiftui and uikit
    public func embedding(
        _ id: String,
        arguments: Any? = nil,
        onEvent: ((_ event: ComponentEvent) -> Void)? = nil
    ) -> some View
    public func embedding<V: View>(
        _ id: String,
        arguments: Any? = nil,
        onEvent: ((_ event: ComponentEvent) -> Void)? = nil,
        @ViewBuilder content: (@escaping (_ phase: AsyncEmbeddingPhase) -> V)
    ) -> some View
    public func embeddingUIView(
        _ id: String,
        arguments: Any? = nil,
        onEvent: ((_ event: ComponentEvent) -> Void)? = nil
    ) -> UIView
    public func embeddingUIView(
        _ id: String,
        arguments: Any? = nil,
        onEvent: ((_ event: ComponentEvent) -> Void)?,
        content: @escaping (_ phase: EmbeddingPhase) -> UIView
    ) -> UIView

    // remote config
    public func remoteConfig(_ id: String, phase: @escaping ((_ phase: RemoteConfigPhase) -> Void)) -> RemoteConfig
    // remote config as view
    public func remoteConfigAsView<V: View>(_ id: String, @ViewBuilder phase: @escaping ((_ phase: RemoteConfigPhase) -> V)) -> some View

    // overlay
    public func overlay() -> some View
    public func overlayViewController() -> UIViewController
}

.dispatch

You can dispatch a nativebrik event to trigger your popup experiments or to track your goal metrics:

Dispatch a custom event
nativebrik
    .experiment
    .dispatch(NativebrikEvent("your_custom_event_name"))

where event is NativebrikEvent.

.embedding an embedding experiment

You can embed an embedding experiment that is created in nativebrik dashboard:

You can use the experiment id or the custom id. It's good to design the experiment with custom id beforehand. Because without editing the code, you can change the experiment in the nativebrik dashboard.

swiftui
struct YourView: View {
    @EnvironmentObject var nativebrik: NativebrikClient
    var body: some View {
        nativebrik
            .experiment
            .embedding("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>")
            .frame(height: 200) // recommended: set the frmae size
    }
}
uikit
class YourUIView: UIView {
    init(frame: CGRect) {
        super.init(frame: frame)

        let uiview = nativebrik
            .experiment
            .embeddingUIView("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>")
        uiview.frame = CGRect(x: 0, y: 0, width: 200, height: 200) // recommended: set the frmae
        self.addSubview(uiview)
    }
}

.embedding an embedding experiment adding an event handler

You can add an event handler to the embedding experiment.

swiftui
nativebrik
    .experiment
    .embedding("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>") { event in
        print(event)
    }
    .frame(height: 200)
uikit
let uiview = nativebrik
    .experiment
    .embeddingUIView("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>") { event in
        print(event)
    }

.embedding an embedding experiment with AsyncComponentPhase/ComponentPhase

In SwfitUI, You can use AsyncEmbeddingPhase to customize the view when it's in the loading, failed, and completed phase.

swiftui
nativebrik
    .experiment
    .embedding("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>", onEvent: nil) { phase in
        switch phase {
        case .loading:
            Text("loading")
        case .notFound:
            Text("not found")
        case .failed:
            Text("error")
        case .completed(let component):
            component.frame(height: 200)
        }
    }

In UIKit, You can use EmbeddingPhase to customize the view when it's in the loading, failed, and completed phase.

uikit
let uiview = nativebrik
    .experiment
    .embeddingUIView("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>", onEvent: nil) { phase in
        switch phase {
        case .loading:
            return UILabel(text: "loading")
        case .notFound:
            return UILabel(text: "not found")
        case .failed:
            return UILabel(text: "error")
        case .completed(let component):
            return component
        }
    }

.remoteConfigAsView (for SwiftUI)

It's useful when you try an a/b testing your whole app UI/UX, or you use it as a feature flag.

You can use .remoteConfigAsView to get RemoteConfigVariant as a remote config in the a swiftui builder context:

swift
struct YourView: View {
    @EnvironmentObject var nativebrik: NativebrikClient
    var body: some View {
        nativebrik
            .experiment
            .remoteConfigAsView("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>") { phase in
                switch phase {
                case .completed(let configVariant):
                    Text(configVariant.getAsString("<KEY_IN_VARIANT_CONFIG>"))
                default:
                    Text("loading")
                }
            }
    }
}

where the phase is RemoteConfigPhase:

.remoteConfig

You can use .remoteConfig to get RemoteConfigVariant in any context as well:

swift
nativebrik
    .experiment
    .remoteConfig("<EXPERIMENT_ID> or <EXPERIMENT_CUSTOME_ID>") { phase in
        switch phase {
        case .completed(let variantConfig):
            print(variantConfig.getAsString("<KEY_IN_VARIANT_CONFIG>"))
        default:
            print("loading or error")
        }
    }

where the phase is RemoteConfigPhase:

Advanced

Register the overlay

This is the overlay view that manages the popup exeperiments that is created in the Nativebrik app. To show the popups, you need to add the overlay view to your application.

SwiftUI

If NativebrikProvider is added to your application, the overlay view is automatically added to your application. But you can add the overlay view manually, if you prefer not to use NativebrikProvider.

Adding the overlay view to your SwiftUI application
@main
struct YourApp: App {
    let nativebrikClientContext: NativebrikClient = NativebrikClient(projectId: "<YOUR_PROJECT_ID>")

    var body: some Scene {
        WindowGroup {
            ZStack(alignment: .top) {
                // the overlay should be top to show popups in front
                self.nativebrikClientContext.experiment.overlay()
                // to access to the nativebrik client from child views
                ContentView().environmentObject(self.nativebrikClientContext)
            }
        }
    }
}

UIKit

Adding the overlay controller view to your UIKit application
// singleton
let nativebrik = {
    return NativebrikClient(projectId: "<YOUR_NATIVEBRIK_PROJECT_ID>")
}()

class YourViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let overlay = nativebrik.experiment.overlayViewController()
        self.addChild(overlay)
        self.view.addSubview(overlay.view)
    }
}
On this page