Installation
Segment
Installing Spectacle with Segment
While we hope to have an official integration in the Segment directory in the near future, for now you can use the following code to install Spectacle with Segment.
In Segment, go to Connections → Catalog → Select the "Functions" tab → New Function
Then, paste the following code into the Edit source code field:
const BASE_URL = 'https://api.spectaclehq.com/tracking';
const WORKSPACE_ID = "YOUR_WORKSPACE_ID";
/**
* Handle track event
* @param {SegmentTrackEvent} event
* @param {FunctionSettings} settings
*/
async function onTrack(event, settings) {
return await proxyEvent('track', event);
}
/**
* Handle identify event
* @param {SegmentIdentifyEvent} event
* @param {FunctionSettings} settings
*/
async function onIdentify(event, settings) {
return await proxyEvent('identify', event);
}
/**
* Handle page event
* @param {SegmentPageEvent} event
* @param {FunctionSettings} settings
*/
async function onPage(event, settings) {
return await proxyEvent('page', event);
}
/**
* Proxy event to Spectacle API
* @param {SegmentEvent} event
*/
async function proxyEvent(path, event) {
const endpoint = BASE_URL + path; // replace with your endpoint
let response;
event.workspaceId = WORKSPACE_ID;
try {
response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': event.context.userAgent,
},
body: JSON.stringify(event)
});
} catch (error) {
// Retry on connection error
throw new RetryError(error.message);
}
if (response.status >= 500 || response.status === 429) {
// Retry on 5xx (server errors) and 429s (rate limits)
throw new RetryError(`Failed with ${response.status}`);
}
}
/**
* Handle group event
* @param {SegmentGroupEvent} event
* @param {FunctionSettings} settings
*/
async function onGroup(event, settings) {
throw new EventNotSupported('group is not supported');
}
/**
* Handle screen event
* @param {SegmentScreenEvent} event
* @param {FunctionSettings} settings
*/
async function onScreen(event, settings) {
throw new EventNotSupported('screen is not supported');
}
/**
* Handle alias event
* @param {SegmentAliasEvent} event
* @param {FunctionSettings} settings
*/
async function onAlias(event, settings) {
throw new EventNotSupported('alias is not supported');
}
/**
* Handle delete event
* @param {SegmentDeleteEvent} event
* @param {FunctionSettings} settings
*/
async function onDelete(event, settings) {
throw new EventNotSupported('delete is not supported');
}
Remember to replace YOUR_WORKSPACE_ID
with your actual workspace id.