Skip to content

Instantly share code, notes, and snippets.

View asaschachar's full-sized avatar

Asa Schachar asaschachar

View GitHub Profile

Backend Feature Flags in NodeJS

Prerequisites

  • Node version >= 8.1
  • npm version >= 5.6

Steps

Exporting Optimizely Event Data

Note: This functionality requires an Optimizely plan with access to "Event Data Export"

The following uses the open source command line tool oevents.

Prerequisites

  • A terminal to run the oevents bash script (any OS X and most GNU/Linux distributions)
  • jq - a command-line processor
  • curl (Note: All versions of OS X starting with Jaguar come with curl installed)
  • The Amazon AWS CLI (v2+)
# myapp.rb
require 'sinatra'
require './optly'
get '/' do
enabled = Optly.instance.client.is_feature_enabled('hello_world', 'user123', {
'customerId' => 123,
'isVIP' => true
})
enabled = Optly.instance.client.is_feature_enabled('hello_world', 'user123', {
'customerId' => 123,
'isVIP' => true
})
enabled ? 'Hello World! You got the hello_world feature!' : 'You did not get the hello_world feature'
# myapp.rb
require 'sinatra'
require './optly' # Imports the Optimizely singleton
get '/' do
'Hello world!'
end
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
# optly.rb
require 'singleton'
require 'logger'
require 'optimizely/optimizely_factory'
class Optly
include Singleton
def initialize
sdk_key = '<Your_SDK_Key>'
func handleRequest(c *gin.Context) {
userObj := map[string]string{"userId": "user123"}
jsonString, _ := json.Marshal(userObj)
req, _ := http.NewRequest("POST", "http://localhost:8080/v1/activate", bytes.NewBuffer(jsonString))
q := req.URL.Query()
q.Add("featureKey", "hello_world")
@asaschachar
asaschachar / CompleteExample.html
Last active March 2, 2022 04:40
Optimizely Full Stack in a Static Site
<html>
<head>
<script src="https://unpkg.com/@optimizely/optimizely-sdk@3.5/dist/optimizely.browser.umd.min.js"></script>
<script src="https://cdn.optimizely.com/datafiles/V7kE2tbrJaYGmtRF5W5QXf.json/tag.js"></script>
</head>
<body>
<h1>Hello World</h1>
<p id="subtitle">This is a subtitle</p>
</body>
<script>