fusion-plugin-universal-events-react
The fusion-plugin-universal-events-react
is a drop-in replacement for UniversalEventsToken
from the fusion-plugin-universal-events
. It can be used as a dependency by other Fusion.js plugins and works as an event emitter for data such as statistics and analytics. This plugin captures events emitted from the client, sends them in batches to the server periodically, and allows the server to handle them. Note that due to the batched and fire-and-forget nature of the client-to-server event emission, this library is not suitable for timing-sensitive requests such as error logging or RPC calls.
This plugin also installs a React provider component at the top of the React tree and exports a higher order component that exposes the event emitter to component props.
This plugin is useful for when you want to collect data about user actions or other metrics, and send them in bulk to the server to minimize the number of HTTP requests.
For convenience, this plugin automatically flushes its queue on page unload.
Table of contents
Installation
yarn add fusion-plugin-universal-events-react
Usage
; const Component = { universalEvents;}; Component;
Setup
// main.js;;;; { const app = ; app; __BROWSER__ && app; return app;}
API
Registration API
UniversalEvents
;
The plugin. Provides the service API. Typically should be registered to UniversalEventsToken
.
Dependencies
FetchToken
;// ...__BROWSER__ && app;
Required. Browser-only. See https://github.com/fusionjs/fusionjs/tree/master/fusion-tokens#fetchtoken
Service API
events.on
Registers a callback to be called when an event of a type is emitted. Note that the callback will not be called if the event is emitted before the callback is registered.
events
type: string
- Required. The type of event to listen on. The type*
denotes all events.callback: (mappedPayload: Object, ctx: ?Context) => void
- Required. Runs when an event of matching type occurs. Receives thepayload
after it has been transformed by mapper functions, as well an optional ctx object.
events.emit
events;
type: string
- Required. The type of event to emit. The type*
denotes all events.payload: Object
- Optional. Data to be passed to event handlers
events.map
Mutates the payload. Useful if you need to modify the payload to include metrics or other meta data.
events
type: string
- Required. The type of event to listen on. The type*
denotes all events.callback: (payload: Object, ctx: ?Context) => Object
- Required. Runs when an event of matching type occurs. Should return a modifiedpayload
events.flush
Flushes the data queue to the server immediately. Does not affect flush frequency
events;
events.setFrequency
events;
Sets the frequency at which data is flushed to the server. Resets the interval timer.
frequency: number
- Required.
events.teardown
events;
Stops the interval timer, clears the data queue and prevents any further data from being flushed to the server. Useful for testing
events.from
const scoped = events;
Returns a scoped version of the events api.
ctx: Context
- A Fusion.js context
withBatchEvents
; const Component = { universalEvents;}; Component;
Other examples
Event transformation
It's possible to transform event data with a mapping function, for example to attach a timestamp to all actions of a type.
events; events; events;
ctx
Accessing Event mappers and handlers take an optional second parameter ctx
. For example:
events;
This parameter will be present when events are emitted from the ctx
scoped EventsEmitter instance. For example:
app;
* event type
*
is a special event type which denotes all events. This allows you to add a mapper or handler to all events. For example:
events;