Skip to content

Instantly share code, notes, and snippets.

@ericwishom
ericwishom / kochava_enrichment_mockup.js
Last active September 30, 2021 03:59
Structural mockup of a function that enriches payloads with necessary fields before sending down to Kochava
// Handler function, invoked on every 'Audience Entered' and 'Audience Exited' event (i.e. track calls sent outbound from Personas with audience data)
// Invoke function on Track Events. Define Vars
async function onTrack(event, settings) {
var userId = event.userId;
var email = event.traits.email;
var personasAudienceEvent = event.event; // This will resolve to 'Audience Entered' or 'Audience Exited'
var audienceKey = event.properties.audience_key;
var kochavaEndpoint = 'http://control.kochava.com/track/json';
// Learn more about source functions API at
// https://segment.com/docs/connections/sources/source-functions
/**
* Handle incoming HTTP request
*
* @param {FunctionRequest} request
* @param {FunctionSettings} settings
*/
async function onRequest(request) {
async function onTrack(event, settings) {
const endpoint = settings.streamingEndpoint;
let response;
//only sending event if it includes userID. Will not send event if the event only includes anonymousID
if (event.userId) {
let body = {
header: {
schemaRef: {
id: settings.schemaId,
// Handler function, invoked on every identify event (i.e. identify calls sent outbound from Personas with audience data)
// Step 1. Invoke function on identify event. Save userId & email to js vars
async function onIdentify(event, settings) {
var traits = event.traits || {};
var email = traits.email;
var userId = event.userId;
var anonymousId = event.anonymousId;
// Save Clearbit Reveal fields to variables
// Learn more about destination functions API at
// https://segment.com/docs/connections/destinations/destination-functions
/**
* Handle track event
* @param {SegmentTrackEvent} event
* @param {FunctionSettings} settings
*/
async function onTrack(event, settings) {
// Learn more at https://segment.com/docs/connections/spec/track/
// Handler function, invoked on every identify event (i.e. identify calls sent outbound from Personas with audience data)
// Step 1. Invoke function on identify event. Save userId & email to js vars
async function onIdentify(event, settings) {
var traits = event.traits || {};
var email = traits.email;
var userId = traits.userId;
// Step 2. Make HTTP request to Profile API with email
// You can use userId value instead of email as a lookup field. In this case, insert "user_id:${userId_variable_name}" in Profile API URL instead of "email:${email_variable_name}"
@ericwishom
ericwishom / onehope_dest_function_mockup.js
Created February 3, 2021 16:01
Mockup of Segment Destination Function Code to send event back into Segment upon a true value for a SQL trait
// Handler function, invoked on every identify event
// Argument: event = event payload, settings = function settings, as of Jan 2020 it provides access to API Key value.
// Step 1. Invoke func on identify event
async function onIdentify(event, settings) {
event.traits = event.traits || {};
var ce_email = event.traits.email; // Step 2. Pick up email from 'traits' obj of the incoming event
var ce_user_id = event.userId; // Pick up CE userId from the incoming event
var isQualified = event.traits.is_qualified; // Should be true or false. Set by SQL Trait
// Step 3. Make HTTP request to Profile API with the email from previous step
@ericwishom
ericwishom / iterable_mp_mockup.js
Last active September 28, 2021 19:13
Mockup for enriching payloads before pushing into an Iterable Marketing Project endpoint
// Step 1. Invoke func on incoming identify event. onIdentify is a handler function, invoked on every identify event
// Argument: event = event payload, settings = function settings, as of Jan 2020 settings provides access to API Key values.
// It is recommended you store these in Function’s Settings and reference settings.<field_name> in the function below
async function onIdentify(event, settings) {
// Define variables for Iterable Marketing Project access keys and/or IDs and Personas specific keys
const iterableMpId = settings.iterableMpId;
const iterableMpKey = settings.iterableMPKey;
const iterableMpEndpoint = settings.iterableMpEndpoint;
// Step 1. Invoke func on incoming identify event. onIdentify is a handler function, invoked on every identify event
// Argument: event = event payload, settings = function settings, as of Jan 2020 settings provides access to API Key values.
// It is recommended you store these in Function’s Settings and reference settings.<field_name> in the function below
async function onIdentify(event, settings) {
// Define variables for CIO and Personas specific keys
const cioSiteId = settings.cioSiteId;
const cioApiKey = settings.cioApiKey;
const cioEndpoint = settings.cioTrackingEndpoint; // https://customer.io/docs/api/
const personasSpaceId = settings.personasSpaceId;
const personasAccessSecret = settings.personasAccessSecret;
async function onRequest(request, settings) {
const body = await request.json();
console.log(body); // For testing
const mapping =
// StockX team to verify that these mappings are accurate
[
{ oldVal: 'Product Tile', newVal: 'Product Tile Clicked' },
{ oldVal: 'Buy or Bid', newVal: 'Buy or Bid Button Clicked' },
{ oldVal: 'Sell or Ask', newVal: 'Sell or Ask Button Clicked' },
{ oldVal: 'Toggle', newVal: 'Checkout Flow Toggle Clicked' },