Segment Custom Destination Functions allow you to build and deploy custom destinations on your workspace. By writing just a few lines of (serverless) code, you can transform your Segment events and send them to other APIs. Functions allow you to send your data into tools outside the Segment catalog, or to your own internal services.
This repo contains the recommended segment function (see src/index.js) for connection with Optimizely. Version 1 of this repo is intended to support Segment event relay for Optimizely Full Stack. Depending on early feedback, future versions may aim to support Optimizely Web Experimentation.
NOTE: The entire Segment Custom Function for Optimizely is in src/index.js. The rest of this project simply provides documentation and support for linting / formatting / testing.
- Segment Custom Destination Functions
- Segment track
- Getting your Optimizely Datafile and SDK key
- Optimizely Event API
git clone https://github.com/circAssimilate/optimizely-segment-custom-destination-function.git
cd optimizely-segment-custom-destination-function
nvm use
npm install
This Custom Destination Function expects Segment analytics.track() calls to have an event with following format:
{
"event": "CTA Click",
"integrations": {
"Optimizely": {
"attributes": {
"account_created_epoch_date": 1373291740686,
"account_id": 123456,
"is_internal_user": false,
"role": "developer",
"total_spend_in_cents": 99000,
},
"userId": "9z8y7x6w5v"
}
},
"timestamp": "2020-03-09T22:28:49.259Z",
"type": "track",
}
Most importantly, the data in integrations.Optimizely
is the engine that makes this solution work - so be sure to set that up where segment tracking occurs. Note, the above example has most Segment common fields removed.
Create a Segment Custom Destination Function and copy the contents of src/index.js (until module.exports
) into Segment. By default, you'll need to complete the following steps to grab a snapshot of the relevant Optimizely data.
- Copy the relevant Environment SDK Key from the Optimizely Full Stack Settings page (e.g.
a1b2c3d4e5f6
) - Complete Development steps above, then run
npm run build -- --sdkKey=a1b2c3d4e5f6
(replacinga1b2c3d4e5f6
with your SDK Key) - Copy all the contents of
dist/index.js
- Paste in the Segment Custom Destination Function editor
The npm run build -- --sdkKey=a1b2c3d4e5f6
script can be ran as needed to update the Optimizely data snapshot. As an example, this could be done each time new Events or Attributes are added.
Optionally, the shouldFetchOptimizelyData
setting can be used to fetch new Optimizely data each time (not recommended).
This project takes advantage of libraries like ESLint, Prettier, and node-markdown-spellcheck so contributors don't have to think about those things - these will be run as part of a pre-commit hook.
Run ESLint manually via npm run lint
.
Prettier can also be configured to run on save in most IDEs - or run manually via npm run pretty
.
Spellcheck for .md
files can be manually run via npm run spellcheck
. *If committing hangs on this step there are errors. When that happens, cancel commit (via ctrl + c
) and npm run spellcheck
to manually address errors.
This project uses Jest for testing. Tests are lightweight but important to prevent regressions and new bugs. Tests will be run as part of a pre-commit hook and Pull Request action, but they can also be run at will via npm run test
.
This project adheres to Semantic Versioning and keeps a changelog based on https://keepachangelog.com/en/1.0.0/). Every noteworthy PR should either update the Unreleased section in or add a new release section to the changelog. For new releases, be sure and update the version in package.json as well.