Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide
These docs are for v2.0. Click to read the latest docs for v3.0.

Enable bundled datafiles

When your customer opens your app for the first time after installing or updating it, the manager attempts to pull the newest datafile from Optimizely's CDN. If your app is unable to contact the servers, the manager can substitute a datafile that you included (bundled) when you created your app.

By bundling a datafile within your app, you ensure that the Optimizely manager can initialize without waiting for a response from the CDN. This lets you prevent poor network connectivity from causing your app to hang or crash while it loads.

Datafile bundling works with both synchronous and asynchronous initialization because reverting to a bundled datafile happens during the Optimizely manager's initialization, before a client is instantiated.

/**
 * Initialize Optimizely asynchronously with a datafile.
 *  If it is not able to download a new datafile, it will 
 *  initialize an OptimizelyClient with the one provided.
 */
optimizelyManager.initialize(this, R.raw.datafile, new OptimizelyStartListener() {
    @Override
    public void onStart(OptimizelyClient optimizely) {
        startVariation();
    }
});

/** 
* Initialize Optimizely synchronously
*  This will immediately instantiate and return an 
*  OptimizelyClient with the datafile that was passed in. 
*  It'll also download a new datafile from the CDN and 
*  persist it to local storage.
*  The newly downloaded datafile will be used the next 
*  time the SDK is initialized.
*/
optimizelyManager.initialize(myApplication, R.raw.datafile);
OPTLYManager *manager = [[OPTLYManager alloc] initWithBuilder:[OPTLYManagerBuilder  builderWithBlock:^(OPTLYManagerBuilder * _Nullable builder) {
  // Load the datafile from the bundle
  NSString *filePath =[[NSBundle bundleForClass:[self class]]
                       pathForResource:@"datafileName"
                       ofType:@"json"];
  NSString *fileContents =[NSString stringWithContentsOfFile:filePath
                           encoding:NSUTF8StringEncoding
                           error:nil];
  NSData *jsonData = [fileContents dataUsingEncoding:NSUTF8StringEncoding];

  // Set the datafile in the builder
  builder.datafile = jsonData;
  builder.sdkKey = @"SDK_KEY_HERE";
}]];
let manager = OPTLYManager(builder: OPTLYManagerBuilder(block: { (builder) in
  builder?.sdkKey = "SDK_KEY_HERE"
  // Load the datafile from the bundle
  let bundle = Bundle.init(for: self.classForCoder)
  let filePath = bundle.path(forResource: "datafileName", ofType: "json")
  var jsonDatafile: Data? = nil
  do {
    let fileContents = try String.init(contentsOfFile: filePath!,
    encoding: String.Encoding.utf8)
    jsonDatafile = fileContents.data(using: String.Encoding.utf8)!
  }
  catch {
  print("Invalid JSON data")
  }
  // Set the datafile in the builder
  builder!.datafile = jsonDatafile
}))