1.0.3 • Published 5 years ago

uieventdispatcher v1.0.3

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

EventDispatcher

Build Status Language grade: JavaScript Coverage Status code style: prettier

Process events, post events to server in batch, enrich events by appendig additonal data using simple configuration. Persists events in case of browser reload, closing the browser window (local storage used).

Usage

EventDispatcher is an abstract class. So you need to impliment a class that extends EventDispatcher. Create a service EventDispatcherService that extends EventDispatcher.

// EventDispatcherService.js
import EventDispatcher from "uieventdispatcher";

export class EventDispatcherService extends EventDispatcher {
  constructor() {
    super({
      eventsToPostInSingleCall: 1
    });
  }

  eventEnricher(event) {
    // Modify event if needed
    event.userId = '1KL';
    return event;
  }

  methodToPostEvents(data) {
    return fetch("http://backend-url.com/api/analytics", {
      body: JSON.stringify(data),
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      method: "POST"
    });
  }
}

// Expose the same instance to your whole application 
const eventDispatcher = new EventDispatcherService();
export default eventDispatcher;

In your application code

import eventDispatcher from 'EventDispatcherService';

eventDispatcher.sendEvent({ page: 1});

eventDispatcher.getEventList(); 
/** output
  
  [ { page: 1,
     eventDispatcherUuid: '1548943992704__DEL__126690',   // a unique id generated for eventDispatcher instance
     timeStamp: 1548943992705,                            // timestamp when sendEvent method was called
     userId: '1KL'                                        // modification we did in eventEnricher
    } 
 ]
 x
**/

eventDispatcher.sendEvent({ page: 2}); 
// This will trigger methodToPostEvents as we defined eventsToPostInSingleCall as 1 
// and this is 2nd event we are submitting

Config

EventDispatcher supports following config keys:

NameTypeDescription
eventsToPostInSingleCallnumberWait for these many events before triggering methodToPostEvents

Abstract methods

The application need to extend EventDispatcher class and impliment these functions.

NameTypeDescription
eventEnricher(event: IEvent) => anyTriggered every time sendEvent is called, can be used to append additonal data to each event.
methodToPostEvents(data: any) => PromiseTriggered when after sendEvent method has been called eventsToPostInSingleCall times atleast.
getLocalStorageKeyName() => stringProvide a unique string which will be used to store event in localStorage. Should not change on page reload. Bad example: timeStamp, good example: appName_userId

Supported methods

Namearguments in orderDescription
sendEventeventToSend: IEvent; forceful = false; consoleEvent = falseeventToSend: event data; forceful: ignore the check for eventsToPostInSingleCall and call methodToPostEvents or apiPathToPostEvents immidiately; consoleEvent: console.log the event being sent to eventToSend method. Called after eventEnricher if provided
getEventListno argumentsReturns the events currently present with eventDispatcher instance
clearEventsno argumentsClear the events currently present with eventDispatcher instance
getUUIDno argumentsEach EventDispatcher instance gets a unique UUID, use this method to get the