-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// FUNCTION_NAME: template_destination | ||
|
||
/** | ||
* Handle track event | ||
* @param {SegmentTrackEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onTrack(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/track/ | ||
const endpoint = ''; // replace with your endpoint | ||
let response; | ||
|
||
try { | ||
response = await fetch(endpoint, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${btoa(settings.apiKey + ':')}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
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 identify event | ||
* @param {SegmentIdentifyEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onIdentify(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/identify/ | ||
throw new EventNotSupported('identify is not supported'); | ||
} | ||
|
||
/** | ||
* Handle group event | ||
* @param {SegmentGroupEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onGroup(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/group/ | ||
throw new EventNotSupported('group is not supported'); | ||
} | ||
|
||
/** | ||
* Handle page event | ||
* @param {SegmentPageEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onPage(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/page/ | ||
throw new EventNotSupported('page is not supported'); | ||
} | ||
|
||
/** | ||
* Handle screen event | ||
* @param {SegmentScreenEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onScreen(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/screen/ | ||
throw new EventNotSupported('screen is not supported'); | ||
} | ||
|
||
/** | ||
* Handle alias event | ||
* @param {SegmentAliasEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onAlias(event, settings) { | ||
// Learn more at https://segment.com/docs/connections/spec/alias/ | ||
throw new EventNotSupported('alias is not supported'); | ||
} | ||
|
||
/** | ||
* Handle delete event | ||
* @param {SegmentDeleteEvent} event | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onDelete(event, settings) { | ||
// Learn more at https://segment.com/docs/partners/spec/#delete | ||
throw new EventNotSupported('delete is not supported'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// FUNCTION_NAME: template_source | ||
|
||
/** | ||
* Handle incoming HTTP request | ||
* | ||
* @param {FunctionRequest} request | ||
* @param {FunctionSettings} settings | ||
*/ | ||
async function onRequest(request, settings) { | ||
const body = request.json() | ||
|
||
const endpoint = ''; // replace with your endpoint | ||
let response | ||
|
||
try { | ||
response = await fetch(endpoint, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${btoa(settings.apiKey + ':')}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}); | ||
} 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}`) | ||
} | ||
|
||
// See https://segment.com/docs/connections/spec/track/ | ||
Segment.track({ | ||
event: 'Test Event', | ||
userId: 'user_id', | ||
properties: { | ||
testProperty: 'testValue', | ||
testProperty2: response.propertyName, | ||
} | ||
}) | ||
|
||
// See https://segment.com/docs/connections/spec/identify/ | ||
Segment.identify({ | ||
userId: 'user_id', | ||
traits: { | ||
userName: 'Unicorn' | ||
} | ||
}) | ||
|
||
// See https://segment.com/docs/connections/spec/group/ | ||
Segment.group({ | ||
groupId: 'group_id', | ||
userId: 'user_id', | ||
traits: { | ||
groupName: 'Unicorn' | ||
} | ||
}) | ||
|
||
// See https://segment.com/docs/connections/spec/page/ | ||
Segment.page({ | ||
name: 'Home page', | ||
userId: 'user_id', | ||
properties: { | ||
url: 'https://mywebsite.com/about' | ||
} | ||
}) | ||
|
||
// See https://segment.com/docs/connections/spec/screen/ | ||
Segment.screen({ | ||
name: 'Test Screen', | ||
userId: 'user_id', | ||
properties: { | ||
url: 'https://mywebsite.com/about' | ||
} | ||
}) | ||
|
||
// See https://segment.com/docs/connections/sources/catalog/libraries/server/object-api/ | ||
Segment.set({ | ||
collection: 'users', | ||
id: 'user_id', | ||
properties: { | ||
userName: 'Unicorn' | ||
} | ||
}) | ||
} |