-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_source.js
87 lines (77 loc) · 2.12 KB
/
template_source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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'
}
})
}