An example of running an Optimizely Full Stack experiment with the Node SDK on a Twilio Programmable SMS service through their Serverless Function capability.
Set up an Optimizely Full Stack project by following our getting started guide.
- Get a Twilio phone number by following Twilio's getting started guide.
- Create a Twilio Runtime Function
- Go to the Configure dashboard of Runtime Functions
- Click
Enable ACCOUNT_SID and AUTH_TOKEN
to make initializing Twilio Client easier. - Add
optimizely-server-sdk
as a dependency at1.4.2
or use the latest version. - Add 'request' as a dependency at
2.83.0
. - Add 'request-promise' as a dependency at
4.2.2
.
Example implementation can be found in index.js
.
-
Create a new function from the
Hello SMS
template. -
Name the twilio function and give it a path.
-
Copy and paste the initialization code for the Optimizely SDK from the "Create an Optimizely Client" section of the Node getting started guide.
-
Grab the incoming message body.
var incomingMessage = event.Body;
-
Grab the incoming phone number.
var userId = event.From;
-
Import and initialize a hash object to anonymize phone numbers.
var crypto = require('crypto'); var md5sum = crypto.createHash('md5'); var userId = md5sum.update(event.From).digest('hex');
-
Check if the message body is "Hello"
-
Activate the experiment and send back messages of different variations.
-
Check if the message body is "Yes" or "No"
-
Track the responses and thank them for participating.