nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

index.js (1598B)


      1 import * as Sentry from '@sentry/browser';
      2 
      3 function randId() {
      4     return Math.random().toString(36).substring(2, 13);
      5 }
      6 
      7 const sessionId = randId();
      8 
      9 function captureMessage(msg, extra = {}) {
     10     extra.url = window.location.toString();
     11     console.log('captureMessage', msg, extra); // eslint-disable-line no-console
     12     Sentry.withScope(function(scope) {
     13         scope.setExtras(extra);
     14         Sentry.captureMessage(msg);
     15     });
     16 }
     17 
     18 function captureException(e, description) {
     19     console.log('captureException', e, description); // eslint-disable-line no-console
     20     Sentry.withScope(function(scope) {
     21         if (description) {
     22             scope.setTag('description', description);
     23         }
     24         scope.setExtra('url', window.location.toString());
     25         Sentry.captureException(e);
     26     });
     27 }
     28 function captureBreadcrumb(message, data = {}) {
     29     data.url = window.location.toString();
     30     Sentry.addBreadcrumb({
     31         message, data
     32     });
     33 }
     34 
     35 function logEvent(eventName, extra) {
     36     const url = 'https://nakarte.me/event';
     37 
     38     const data = {event: eventName.toString()};
     39     data.data = {
     40         ...extra,
     41         beacon: true,
     42         session: sessionId,
     43         address: window.location.toString()
     44     };
     45     let s = JSON.stringify(data);
     46     try {
     47         navigator.sendBeacon(url, s);
     48     } catch (e) {
     49         data.data.beacon = false;
     50         s = JSON.stringify(data);
     51         const xhr = new XMLHttpRequest();
     52         xhr.open('POST', 'https://nakarte.me/event');
     53         xhr.send(s);
     54     }
     55 }
     56 
     57 export {captureMessage, captureException, captureBreadcrumb, logEvent, randId};