Skip to main content

Roku Client SDK

Getting Started

The following will outline how to get up and running with Statsig for Roku.

Create an Account

To work with the SDK, you will need a Statsig account. If you don't yet have an account, go ahead and sign up for a free account now.

You could skip this for now, but you will need an SDK key and some gates/experiments to use with the SDK in just a minute.

Installation

You can start by downloading a copy of the github repository. Roku does not have a package manager where we could release an SDK, so instead, you will copy over the implementation from this repository to integrate Statsig in your roku app.

You will need the following files:

Statsig
-- components
-- statsigsdk
-- StatsigTask.brs
-- StatsigTask.xml
-- source
-- DynamicConfig.brs
-- Statsig.brs
-- StatsigClient.brs
-- StatsigUser.brs

The library consists of two main parts:

source/Statsig - an object used by SceneGraph components that connects a StatsigClient with the background StatsigTask. components/StatsigTask - a SceneGraph Task to do work in the background for integrating with Statsig like batching events and fetching values from Statsig servers.

The "components" folder contains SceneGraph components and the "source" folder contains BrightScript files. All of these files must be included in their respective folders of the application. Note that if you change the file paths, you will need to update the file references in StatsigTask.xml. You will also need to include those references in your main xml file.

Initialize the SDK

After installation, you will need to initialize the SDK using a Client SDK key from the "API Keys" tab on the Statsig console.

These Client SDK Keys are intended to be embedded in client side applications. If need be, you can invalidate or create new SDK Keys for other applications/SDK integrations.

info

Do NOT embed your Server Secret Key in client side applications

In addition to the SDK key, you should also pass in a StatsigUser for feature gate targeting and experimentation grouping purposes.

To Initialize the SDK, you first need to integrate the SDK files into your application.

Include StatsigClient.brs, StatsigUser.brs. DynamicConfig.brs, and Statsig.brs:

<script type="text/brightscript" uri="pkg:/source/Statsig.brs" />
<script type="text/brightscript" uri="pkg:/source/StatsigClient.brs" />
<script type="text/brightscript" uri="pkg:/source/StatsigUser.brs" />
<script type="text/brightscript" uri="pkg:/source/DynamicConfig.brs" />

Next, you can initialize the library in your init() function, and add a listener for when gates/experiments have been fetched:

<!-- in component xml -->
<StatsigTask id="statsigTask" />
    statsigTask = m.top.findNode("statsigTask")
statsigTask.observeField("initializeValues", "onStatsigReady")
m.statsig = Statsig(statsigTask)

user = StatsigUser()
user.setUserID("456")
m.statsig.initialize("<STATSIG_CLIENT_SDK_KEY>", user)

For more information on all of the user fields you can use, see the StatsigUser docs.

Before the SDK has loaded the updated values, all APIs will return default values (false for gates, empty configs and experiments).
To implement a callback handler for statsig being ready, and tell the SDK to load the updated values in the onStatsigReady function observed above:

function onStatsigReady() as void
m.statsig.load()

// Check gates, log events, check experiments, etc
gate = m.statsig.checkGate("gate_id")
config = m.statsig.getConfig("config_id")
experiment = m.statsig.getExperiment("experiment_id")
m.statsig.logEvent("event_name", "event_value", {metadata: "event_metadata"})
end function

If you need to update the user, m.statsig.updateUser(newUser) will trigger the same onStatsigReady callback once the new gate/config/experiment values have been fetched from statsig servers.