analytics
Fetch plugin for 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:
- analytics.page - Sends page views into your analytics url endpoint
- analytics.track - Track custom events and send to your analytics url endpoint
- analytics.identify - Identify visitors and send details to your analytics url endpoint
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:
- analytics.page - Sends page views into your analytics url endpoint
- analytics.track - Track custom events and send to your analytics url endpoint
- analytics.identify - Identify visitors and send details to your analytics url endpoint
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 |