@beakyn/activity-tracker
TypeScript icon, indicating that this package has built-in type declarations

2.4.2 • Public • Published

Beakyn Activity Tracker

This is a custom redux-beacon integration that allows us to have our very own User Activity tracking API as a target.

Table of Contents

Usage

First of all, you must add both redux-beacon and @beakyn/activity-tracker as dependencies.

Then, you should have a config object with the following properties:

  • appName (string): The application which is being tracked
  • tenant (string): The current tenant
  • token (string): The logged user jwt token
  • url (string): The Activity API instance url
  • resourceId (string): Either the user email or their ID, depending on resourceType
  • resourceType (ResourceType): Either .email or .user, depending on resourceId

For example:

import { ResourceType } from '@beakyn/activity-tracker';

const config = {
  appName: 'My Applictation',
  tenant: 'My tenant',
  token: '...',
  url: '...',
  resourceId: '...',
  resourceType: ResourceType.email,
};

Note: This module detects if it's running in localhost and does nothing there to avoid polluting your analytics with local data. In order to allow traking while on development – and debug sent requests or similar – you may want to add a devMode: true property to your config object.

Next step is to define a custom event map like the following:

import { actions as bknAuthActions } from 'bkn-auth';
import { ActivityService } from '@beakyn/activity-tracker';

const { trackEvent } = ActivityService.getInstance(config);

const eventsMap = {
  [bknAuthActions.logout.getType()]: trackEvent(action => {
    return {
      feature: 'App',
      action: 'Logged out',
    };
  }),
};

Last but not least, head over to your Redux store:

import { applyMiddleware, compose, createStore } from 'redux';
import { createMiddleware } from 'redux-beacon';

import { ActivityMiddleware, ResourceType } from '@beakyn/activity-tracker';

import { rootReducer, RootState } from './rootReducer';

import { config, eventsMap } from './utils/activity'; // Or somewhere else you have both

const activityInstance = ActivityMiddleware(config);

const activityMiddleware = createMiddleware(eventsMap, activityInstance);

const middlewares = [activityMiddleware];

const storeEnhancers = [applyMiddleware(...middlewares)];

export const store = createStore<RootState>(
  rootReducer,
  compose(...storeEnhancers)
);

Then you're done! 🚀

License

This project is licensed under the terms of the MIT license.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.4.2
    9
    • latest

Version History

Package Sidebar

Install

npm i @beakyn/activity-tracker

Weekly Downloads

11

Version

2.4.2

License

MIT

Unpacked Size

131 kB

Total Files

35

Last publish

Collaborators

  • nelsoneldoro
  • jpbeakyn
  • juanpujol
  • abraaoalves
  • falcaoaugustos
  • brunolazarus
  • hileomsi
  • murilopaixao