You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For Business plans with access to Regional Segment, you can use the host configuration parameter to send data to the desired region:
45
+
46
+
Oregon (Default) — api.segment.io/v1
47
+
Dublin — events.eu1.segmentapis.com
48
+
An example of setting the host to the EU endpoint using the Node library would be:
49
+
50
+
```ts
51
+
const analytics =newAnalytics('YOUR_WRITE_KEY', {
52
+
host: "https://events.eu1.segmentapis.com"
39
53
});
40
54
```
41
55
42
56
## Complete Settings / Configuration
43
57
See complete list of settings in the [AnalyticsSettings interface](src/app/settings.ts).
44
58
```ts
45
-
newAnalytics({
59
+
const analytics =newAnalytics({
46
60
writeKey: '<MY_WRITE_KEY>',
61
+
plugins: [plugin1, plugin2],
47
62
host: 'https://api.segment.io',
48
63
path: '/v1/batch',
64
+
maxRetries: 3,
65
+
maxEventsInBatch: 15,
49
66
flushInterval: 10000,
50
-
plugins: [plugin1, plugin2],
51
67
// ... and more!
52
68
})
53
69
54
70
```
55
71
56
-
## Graceful Shutdown
72
+
## Batching
73
+
Our libraries are built to support high performance environments. That means it is safe to use our Node library on a web server that’s serving thousands of requests per second.
74
+
75
+
Every method you call does not result in an HTTP request, but is queued in memory instead. Messages are then flushed in batch in the background, which allows for much faster operation.
76
+
77
+
By default, our library will flush:
78
+
79
+
- The very first time it gets a message.
80
+
- Every 15 messages (controlled by `settings.maxEventsInBatch`).
81
+
- If 10 seconds has passed since the last flush (controlled by `settings.flushInterval`)
82
+
83
+
There is a maximum of 500KB per batch request and 32KB per call.
84
+
85
+
If you don’t want to batch messages, you can turn batching off by setting the `maxEventsInBatch` setting to 1, like so:
Batching means that your message might not get sent right away. But every method call takes an optional callback, which you can use to know when a particular message is flushed from the queue, like so:
Different parts of your application may require different types of batching, or even sending to multiple Segment sources. In that case, you can initialize multiple instances of Analytics with different settings:
- If you want to disable analytics for unit tests, you can use something like [nock](https://github.com/nock/nock) or [jest mocks](https://jestjs.io/docs/manual-mocks).
263
+
264
+
You should prefer mocking. However, if you need to intercept the request, you can do:
265
+
266
+
```ts
267
+
// Note: nock will _not_ work if polyfill fetch with something like undici, as nock uses the http module. Undici has its own interception method.
268
+
importnockfrom'nock'
269
+
270
+
const mockApiHost ='https://foo.bar'
271
+
const mockPath ='/foo'
272
+
273
+
nock(mockApiHost) // using regex matching in nock changes the perf profile quite a bit
0 commit comments