Segment: Custom Functions

This solution is intended for use when you want to track a backend event in Everflow which is not tied directly to your website.

peter kurjanowicz avatar
Written by peter kurjanowicz
Updated over a week ago

Table of Contents


Overview

Please note that this setup should only be executed by an advanced user. The main reason to utilize this solution instead of the built-in Segment integration, is if you want to use email attribution to tie an event from Segment to an event in Everflow.

Segment has some great documentation on how their functions work.
We encourage you to check it out - [Click Here]


How-To Guide with Example

The steps below will follow an example which will be using the onTrack function to watch for analytics.track() events sent to Segment and then pull data out such as userId or email to send that to Everflow.

Step #1 - Configure Event In Everflow

  • Configure the Advertiser-Level Event for tracking from Segment to Everflow. For instructions - [Click Here]

Step #2 - Copy Values From Everflow

  • Pull your Tracking Domain, Advertiser ID (aid), and Advertiser Event ID (adv_event_id) and implement it inside the code below.

Here is where to find the aid and adv_event_id. In this example, the aid is 139 and the adv_event_id is 26.

Please note that your Tracking card could look different if Javascript SDK is not selected as the Conversion Method. If so, edit the the Conversion Method before moving on by:

  • navigating to Edit > Attribution > Conversion Method: Javascript SDK.

Step #3 - Code Prep

  • Copy the following code so that you may paste it into Segment. Be sure to replace INSERT_TRACKING_DOMAIN, INSERT_AID, and INSERT_ADV_EVENT_ID with the values you copied in Step #2.

async function onTrack(event, settings) {
const eventName = event.event;
const userId = event.userId;
const endpoint = 'INSERT_TRACKING_DOMAIN?aid=INSERT_AID&adv_event_id=INSERT_ADV_EVENT_ID&email=' + userId;
let response;
try {
response = await fetch(endpoint, {
method: 'GET'
});
} catch (error) {
throw new RetryError(error.message);
}
if (response.status >= 500 || response.status === 429) {
throw new RetryError(`Failed with ${response.status}`);
}
}

Below is an example showing the code with our sample values plugged in:

async function onTrack(event, settings) {
const eventName = event.event;
const userId = event.userId;
const endpoint = 'https://www.serve-eflow-test.com?aid=139&adv_event_id=26&email=' + userId;
let response;
try {
response = await fetch(endpoint, {
method: 'GET'
});
} catch (error) {
throw new RetryError(error.message);
}
if (response.status >= 500 || response.status === 429) {
throw new RetryError(`Failed with ${response.status}`);
}
}

Step #4 - Configure Function In Segment

  • In Segment, navigate to Catalog > Functions.

  • Then, click the blue [+ New Function] button on the upper right side. Select Destination, then click Build.

Step #5 - Implement Code In Segment

  • Delete the code example that is currently in the box.

  • Replace it with the code you prepared in Step #3, then click Configure.

Step #6 - Tie Event To Destination In Segment

  • Name the function and click Create Function.

  • Then, click [+ Connect Destination].

  • Select the destination.

  • Then, name it and click Save.

Step #7 - Test

  • You are now ready to test!


More Information

  • Use the following code if you would like to fire the conversion only on specific events:

async function onTrack(event, settings) {
const eventName = event.event;
const userId = event.userId;
const endpoint = 'INSERT_TRACKING_DOMAIN?aid=INSERT_ADV_ID&adv_event_id=INSERT_ADV_EVENT_ID&email=' + userId;
let response;
if (eventName === 'test') {
try {
response = await fetch(endpoint, {
method: 'GET'
});
} catch (error) {
throw new RetryError(error.message);
}
if (response.status >= 500 || response.status === 429) {
throw new RetryError(`Failed with ${response.status}`);
}
}
}

  • You must replace 'test' with the name of the event inside Segment. You can find the name of the event by clicking Sources, clicking the source and then the Debugger.

**Please note we also use userId in the example above, as you can use any data from the analytics.track call. Here is an example showing all the data using the debugger:


Did this answer your question?