Skip to content

Commit 7ae64ea

Browse files
authored
Merge pull request #7369 from segmentio/add-ajs-settings-info
Add customer header and `keepalive` info to Analytics.js documentation
2 parents 49f4e09 + 5bb1ed8 commit 7ae64ea

File tree

1 file changed

+49
-9
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+49
-9
lines changed

src/connections/sources/catalog/libraries/website/javascript/index.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ The Analytics.js utility methods help you change how Segment loads on your page.
328328
- [On (Emitter)](#emitter)
329329
- [Timeout](#extending-timeout)
330330
- [Reset (Logout)](#reset-or-log-out)
331-
- [Keepalive](#keepalive)
332331

333332
### Load
334333

@@ -372,7 +371,7 @@ If you want to access end-tool library methods that do not match any Analytics.j
372371

373372

374373
```js
375-
analytics.ready(function() {
374+
analytics.ready(() => {
376375
window.mixpanel.set_config({ verbose: true });
377376
});
378377
```
@@ -422,7 +421,7 @@ analytics.on(method, callback);
422421
Example:
423422

424423
```js
425-
analytics.on('track', function(event, properties, options) {
424+
analytics.on('track', (event, properties, options) => {
426425

427426
bigdataTool.push(['recordEvent', event]);
428427

@@ -461,11 +460,6 @@ The `reset` method only clears the cookies and `localStorage` created by Segment
461460

462461
Segment doesn't share `localStorage` across subdomains. If you use Segment tracking on multiple subdomains, you must call `analytics.reset()` for each subdomain to completely clear out the user session.
463462

464-
### Keepalive
465-
466-
You can utilize this in instances where an API call fires on a hard redirect, and are missed from getting captured in Segment. If you set this flag to true, it enables firing the event before the redirect. This is available for all events. You can read more about this in the [Github PR](https://github.com/segmentio/analytics-next/issues/768#issuecomment-1386100830){:target="_blank"}.
467-
468-
469463
## Managing data flow with the Integrations object
470464

471465
> success ""
@@ -530,7 +524,7 @@ analytics.load('writekey', { integrations: { All: false, 'Google Analytics': tru
530524
This way, you can conditionally load integrations based on what customers opt into on your site. The example below shows how you might load only the tools that the user agreed to use.
531525

532526
```js
533-
onConsentDialogClosed(function(consentedTools){
527+
onConsentDialogClosed((consentedTools) => {
534528
analytics.load('writekey', { integrations: consentedTools })
535529
})
536530
```
@@ -588,6 +582,52 @@ When enabled, Analytics.js automatically retries network and server errors. With
588582

589583
Analytics.js stores events in `localStorage` and falls back to in-memory storage when `localStorage` is unavailable. It retries up to 10 times with an incrementally increasing back-off time between each retry. Analytics.js queues up to 100 events at a time to avoid using too much of the device's local storage. See the [destination Retries documentation](/docs/connections/destinations/#retries) to learn more.
590584

585+
## Delivery strategy configuration
586+
587+
The `deliveryStrategy.config` object lets you customize how data is delivered to Segment. This includes options like setting custom headers and enabling `keepalive` to capture events during hard redirects.
588+
589+
### Adding custom headers
590+
591+
You can override default headers by providing custom headers in your configuration. Use the `deliveryStrategy.config.headers` option to specify the headers, like in the following example:
592+
593+
```ts
594+
analytics.load("<YOUR_WRITE_KEY>", {
595+
integrations: {
596+
'Segment.io': {
597+
deliveryStrategy: {
598+
config: {
599+
headers: { 'x-api-key': 'foo' }
600+
}
601+
}
602+
}
603+
}
604+
});
605+
606+
## Keepalive
607+
608+
You can use the `keepalive` option to make sure that Segment captures API calls triggered during a hard redirect. When enabled, `keepalive` will try to fire events before the redirect occurs.
609+
610+
By default, `keepalive` is set to false, because all fetch requests with the `keepalive` flag are subject to a 64kb size limit. Additionally, `keepalive` requests share this size limit with all other in-flight `keepalive` requests, regardless of whether they're related to Segment. This competition for resources can lead to data loss in some scenarios.
611+
612+
Segment only uses `keepalive` by default if:
613+
- The browser detects that the page is unloading (like if the user closes the tab or navigates away).
614+
- You have batching enabled.
615+
616+
To enable `keepalive`, use the following configuration:
617+
618+
```ts
619+
analytics.load("<YOUR_WRITE_KEY>", {
620+
integrations: {
621+
'Segment.io': {
622+
deliveryStrategy: {
623+
config: {
624+
keepalive: true
625+
}
626+
}
627+
}
628+
}
629+
});
630+
```
591631

592632
## Batching
593633
Batching is the ability to group multiple requests or calls into one request or API call. All requests sent within the same batch have the same `receivedAt` time. With Analytics.js, you can send events to Segment in batches. Sending events in batches enables you to have:

0 commit comments

Comments
 (0)