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
Copy file name to clipboardExpand all lines: src/connections/sources/catalog/libraries/website/javascript/index.md
+119-4Lines changed: 119 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Analytics.js 2.0 provides a reduction in page load time, which improves site per
28
28
29
29
### Developer experience
30
30
31
-
Analytics.js 2.0 improves developer experience by introducing new ways for developers to augment events throughout the event timeline. For example, developers can augment events either before or after an event occurs, or while the event is in-flight.
31
+
Analytics.js 2.0 improves developer experience by introducing new ways for developers to augment events throughout the event timeline. For example, developers can augment events either before or after an event occurs, or while the event is in-flight.
32
32
33
33
For example, you can use the Analytics.js 2.0 to build features that:
34
34
@@ -656,6 +656,121 @@ When enabled, Analytics.js automatically retries network and server errors. With
656
656
657
657
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.
658
658
659
+
660
+
## Batching
661
+
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:
662
+
- Delivery of multiple events with fewer API calls, using the batch endpoint
663
+
- Fewer errors if a connection is lost because an entire batch will retry at once rather than multiple calls retrying at random times.
664
+
665
+
### Setup
666
+
You can start batching by changing the parameters for `size` and `timeout` within the `load` method in the analytics object. Batching requires both parameters.
667
+
668
+
```js
669
+
analytics.load("<write_key>", {
670
+
integrations: {
671
+
"Segment.io": {
672
+
deliveryStrategy: {
673
+
strategy:"batching",
674
+
config: {
675
+
size:10,
676
+
timeout:5000
677
+
}
678
+
}
679
+
}
680
+
}
681
+
});
682
+
```
683
+
684
+
You can check to see if batching works by checking your source’s debugger in **Sources > Debugger**. When you select an event and view the **Raw** code, the `receivedAt` time of all the events in the batch should be the same.
685
+
686
+
#### Batch size
687
+
The batch size is the threshold that forces all batched events to be sent once it’s reached. For example, `size: 10` means that after triggering 10 events, Analytics.js sends those 10 events together as a batch to Segment.
688
+
689
+
Your total batched events can’t exceed the maximum payload size of 500 KB, with a limit of 32 KB for each event in the batch. If the 500 KB limit is reached, the batch will be split.
690
+
691
+
#### Timeout
692
+
`timeout` is the number of milliseconds that forces all events queued for batching to be sent, regardless of the batch size, once it’s reached. For example, `timeout: 5000` sends every event in the batch to Segment once 5 seconds passes.
693
+
694
+
### Implicit and explicit batching
695
+
All batching is done implicitly and doesn’t require you to change your Analytics.js implementation.
696
+
697
+
You can fire your analytics events as usual and assume they’ll be delivered the same as individual events would. This code block shows implicit batching:
698
+
699
+
```js
700
+
701
+
// Implicit Batching
702
+
703
+
analytics.page('Checkout')
704
+
analytics.identify('User ID')
705
+
analytics.track('Checkout seen')
706
+
707
+
// some time later
708
+
analytics.track('Product Added')
709
+
analytics.track('Checkout clicked')
710
+
711
+
// These 5 events will potentially be delivered in the same batch, depending on how far apart they're executed (based on batching config)
712
+
```
713
+
714
+
You can get explicit delivery guarantees by changing the implicit code example above to this:
715
+
716
+
```js
717
+
constcheckout=analytics.page('Checkout')
718
+
constidentify=analytics.identify('User ID')
719
+
constseen=analytics.track('Product Added')
720
+
721
+
// some time later
722
+
constadded=analytics.track('Product Added')
723
+
constclicked=analytics.track('Checkout clicked')
724
+
725
+
// Collecting all promises and awaiting on them will create an artificial batch
726
+
// This line will block execution until all events in the artificial batch have been delivered
In the case where all events are delivered without any user behavior, you can explicitly guarantee delivery as shown in this example code:
730
+
731
+
```js
732
+
// Will wait for all events to be delivered before moving on.
733
+
awaitPromise.all([
734
+
analytics.page('Checkout'),
735
+
analytics.identify('User ID'),
736
+
analytics.track('Product Added')
737
+
])
738
+
739
+
// some time later
740
+
analytics.track('Product Added')
741
+
analytics.track('Checkout clicked')
742
+
```
743
+
744
+
### FAQs
745
+
#### Will Analytics.js deliver events that are in the queue when a user closes the browser?
746
+
Analytics.js will do its best to deliver the queued events before the browser closes, but the delivery isn’t guaranteed.
747
+
748
+
Upon receiving the `beforeunload` browser event, Analytics.js attempts to flush the queue using `fetch` requests with `keepalive` set to true. Since the max size of `keepalive` payloads is limited to 64 KB, if the queue size is bigger than 64 KB at the time the browser closes, then there is a chance of losing a subset of the queued events. Reducing the batch size or timeout will alleviate this issue, but that will be a trade-off decision.
749
+
750
+
#### Is Batching supported on Analytics.js classic?
751
+
No, Batching is only supported as part of Analytics.js 2.0.
752
+
753
+
#### Can other destinations receive batched events?
754
+
No, this batching only impacts events sent to Segment. Once the batch reaches Segment, it is split up and follows the normal path of an event.
755
+
756
+
#### Will batching impact billing or throughput?
757
+
No, batching won’t impact billing or throughput.
758
+
759
+
#### Can I use batching with partner integrations?
760
+
Partner integrations don’t support batching as all other partner integrations run one event at a time. Only Segment.io events support batched delivery.
761
+
762
+
#### Does batching work on all browsers?
763
+
Batching won’t work on Internet Explorer.
764
+
765
+
#### If a source has retry enabled, does the retry behavior change when using batching?
766
+
Batching delays retries, as events that are queued for batching aren’t retried until a batch delivery fails.
767
+
768
+
#### When using Middlewares as a source and destination, is there a change in behavior when using batching?
769
+
No, there is no change in behavior to Middlewares.
770
+
771
+
#### When using Segment features (Schema filtering, integrations object, Protocols) to filter events from going to destinations (device and cloud-mode), will batching impact the filtering of events?
772
+
No, there is no impact to how events filter.
773
+
659
774
## Plugins
660
775
661
776
Segment offers video player 'plugins' so you can quickly collect video events using Analytics.js. See the specific documentation below to learn more:
License: MIT License, available at: [https://github.com/developit/unfetch/blob/master/LICENSE.md](https://github.com/developit/unfetch/blob/master/LICENSE.md)
845
+
License: MIT License, available at: [https://github.com/developit/unfetch/blob/master/LICENSE.md](https://github.com/developit/unfetch/blob/master/LICENSE.md)
0 commit comments