Skip to content

optimizely/edge-delivery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optimizely Edge Delivery SDK

Optimizely Edge Delivery lets you execute Optimizely Web experiments on Cloudflare Workers.


Prequisites

Quick Start

#TODO: turn this into a cloudflare worker template instead

To get started quickly with a new project:

  1. Clone this repository and navigate to the templates/edge-delivery-starter directory

    cd templates/edge-delivery-starter
  2. Install requirements

    npm install
  3. Run the worker locally

    npm run dev 

    This will open your worker executing in a browser. This loads the website https://example.com/ and executes an experiment that modifies the h1 header text on the edge.

  4. Modify the SNIPPET_ID and DEV_URL environment variables in the in the wrangler.toml file. In this project, these are set to an example Optimizely Web Experiment by default. Modify these to your to test an Optimizely Web Experiment against your own website.

    • Run npm run dev again to test the changes.
  5. Log in to your Cloudflare account using OAuth

    wrangler login
  6. Deploy the worker to your Cloudflare account:

    npm run deploy
  7. On Cloudflare, add a route for your worker for the target website you want to proxy

  8. Navigate to your website in a browser, and see your experiments in action!

Implementing in an existing Worker

You can install the Optimizely Edge Delivery SDK in any existing Cloudflare Worker, whether you already route your incoming traffic through a Cloudflare Worker, or you'd prefer to start from scratch using Cloudflare's getting started guide.

Installing the Edge Delivery SDK

To install the Edge Delivery library, download and install the latest version of the edge-delivery npm package:

npm install @optimizely/edge-delivery@latest

Implementing and executing experiments

The SDK requires a Snippet ID (snippetId) to know which configuration file to retrieve to execute your experiments.

Basic configuration options

It's recommended to set a Development URL (devUrl) for the SDK to use as a target when testing locally or at your worker site directly.

const options = {
    "snippetId": "29061560280",
    "devUrl": "https://example.com/"
};

applyExperiments

The applyExperiments method is used to execute experiments. This method uses the request information to make experiment bucketing decisions and apply active experiment variations to the control. Any decisions or changes that cannot be made on the edge are packaged together and added to the <head> element for execution on the browser.

import { applyExperiments } from '@optimizely/edge-delivery';
...
await applyExperiments(request, ctx, options);

Other configuration options

Optionally, you may pass a Response object as the control in the options parameter. This can be useful if you already have an existing Cloudflare Worker that, for example, makes modifications to the control outside of Optimizely experiments.

let control = await fetch(request);
...
const options = {
    // Other options
    "control": control
};