circAssimilate / segment-optimizely-crier

A Segment Custom Destination Function for Optimizely

Home Page:https://segment.com/docs/connections/destinations/custom-destinations/#custom%20destinations:%20functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimizely Crier

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.

API Documentation

Development

  1. git clone https://github.com/circAssimilate/optimizely-segment-custom-destination-function.git
  2. cd optimizely-segment-custom-destination-function
  3. nvm use
  4. npm install

Passing Optimizely user and attribute data via Segment tracking

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.

Setting up in Segment

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.

  1. Copy the relevant Environment SDK Key from the Optimizely Full Stack Settings page (e.g. a1b2c3d4e5f6)
  2. Complete Development steps above, then run npm run build -- --sdkKey=a1b2c3d4e5f6 (replacing a1b2c3d4e5f6 with your SDK Key)
  3. Copy all the contents of dist/index.js
  4. 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).

Linting, Formatting, and Spellcheck

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.

Testing

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.

Contributing

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.

About

A Segment Custom Destination Function for Optimizely

https://segment.com/docs/connections/destinations/custom-destinations/#custom%20destinations:%20functions


Languages

Language:JavaScript 100.0%