React Spy
A set of utilities for collecting UX-analytics of your React-application (ex: clicks, shows, errors and etc.)
npm i --save react-spy
Features
- Easy integration with any ui-library (ex: Ant-Design)
- Full control over the events
API
- spy — decorator of react-components
- addSpyObserver — add observer of events
- addSpyErrorObserver — add observer of errors
- intercept — intercepting a chain of events
- Components
- Low Level
- broadcast — broadcast any chain of events
- broadcastError — broadcast any error
Usage
For example with Google Analytics
// Btn.js; const Btn = <button name=name>value</button>; name // Computed `id` on based component properties listen: 'click' // DOM-events listBtn; // LoginForm.js; Component // ... { evt; try await api; spy; catch err spy; } { return <form onSubmit=thishandleEvent> /* ... */ <Btn name="login" value="Sign In"/> <Btn name="forgot" value="Forgot password"/> </form> ; } id: "login-form" host: true listen: 'mount' 'unmount'LoginForm; // boot.js; ; // Component Errors; ReactDOM;
spy<Props>(options)
Decorate the component to collect analytics
options
- id:
string | (props, context?) => string
— default @seepropName
description - propName:
string
— prop-name forid
, by defaultspyId
- listen:
string[]
— DOM-events to listen +error
,mount
andunmount
- callbacks — list of observed callbacks that are passed to it via
props
- propName:
string
— name of the property responsible for the spy'sid
, by defaultspyId
- host:
boolean
- id:
; name listen: 'click' { return <button>value</button>;} // Somewhere in the code<Btn name="login" value="Sign in"/>// *click* -> ["login", "click"]
spy.send(cmp: React.Component, chain: string | string [], detail?: object): void
Send stats from the component and not only
- cmp:
React.Component
— instance ofReact.Component
- chain:
string | string[]
— name of metric - detail:
object
; id: 'parent'Component { return <button onClick= {spy;}>First</button> <button onClick= {spy;}>Second</button> ; }; // Somewhere in a code// click on <First>:// - ["parent", "foo"] {}// click on <Second>:// - ["parent", "bar", "baz"] {val: 123}//// Somewhere in an another place:// spy.send(['global', 'label'], {time: 321}):// - ["global", "label"] {time: 321}
spy.error(cmp: React.Component, chain: string | string [], error: Error): void
send an error from the component and not only
- cmp:
React.Component
— instance ofReact.Component
- chain:
string | string[]
— name of metric - error:
Error
— any an error
addSpyObserver(fn: (chain: string[], detail: object) => void): UnsubsriberFn
Add observer of events for sending to the accounting system of analytics
; ; // Somewhere (if you need to)unsubscribe;
addSpyErrorObserver(fn: (detail: ErrorDetail) => void): UnsubsriberFn
Add observer of component errors
detail
- error:
Error
— JavaScript error - info:
object
— React error info - chain
string[]
— spyid
chain
- error:
; addSpyErrorObserver;
intercept(rules: InterceptRules)
Intercepting a chain of events
; intercept;
<Spy>...</Spy>
import Spy from 'react-spy'; const SomeFragment = condition onShowDetail <div> <Spy ="top"> <Button ="detail" ="Show detail" =/> </Spy> condition && <Spy ="bottom" => Detail </Spy> </div>; // 1. *click on button* -> ["top", "detail", "click"]// 2. *mounting* -> ["bottom", "mount"]
<SpyStep name="..."/>
The hidden spy element for steps monitoring
- name:
string
— a step name - enter:
string | string[]
— the enter phase (optional) - leave:
string | string[]
— the leave phase (optional)
broadcast(chain: string[], detail?: object)
; broadcast, ;// or just// spy.send(['custom', 'event', 'chain'], {value: 'Wow'})
broadcastError(detail: ErrorDetail)
detail
- error:
Error
— JavaScript error - chain
string[]
— spyid
chain - info:
object
— React error info (optional)
- error:
; broadcastError;// or just// spy.error('localStorage', new Error('Read'));// spy.error(thisReactCmp, 'localStorage', new Error('save'));
Development
npm i
npm test
, code coverage