5.0.14 • Published 1 year ago

@kano/kbc-telemetry v5.0.14

Weekly downloads
1,327
License
ISC
Repository
-
Last release
1 year ago

kbc-telemetry

Telemetry module for boilerplate apps, using react-tracking. For more information, see NYTimes React Tracking.

kbc-telemetry also handles when the application goes offline, and refreshes the session (default: set to 1 minute interval checks and ends session after 10 minutes of inactivity).

Usage

Setup

Import and use TelemetryProvider component as a wrapper for your app:

import { TelemetryProvider } from '@kano/kbc-telemetry';

Wrap your app in <TelemetryProvider> (IMPORTANT: this should be a top level wrapper only second to <Router>) and pass config information. To start tracking you will need to pass the trackUser function as props to your App component. If the app is user-less, you can set this to null on app first load.

const config = {
    app: app.name,
    appVersion: app.version
    url: app.url,
    env: app.env,
    trackingInterval?: 1000, // Check and send tracking data to database - Optional
    refreshIdleSessionInterval?: 30 * 1000, // Check if user is still active but not sending events - Optional
    sessionSoftTimeout?: 1 * 60 * 1000, // Soft session timeout default with possibility of a resume_session - Optional
    sessionHardTimeout?: 3 * 60 * 1000, // End the session without possibility of a resume_session event unless another tab has been updating the cookie session - Optional
    refreshCookieInterval?: 1 * 60 * 1000, // Refresh any available cookie session - Optional
    hasCrossDomainStorage?: boolean, //  cross-domain storage opt in/out - Optional
}

render() {
    return (
        <Router>
            <TelemetryProvider config={config}>
                {(trackUser) => (
                    <App trackUser={(userId) => trackUser(userId)} />
                )}
            </TelemetryProvider>
        </Router>
    );
}

Within components

Tracking data can be sent in multiple ways:

Decorators

Either on component level or at method level.

import { track } from '@kano/kbc-telemetry';

@track({ event: 'name_of_your_event' })
class MyComponent extends Component {

    @track({ event: 'I_did_something', action: 'click' })
    handleSomething() {
        // I run something
    }
}

Above shows the initial parameter as an object. But it can also be sent as a function, which accesses 3 parameters component props, component state, function arguments as an array, for example:

@track((props, state, args) => ({ event: props.event, data: { something: state.something, args: args[0] } })
handleSomething(iAmArgsZero) {
    // I run something
}

For decorators, there are explict fields that can be set as follows:

DecoratorField Options
@track()event?: String data?: Any action?: String module?: String
@trackError()error: { name: String, stack: String, message: String }

Props

You can also use as props: this.props.tracking.trackEvent({}). This currently doesn't enforce the field options above.

To use tracking in props, the component must be wrapped using one of the 3 options below:

Opt 1) export / track()(Comp)

Wrapping it in track(), as follows (NOTE: the decorator must be passed an event object, the most common way to do this is by passing a module property - if you do not wish to pass anything please see Opt 3):

import { track, ITrackingProps } from '@kano/kbc-telemetry';

const FooComp = (props: ITrackingProps) => {
	return <div onClick={() => props.tracking.trackEvent({ action: 'click' })} />;
}

export default track({
  module: 'FooComp',
})(FooComp);
Opt 2) decorator / @track()class

Wrapping it with the @track({ }) decorator (NOTE: the decorator must be passed an event object, the most common way to do this is by passing a module property - if you do not wish to pass anything please see Opt 3):

import { track, ITrackingProps } from '@kano/kbc-telemetry';

@track({ module: 'MyComponent' })
class MyComponent extends Component<any, ITrackingProps> {
	handleSomething() {
	    this.props.tracking.trackEvent({ event: 'i_handle_something' })
	}
}
Opt 3) HOC / withTracking

Wrapping your exported component using the withTracking higher order component:

import { withTracking, ITrackingProps } from '@kano/kbc-telemetry';

class MyComponent extends Component<any, ITrackingProps> {
	handleSomething() {
	    this.props.tracking.trackEvent({ event: 'i_handle_something' })
	}
}

export default withTracking(FooComp);

Hooks

You can also gain access to the tracking.trackEvent({}) using the useTracking react hook:

import { useTracking } from '@kano/kbc-telemetry';

const MyComp = () => {
    const tracking = useTracking();

    const handleTrackedClick = () => {
        tracking.trackEvent({ event: 'omg' });
    }

    return {
        <button onClick={handleTrackedClick}>Omg please track me</button>
    }
}

Tracking Errors

Currently trackError can only be used as a decorator. See below and see field options in the table above.

@trackError({ error: { name: 'handle_error', stack: 'stack', message: 'message' } })
handleError() {
    // I handle an error
}

If you want to track errors using props, you can follow the same principles as above (using trackEvent as the function) but just pass an error event:

handleSomething() {
        this.props.tracking.trackEvent({ error: { name: 'handle_error', stack: 'stack', message: 'message' } })
}

Offline Client

The TelemetryProvider is wrapped with the OfflineClientProvider, this will pass down the status of online when the application changes from online to offline and vice versa. If all apps are wrapped with the TelemetryProvider, a hoc withOfflineClient can be used on any component of the application, for example:

const Editor = withOfflineClient(EditorComponent);
export Editor;

You can either listen to changes in componentDidUpdate for the offlineClient.online or to check the current status you can call offlineClient.getOnlineStatus.

Alternatively, if the application is not wrapped in the TelemetryProvider, you can use the OfflineClientProvider as a standalone component:

<OfflineClientProvider>
    {({ offlineClient: { online } }: IOfflineClientAPI) => {
        <App {...offlineClient} />
    }}
</OfflineClientProvider>
5.0.12

1 year ago

5.0.13

1 year ago

5.0.14

1 year ago

5.0.9

1 year ago

5.0.8

1 year ago

5.0.10

1 year ago

5.0.11

1 year ago

5.0.7

2 years ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.1.7

3 years ago

4.2.0

3 years ago

4.1.6

3 years ago

4.1.5

3 years ago

4.1.4

3 years ago

4.1.3

3 years ago

4.0.31

3 years ago

4.1.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.0.27

3 years ago

4.0.26

3 years ago

4.0.29

3 years ago

4.0.28

3 years ago

4.0.25

3 years ago

4.0.24

3 years ago

4.0.30

3 years ago

4.0.23

3 years ago

4.0.21

3 years ago

4.0.22

3 years ago

4.0.20

3 years ago

4.0.19

3 years ago

4.0.18

3 years ago

4.0.17

3 years ago

4.0.16

3 years ago

4.0.15

3 years ago

4.0.14

3 years ago

4.0.12

3 years ago

4.0.13

3 years ago

4.0.10

3 years ago

4.0.11

3 years ago

4.0.7

3 years ago

4.0.9

3 years ago

4.0.8

3 years ago

4.0.6

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-pre.2

3 years ago

3.0.0-pre.1

3 years ago

3.0.0-pre.0

3 years ago

2.3.12

3 years ago

2.3.8

3 years ago

2.3.9

3 years ago

2.3.11

3 years ago

2.3.10

3 years ago

2.3.7

3 years ago

2.3.6

3 years ago

2.3.5

3 years ago

2.3.2

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.10

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.7

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1-alpha.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

4.25.2-alpha.0

4 years ago

4.25.0-alpha.1

4 years ago

4.25.0-alpha.0

4 years ago

4.24.0-alpha.0

4 years ago

4.23.1-alpha.0

4 years ago

4.23.0-alpha.0

4 years ago

4.22.24-alpha.0

4 years ago

4.22.20-alpha.0

4 years ago

4.22.14-alpha.0

4 years ago

4.22.11-alpha.0

4 years ago

4.22.1-alpha.0

4 years ago

4.21.6-alpha.0

4 years ago

4.21.1-alpha.0

4 years ago

4.21.0-alpha.0

4 years ago

4.17.1-alpha.0

4 years ago

4.16.3-alpha.0

4 years ago

4.14.10-alpha.0

4 years ago

4.13.5-alpha.0

4 years ago

4.13.0-alpha.0

4 years ago

4.12.0-alpha.0

4 years ago

4.11.5-alpha.0

4 years ago

4.10.1-alpha.0

4 years ago

4.8.0-alpha.0

4 years ago

4.5.0-alpha.0

4 years ago

4.4.1-alpha.0

4 years ago

4.4.0-alpha.0

4 years ago

4.3.0-alpha.0

4 years ago

4.2.2-alpha.0

4 years ago

4.2.1-alpha.0

4 years ago

4.1.1-alpha.0

4 years ago

4.1.0-alpha.0

4 years ago

4.0.0-alpha.0

4 years ago