Skip to content
absmartlyPublic

About

A/B Smartly Scala SDK

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ABsmartly Scala SDK

A Scala SDK for ABsmartly - A/B testing and feature flagging platform.

Compatibility

The ABsmartly Scala SDK is compatible with Scala 2.13+ and Scala 3. It provides both asynchronous (Future-based) and synchronous interfaces for variant assignment and goal tracking. The SDK is built with type-safe JSON handling using circe and functional programming principles.

Supported Scala Versions:

  • Scala 2.13.x
  • Scala 3.3.x

Installation

sbt

Add this to your build.sbt:

libraryDependencies += "com.absmartly" %% "absmartly-sdk" % "0.3.0"

Maven

Add this to your pom.xml:

<dependency>
  <groupId>com.absmartly</groupId>
  <artifactId>absmartly-sdk_2.13</artifactId>
  <version>0.3.0</version>
</dependency>

Gradle

implementation 'com.absmartly:absmartly-sdk_2.13:0.3.0'

Getting Started

Please follow the installation instructions before trying the following code.

Initialization

This example assumes an API Key, an Application, and an Environment have been created in the ABsmartly web console.

Recommended: Simple Initialization

import com.absmartly.sdk.SDK
import scala.concurrent.ExecutionContext.Implicits.global

val sdk = SDK.create(
  endpoint = "https://your-company.absmartly.io/v1",
  apiKey = "YOUR-API-KEY",
  application = "website",
  environment = "development"
)

With Optional Parameters

val sdk = SDK.create(
  endpoint = "https://your-company.absmartly.io/v1",
  apiKey = "YOUR-API-KEY",
  application = "website",
  environment = "development",
  retries = 3,
  timeout = 5000
)

Alternative: Using Configuration Objects

import com.absmartly.sdk.{SDK, SDKConfig}
import scala.concurrent.ExecutionContext.Implicits.global

val config = SDKConfig(
  endpoint = "https://your-company.absmartly.io/v1",
  apiKey = "YOUR-API-KEY",
  environment = "development",
  application = "website"
)

val sdk = new SDK(config)

SDK Options

Config Type Required? Default Description
endpoint String ✅ "" The URL to your API endpoint. Most commonly "https://your-company.absmartly.io/v1"
apiKey String ✅ "" Your API key which can be found on the Web Console.
environment String ✅ "" The environment of the platform where the SDK is installed. Environments are created on the Web Console and should match the available environments in your infrastructure.
application String ✅ "" The name of the application where the SDK is installed. Applications are created on the Web Console and should match the applications where your experiments will be running.
retries Int ❌ 5 Number of retry attempts for failed HTTP requests
timeout Int ❌ 3000 Connection timeout in milliseconds
eventLogger EventLogger ❌ NoOpEventLogger Custom event logger for SDK events (see Advanced section)

Creating a New Context

Asynchronously

import com.absmartly.sdk.{SDK, SDKConfig}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration._

val units = Map("session_id" -> "5ebf06d8cb5d8137290c4abb64155584fbdb64d8")

val contextFuture = sdk.createContext(units)

val context = Await.result(contextFuture, 5.seconds)

println(s"Context ready: ${context.isReady()}")

With Pre-fetched Data

When doing full-stack experimentation with ABsmartly, we recommend creating a context only once on the server-side. Creating a context involves a round-trip to the ABsmartly event collector. We can avoid repeating the round-trip on the client-side by sending the server-side data embedded in the first document.

import scala.concurrent.Await
import scala.concurrent.duration._
import com.absmartly.sdk.{SDK, SDKConfig, ContextData}

val units = Map("session_id" -> "5ebf06d8cb5d8137290c4abb64155584fbdb64d8")

val contextData: ContextData = Await.result(sdk.fetchContextDataAsync(), 5.seconds)

val context = sdk.createContextWith(units, contextData)
assert(context.isReady())

Refreshing the Context with Fresh Experiment Data

For long-running contexts, the context can be refreshed manually to pull updated experiment data.

val newData = sdk.fetchContextData()
context.refresh(newData)

Setting Extra Units

You can add additional units to a context by calling the setUnit() or setUnits() method. This method may be used, for example, when a user logs in to your application, and you want to use the new unit type in the context.

Note: You cannot override an already set unit type as that would be a change of identity. In this case, you must create a new context instead. The setUnit() and setUnits() methods can be called before the context is ready.

context.setUnit("db_user_id", "1000013")

context.setUnits(Map(
  "db_user_id" -> "1000013",
  "user_type" -> "premium"
))

Migrating from 0.2.0 to 0.3.0

Exposure gains a ruleOverride field and ExperimentData an assignmentRules field. The old constructors and apply overloads still work; a positional pattern match on either class (case Exposure(id, name, ...)) or a call to the old copy arity needs updating. See the CHANGELOG.

Migrating from 0.1.0 to 0.2.0

Version 0.2.0 contains source-incompatible changes to align the Scala SDK with the other ABsmartly SDKs:

  • Goal.properties is now Option[Map[String, Json]] (was Option[Map[String, Double]]). Wrap numbers with Json.fromDouble(...).
  • Context.customFieldValue(experimentName, fieldName) returns Option[Json] (was Option[String]); use customFieldValueType for the declared type.
  • Context.customFieldKeys() takes no argument and returns the keys across all experiments (was customFieldKeys(experimentName)).
  • ContextPublisher is a new extension point; custom implementations must implement publish(units, hashed, exposures, goals, attributes).
  • SDK.fetchContextData() is deprecated; use fetchContextDataAsync().

Basic Usage

Selecting a Treatment

val variant = context.treatment("exp_test_experiment")

if (variant == 0) {
  // User is in control group (variant 0)
} else {
  // User is in treatment group
}

Treatment Variables

Variables allow you to configure different values for each variant.

val buttonColor = context.variableValue("button.color", "blue")

val maxItems = context.variableValue("cart.max_items", "10")

Peek at Treatment Variants

Although generally not recommended, it is sometimes necessary to peek at a treatment without triggering an exposure. The ABsmartly SDK provides a peek() method for that.

val variant = context.peek("exp_test_experiment")

if (variant == 0) {
  // User is in control group (variant 0)
} else {
  // User is in treatment group
}

Peeking at Variables

val peekedColor = context.peekVariableValue("button.color", "blue")

Overriding Treatment Variants

During development, for example, it is useful to force a treatment for an experiment. This can be achieved with the setOverride() and/or setOverrides() methods. The setOverride() and setOverrides() methods can be called before the context is ready.

context.setOverride("exp_test_experiment", 1)

context.setOverrides(Map(
  "exp_test_experiment" -> 1,
  "exp_another_experiment" -> 0
))

Advanced

Context Attributes

Attributes are used for audience targeting. The setAttribute() and setAttributes() methods can be called before the context is ready. They accept circe Json values.

import io.circe.Json
import io.circe.syntax._

context.setAttribute("user_agent", "Mozilla/5.0".asJson)
context.setAttribute("customer_age", "new_customer".asJson)
context.setAttribute("age", 25.asJson)

context.setAttributes(Map(
  "user_agent" -> "Mozilla/5.0".asJson,
  "customer_age" -> "new_customer".asJson
))

Audiences are always evaluated with the client's configured application and environment as built-in attributes, so audience filters can target them without you setting anything. A user-set attribute of the same name always wins — including one set after the context is created, which re-evaluates any cached assignment. These built-in values are used for evaluation only; they are never added to the publish payload (the collector attaches them server-side). The same values are visible to assignment-rule conditions.

Audience filters and rule conditions support the semantic-version operators semver_eq, semver_gt, semver_gte, semver_lt and semver_lte, for example {"semver_gte": [{"var": "app_version"}, {"value": "2.4.0"}]}. A leading v and build metadata are ignored, missing parts count as 0, numeric parts compare numerically, and a pre-release sorts before its release. Like the JavaScript and Java SDKs, parsing is permissive: a non-numeric part (as in 1.x) is compared as text, after any numeric one. Only an operand that is empty (or empty after removing v and build metadata) or has no string form (null, an array or an object) makes the comparison null (no match).

Custom Assignments

Custom assignments allow you to set a specific variant for an experiment programmatically.

context.setCustomAssignment("exp_test_experiment", 1)

context.setCustomAssignments(Map(
  "exp_test_experiment" -> 1,
  "exp_another_experiment" -> 0
))

Assignment Rules

Experiments can carry assignment rules, configured in the Web Console. The SDK applies them automatically; there is no API to call. The first rule whose environments include the SDK's environment (an empty list means all environments) and whose conditions match the context attributes wins:

  • an assign rule gives its fixed variant;
  • a split rule divides matching units across the variants by its percentages (e.g. "20/30/50"), hashing the unit with the experiment's own seed. A split rule that matches while the experiment's unit is not set gives the experiment's no-unit variant (the full-on variant, otherwise 0) and is re-resolved once the unit is set, unless it was already exposed.

A matching rule beats audiences (including audienceStrict), traffic eligibility, the full-on variant and custom assignments; only setOverride() beats a rule. Rule assignments are published with ruleOverride: true (and assigned: false), which keeps them out of the experiment's analysis. A malformed rule is skipped; an assign rule whose variant is out of range, or a split whose share count differs from the variant count, stops rule evaluation and the unit is assigned normally.

Tracking Goals

Goals are created in the ABsmartly web console.

import io.circe.Json
import io.circe.syntax._

context.track("payment")

context.track("purchase", Some(Map(
  "item_count" -> Json.fromInt(1),
  "total_amount" -> Json.fromDoubleOrNull(1999.99)
)))

Publishing Pending Data

Sometimes it is necessary to ensure all events have been published to the ABsmartly collector before proceeding. You can explicitly call the publish() method.

import scala.concurrent.Await
import scala.concurrent.duration._

val publishFuture = context.publish()
Await.result(publishFuture, 5.seconds)

Finalizing

The finalizeContext() method will ensure all events have been published to the ABsmartly collector, like publish(), and will also "seal" the context, preventing any further events from being tracked. Overrides and custom assignments (single and bulk setters) remain allowed after finalization; treatment and variable reads still throw.

Note: The method is named finalizeContext() instead of finalize() to avoid conflicts with Java's Object.finalize().

import scala.concurrent.Await
import scala.concurrent.duration._

val finalizeFuture = context.finalizeContext()
Await.result(finalizeFuture, 5.seconds)

assert(context.isFinalized())

Custom Event Logger

You can implement a custom event logger to handle SDK events. This is useful for debugging, analytics, or integrating with other systems.

import com.absmartly.sdk.EventLogger
import io.circe.Json

class CustomEventLogger extends EventLogger {
  override def logEvent(eventType: String, data: Json): Unit = {
    eventType match {
      case "exposure" =>
        println(s"Exposure event: ${data.noSpaces}")
      case "goal" =>
        println(s"Goal event: ${data.noSpaces}")
      case "error" =>
        println(s"Error event: ${data.noSpaces}")
      case "ready" =>
        println("Context ready")
      case "refresh" =>
        println("Context refreshed")
      case "publish" =>
        println("Events published")
      case "finalize" =>
        println("Context finalized")
      case _ =>
        println(s"Unknown event: $eventType")
    }
  }
}

Usage:

val sdk = SDK.create(
  endpoint = "https://your-company.absmartly.io/v1",
  apiKey = "YOUR-API-KEY",
  application = "website",
  environment = "development",
  eventLogger = new CustomEventLogger()
)

Event Types

Event When Data
error Data load, refresh or publish fails {"message": ...}
ready Context turns ready ContextData used to initialize
refresh refresh() method succeeds ContextData used to refresh
publish publish() method succeeds PublishEvent sent to collector
exposure treatment() succeeds on first exposure Exposure enqueued for publishing
goal track() method succeeds Goal enqueued for publishing
finalize finalizeContext() succeeds the first time null

Platform-Specific Examples

Using with Play Framework

Integrate ABsmartly into your Play Framework application using dependency injection.

// app/services/ABSmartlyService.scala
package services

import javax.inject._
import com.absmartly.sdk._
import play.api.Configuration
import scala.concurrent.ExecutionContext

@Singleton
class ABSmartlyService @Inject()(config: Configuration)(implicit ec: ExecutionContext) {

  private val sdk: SDK = SDK.create(
    endpoint = config.get[String]("absmartly.endpoint"),
    apiKey = config.get[String]("absmartly.apiKey"),
    application = config.get[String]("absmartly.application"),
    environment = config.get[String]("absmartly.environment")
  )

  def createContext(sessionId: String): scala.concurrent.Future[Context] = {
    val units = Map("session_id" -> sessionId)
    sdk.createContext(units)
  }
}

// app/controllers/ProductController.scala
package controllers

import javax.inject._
import play.api.mvc._
import services.ABSmartlyService
import scala.concurrent.{ExecutionContext, Await}
import scala.concurrent.duration._

@Singleton
class ProductController @Inject()(
  cc: ControllerComponents,
  absmartlyService: ABSmartlyService
)(implicit ec: ExecutionContext) extends AbstractController(cc) {

  def show = Action.async { implicit request =>
    val sessionId = request.session.get("sessionId")
      .getOrElse(java.util.UUID.randomUUID().toString)

    absmartlyService.createContext(sessionId).map { context =>
      val treatment = context.treatment("exp_product_layout")

      context.finalizeContext()

      if (treatment == 0) {
        Ok(views.html.productControl()).withSession("sessionId" -> sessionId)
      } else {
        Ok(views.html.productTreatment()).withSession("sessionId" -> sessionId)
      }
    }
  }
}

// conf/application.conf
absmartly {
  endpoint = "https://your-company.absmartly.io/v1"
  endpoint = ${?ABSMARTLY_ENDPOINT}

  apiKey = ${ABSMARTLY_API_KEY}

  application = "website"
  application = ${?ABSMARTLY_APPLICATION}

  environment = "production"
  environment = ${?ABSMARTLY_ENVIRONMENT}
}

Using with Akka HTTP

Use ABsmartly with Akka HTTP for reactive request handling.

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.model._
import com.absmartly.sdk._
import scala.concurrent.{ExecutionContext, Future}

object WebServer {

  def main(args: Array[String]): Unit = {
    implicit val system: ActorSystem = ActorSystem("absmartly-system")
    implicit val ec: ExecutionContext = system.dispatcher

    val sdk = SDK.create(
      endpoint = "https://your-company.absmartly.io/v1",
      apiKey = "YOUR-API-KEY",
      application = "website",
      environment = "production"
    )

    val route: Route =
      path("product") {
        get {
          optionalCookie("session_id") { sessionCookie =>
            val sessionId = sessionCookie.map(_.value)
              .getOrElse(java.util.UUID.randomUUID().toString)

            val units = Map("session_id" -> sessionId)

            onSuccess(sdk.createContext(units)) { context =>
              val treatment = context.treatment("exp_product_layout")

              context.finalizeContext()

              val responseHtml = if (treatment == 0) {
                "<h1>Control Group</h1>"
              } else {
                "<h1>Treatment Group</h1>"
              }

              complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, responseHtml))
            }
          }
        }
      }

    Http().newServerAt("localhost", 8080).bind(route)
    println("Server online at http://localhost:8080/")
  }
}

About A/B Smartly

A/B Smartly is the leading provider of state-of-the-art, on-premises, full-stack experimentation platforms for engineering and product teams that want to confidently deploy features as fast as they can develop them.

A/B Smartly's real-time analytics helps engineering and product teams ensure that new features will improve the customer experience without breaking or degrading performance and/or business metrics.

Have a look at our growing list of clients and SDKs:

About

A/B Smartly Scala SDK

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages