I want to understand how data is sent to collector,
whenever any event occurs?
is it sent using XMLHttpRequest object ?
or something like WebSocket?
Events are sent whenever one of the ‘track*’ methods are called. If the local storage is available and enabled during tracker initialisation (default is on), then the tracker will first store the event in local storage before sending it. This ensures that if a page unload occurs before we send the event, we will be able to send the event when the next page loads. Beyond that though, you can expect events to be sent as soon as they occur. This can be adjusted using the
bufferSize option, but we strongly advise this is left at 1, as you may lose events (i.e. if you set the buffer to 5 but only 3 events occur, then nothing will be sent to the collector).
We utilise three different ways to send events. These different options can be specified during tracker initialisation using the
eventMethod property. This can be either
- Beacon uses the new sendBeacon API which passes the burden to the browser to send the event. This API is designed for sending analytical events - although Chromium doesn’t currently support sending JSON via Beacon so we fallback to Post on Chromium based browsers (Chrome, Opera, Edge, etc.).
- Post utilises XMLHttpRequest to POST a JSON request to a Snowplow collector that follows the Snowplow Tracker Protocol.
- Get sends a GET request to the collectors
/iendpoint. Due to character length limits in browsers, we generally advise users to use the Post or Beacon options.
Hope that helps clear things up for you. Our Technical Docs help explain some of this further, particularly when considering the tracker initialisation options.