JavaScript

Apptimize supports client-side web, mobile-web, and hybrid mobile app testing through our JavaScript SDK.

At this time, the JavaScript SDK supports programmatic experimentation (Dynamic Variables and Code Blocks), but does not allow you to make visual changes using the Visual Editor.

SDK Installation

Sign in or Register for Apptimize. You’ll see a welcome page that asks for the name of your app. After entering all your info, you’ll see these installation instructions again.

  1. Download the latest JavaScript SDK from the SDK Download page.
  2. Copy the SDK to your project directory, like an include or scripts directory.
  3. Reference the SDK in your code: <script src="<path-to-sdk>/apptimize-js-client-<version>.min.js"></script>.

You can find our latest JavaScript API Documentation here.

Initializing Apptimize

To initialize Apptimize, make the following call with the App Key associated to your app, which can be found in the Install tab of your Dashboard.

As a best practice, we recommend setting a callback function that is executed when new Apptimize metadata has been downloaded. To ensure that experiments are executed as expected, it is particularly important to wait until the first set of metadata has been downloaded after initilazation before using other Apptimize functions. For example, you could set a flag the first time the callback is executed to indicate that Apptimize data is available, and wait until this flag has been set before executing any experiments.

If you don’t wait for the initial set of Apptimize data to be downloaded, you may see a disproportionate number of users allocated in the baseline variant which is the default.

JavaScript
// Create a function that will be executed whenever new Apptimize metadata is downloaded
function onMetadataUpdated(){
    console.log("New Apptimize metadata has been downloaded");
    Apptimize.track("metadata_event");
    // Set a flag to indicate Apptimize data is available
}

Apptimize.setOnMetadataUpdatedCallback(onMetadataUpdated);

Apptimize.setup("<appkey>");