JavaScript APIs in Optimize must be wrapped in a .ready() callback to run properly.
Optimize JavaScript APIs won’t work reliably unless they’re wrapped in a .ready() callback. This callback waits for the snippet to load before running your code — preventing errors and race conditions.
Requirements for using APIs
- The Optimize snippet must be loaded on on the page before calling APIs
- All API calls must be wrapped in
intellimize.ready()blocks
intellimize.ready(function() {
// Your API calls go here
});Note
The intellimize.ready() callback ensures the Optimize JavaScript library has fully loaded before your API calls run.
Options for using multiple APIs
Option 1: Combine multiple calls in a single callback — efficient and recommended if calls are logically related
// Multiple APIs in one callback
intellimize.ready(function() {
intellimize.setAttributes(scope, attributes);
intellimize.onVariationRecorded(success, error);
});Option 2: Split calls across multiple callbacks — useful if you want to intersperse them with other page logic
// Multiple callbacks
intellimize.ready(function() {
intellimize.setAttributes(scope, attributes);
});
intellimize.ready(function() {
intellimize.onVariationRecorded(success, error);
});Callback registration order
Each ready() block is registered in order. They don’t overwrite each other and will all run when the snippet is ready.
Note
You don’t need to worry about conflicts — just declare callbacks before any logic that uses the APIs.