@standardorg/analytics-fetch
TypeScript icon, indicating that this package has built-in type declarations

1.0.11 • Public • Published

Fetch plugin for analytics

Integration with the browser's fetch API for analytics

Click to expand

Installation

npm install analytics
npm install @standard-org/analytics-fetch

How to use

The @standard-org/analytics-fetch package works in the browser and server-side in Node.js. To use, install the package, include in your project and initialize the plugin with analytics.

Below is an example of how to use the browser plugin.

import Analytics from 'analytics'
import analyticsFetch from "@standardorg/analytics-fetch";

const analytics = Analytics({
  app: "bookclub",
  plugins: [
    analyticsFetch(
      (action) => `https://my-tracking-url.com/${action}`,
    ),
  ],
});

/* Track a page view */
analytics.page()

/* Track a custom event */
analytics.track('cartCheckout', {
  item: 'pink socks',
  price: 20
})

/* Identify a visitor */
analytics.identify('user-id-xyz', {
  firstName: 'bill',
  lastName: 'murray'
})

After initializing analytics with the analyticsFetch plugin, data will be sent to your own analytics endpoint whenever analytics.page, analytics.track, or analytics.identify are called.

Platforms Supported

The @standardorg/analytics-fetch package works in the browser and server-side in Node.js

Browser usage

The Analytics Fetch client side browser plugin works with these analytic api methods:

Browser API

import Analytics from 'analytics'
import analyticsFetch from '@standardorg/analytics-fetch'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    analyticsFetch(
      (action) => `https://my-tracking-url.com/${action}`,
      3,
      (err) => console.error(err)
    )
  ]
})

Configuration options for browser

Option description
getUrl
required - function
A function that returns a string representing the url to which the payload should be sent to
maxDepth
optional - number
Max depth for exponential retries. Defaults to 3
onErr
optional - function
Callback for when any of the requests to your endpoint fail. Signature is (err: any) => any

Server-side usage

The Analytics Fetch server-side node.js plugin works with these analytic api methods:

Server-side API

import Analytics from 'analytics'
import analyticsFetch from "@standardorg/analytics-fetch";

const analytics = Analytics({
  app: "bookclub",
  plugins: [
    analyticsFetch(
      (action) => `https://my-tracking-url.com/${action}`,
      3,
      (err) => console.error(err)
    ),
  ],
});

Configuration options for server-side

Option description
getUrl
required - function
A function that returns a string representing the url to which the payload should be sent to
maxDepth
optional - number
Max depth for exponential retries. Defaults to 3
onErr
optional - function
Callback for when any of the requests to your endpoint fail. Signature is (err: any) => any

Dependencies (1)

Dev Dependencies (9)

Package Sidebar

Install

npm i @standardorg/analytics-fetch

Weekly Downloads

0

Version

1.0.11

License

MIT

Unpacked Size

11.2 kB

Total Files

4

Last publish

Collaborators

  • elvisace