Command Line Executable
Optimizely-CLI includes a command line executable, optcli. Optcli will create the local project experiment and variation structure for you, host the local variation for debugging, and publish to Optimizely.
Local Hosting & Script Injection
We’ve found that injecting the experiment and variation code directly into the local browser makes for faster development & debug cycles. optcli includes an express.js based server that will compile and host a variation locally, and will generate a GreaseMonkey or TamperMonkey compliant user script to inject the CSS and JS into any page. Make a code change and refresh the browser – your variation changes will be immediately applied without restarting the server.
Targeting a page served over https? Not a problem! Optcli includes a self-signed certificate and can host over ssl with the
Script injection works by writing CSS and JS elements into the page, and targets any page which has an optcli=activate parameter. You can therefore run the variation code against any page by appending this parameter.
As part of the of EJS templates, you can create an
push-variation commands optcli lets you push your code directly to Optimizely via the REST API. You’ll have to have an API token with the appropriate permmissions issued for the project of course. Once the code is in Optimizely you’ll want to go in and adjust the other test parameters (targeting, traffic allocation, activation conditions, etc) per your test requirements.
Optcli is a node.js project and command-line executable. As such you’ll need to have node.js installed. To install optcli via npm:
npm install -g optimizely-cli
This will install the optcli executable onto your system.
View a list of optcli commands and options
Initialize a Project
Before you create an experiment or variation you’ll want to initialize a project. If have an existing project and have been issued an API key you can use the remote (
-r) option, and if not just create it locally. You’ll need to specify the project id (required):
optcli init [options] [project_id]
Where options are:
-r --remote – initialize from remote project
This will create a
project.json in the current directory.
Create Local Experiment
Create a local experiment under a project with the command:
optcli experiment <folder> <description> <edit_url>
<folder> – The folder (directory) for the new experiment. Required.
<description> – The experiment description that will show up in Optimizely. Required
<edit_url> – The default editor url for the experiment. Required.
Create Local Variation
Create a local variation and scaffold the source files
optcli variation <experiment> <folder> <description>
<experiment> – The directory or id of the experiment. Required.
<folder> – The new folder (directory) that will be created for the variation. Required.
<description> – The variation description that will show up in Optimizely. Required
Host Local Variation
Compiles the experiment and variation CSS / JS, creates a user script for injection and starts a local web server to host the files. Pointing your browser to the root URL (default http://localhost:8080) will show a page with installation steps and variation URLs.
optcli host [options] <path> [port]
path – Path to the variation directory. Required.
[port] – Port to host the server. Default to 8080.
-s --ssl – host over https
Push (create or update) an experiment to Optimizely through the API. This will create a new experiment if it hasn’t been pushed before, or update the experiment global CSS & JS if it has.
optcli push-experiment <path>
<path> – The path to the experiment directory. Required.
-i --iterate – Push experiment and all variatons
Push (create or update) an experiment to Optimizely through the API. This will create a new variation if it hasn’t been pushed before, or update the variation JS if it has. Note that the experiment must be pushed via
push-experiment before the variation.
optcli push-variation <path>
<path> – The path to the variation directory. Required.
We would love to hear any ideas for new features and improvements for Optimizely-CLI. Here are some things we have on the short term roadmap.
- Pulling experiments / variations from Optimizely.
- Templating mechanism for complex layout tests
Want to contribute?
Bug reports / fixes, documentation updates, tests are all welcome and very much appreciated. Head over to the Optimizely-CLI Github page.