UNPKG

@atlaskit/analytics-listeners

Version:

Fabric analytics listeners to be used by the products

44 lines 1.46 kB
const isPromise = c => { return typeof c.then === 'function'; }; export const sendEvent = (logger, client) => event => { if (client) { const { eventType, ...gasEvent } = event; const withClient = cb => { if (isPromise(client)) { client.then(cb).catch(e => logger.warn('There was an error sending the event', e)); } else { try { cb(client); } catch (e) { logger.warn('There was an error sending the event', e); } } }; switch (event.eventType) { case 'ui': logger.debug('Sending UI Event via analytics client', gasEvent); withClient(client => client.sendUIEvent(gasEvent)); break; case 'operational': logger.debug('Sending Operational Event via analytics client', gasEvent); withClient(client => client.sendOperationalEvent(gasEvent)); break; case 'track': logger.debug('Sending Track Event via analytics client', gasEvent); withClient(client => client.sendTrackEvent(gasEvent)); break; case 'screen': logger.debug('Sending Screen Event via analytics client', gasEvent); withClient(client => client.sendScreenEvent(gasEvent)); break; default: logger.error(`cannot map eventType ${event.eventType} to an analytics-web-client function`); } } else { logger.warn('AnalyticsWebClient instance is not provided'); } };