Skip to content

Commit

Permalink
Add function templates
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRy4n committed Feb 21, 2024
1 parent bf296ce commit f7c5b15
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
91 changes: 91 additions & 0 deletions template_destination.js
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');
}
87 changes: 87 additions & 0 deletions template_source.js
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'
}
})
}

0 comments on commit f7c5b15

Please sign in to comment.