send-analytics

A js Library for sending analytics and diagnostics data to a server with page transition and visibility

Usage no npm install needed!

<script type="module">
  import sendAnalytics from 'https://cdn.skypack.dev/send-analytics';
</script>

README

Send Analytics and Diagnostics to Server Asynchronously

sendAnalytics.js is use to schedule asynchronous and non-blocking delivery of data that minimizes resource contention with other time-critical operations, while ensuring that such requests are still processed and delivered to destination. This means:

  • The data is sent reliably
  • It's sent asynchronously
  • It doesn't impact the loading of the next page
  • this library also support transition

Transition happens when a user navigates to a new page, switches tabs, closes the tab, minimizes or closes the browser, or, on mobile, switches from the browser to a different app.

#Supported HTTP Verb

  • POST

#Installation

include sendAnalytics.js to your html, you can grab it from npm

    npm install send-analytics

or from a cdn

    https://cdn.jsdelivr.net/npm/send-analytics

#Usage

send data on DOMdocumentLoad

    
    const obj = { text: 'Hello world' };
    
    const url = "http://localhost/api/hello";
    
    const analytics = sendAnalytics.config()
                      .send(url, obj);

See Examples

React.js Example

#API

config([ transitionObject ])

  • transitionObject < Object > ==> { when: ['visible', 'hidden'] }
    • if no params is passed the once() and repeat() will be ignored
    • data will be sent immediately the DOMdocumentLoad
  • Return < instance of sendAnalytics > which allows you to chain other methods

The Page Transition and Visibility is especially useful for saving resources and improving performance by letting a page avoid performing unnecessary tasks when the document isn't visible

send([ url, data ])

  • url < URL String > ===> The URL that will receive the data. Can be relative or absolute.
  • data < Object > ===> object containing the data to send.
  • Return < instance of sendAnalytics >

once()

  • send data to server once on First visibility state change

repeat()

  • send data on Every visibility state change

onSuccess( [ callback ] )

  • callback < Function >

Note: for you to receive a success Event the onSuccess method must come before the send([ url, data]) method. see example onSuccess

Ref developer.mozilla.org - sendBeacon

#License

the Lamb of God