1 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2 |
|
3 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
4 |
|
5 | import * as Sentry from '@sentry/browser';
|
6 | import hasNewRelic from './utils/hasNewRelic';
|
7 | import hasGoogleAnalytics from './utils/hasGoogleAnalytics';
|
8 | export { Sentry };
|
9 | export * from './types';
|
10 |
|
11 | class Metrics {
|
12 | constructor() {
|
13 | _defineProperty(this, "settings", {
|
14 | context: {},
|
15 | ignoreErrors: [],
|
16 | sentry: {},
|
17 | sentryKey: '',
|
18 | sentryProject: '',
|
19 | userID: null
|
20 | });
|
21 | }
|
22 |
|
23 | initialize(settings) {
|
24 | this.settings = _extends({}, this.settings, {}, settings);
|
25 | this.bootstrapNewRelic();
|
26 | this.bootstrapSentry();
|
27 | this.bootstrapGoogleAnalytics();
|
28 | }
|
29 |
|
30 | bootstrapNewRelic() {
|
31 | const {
|
32 | context,
|
33 | ignoreErrors,
|
34 | userID
|
35 | } = this.settings;
|
36 |
|
37 | if (!this.isNewRelicEnabled()) {
|
38 | return;
|
39 | }
|
40 |
|
41 | if (typeof global.navigator !== 'undefined') {
|
42 | newrelic.setCustomAttribute('browser-locale', global.navigator.language);
|
43 | newrelic.setCustomAttribute('user-agent', global.navigator.userAgent);
|
44 | }
|
45 |
|
46 | newrelic.setCustomAttribute('user-id', userID ? String(userID) : 'N/A');
|
47 | Object.keys(context).forEach(key => {
|
48 | newrelic.setCustomAttribute(key.replace(/[A-Z]/g, match => "-" + match.toLowerCase()), String(context[key]));
|
49 | });
|
50 | newrelic.setErrorHandler(
|
51 |
|
52 | error => {
|
53 | const errorString = String(error);
|
54 | return ignoreErrors.some(filter => typeof filter === 'string' ? errorString.includes(filter) : filter.test(errorString));
|
55 | });
|
56 | }
|
57 |
|
58 | bootstrapSentry() {
|
59 | const {
|
60 | context,
|
61 | ignoreErrors,
|
62 | sentry,
|
63 | sentryKey,
|
64 | sentryProject,
|
65 | userID
|
66 | } = this.settings;
|
67 |
|
68 | if (!this.isSentryEnabled() || typeof global.location === 'undefined') {
|
69 | return;
|
70 | }
|
71 |
|
72 | const {
|
73 | host,
|
74 | protocol
|
75 | } = global.location;
|
76 | Sentry.init(_extends({
|
77 | dsn: protocol + "//" + sentryKey + "@" + host + "/" + sentryProject,
|
78 | enabled: true,
|
79 | environment: process.env.NODE_ENV,
|
80 | ignoreErrors,
|
81 | release: process.env.SENTRY_RELEASE
|
82 | }, sentry));
|
83 | Sentry.configureScope(scope => {
|
84 | scope.setUser({
|
85 | id: userID ? String(userID) : 'N/A'
|
86 | });
|
87 | scope.setExtras(context);
|
88 |
|
89 | if (typeof global.navigator !== 'undefined') {
|
90 | scope.setTag('browser.locale', global.navigator.language);
|
91 | }
|
92 | });
|
93 | }
|
94 |
|
95 | bootstrapGoogleAnalytics() {
|
96 | if (!this.isGoogleAnalyticsEnabled()) {
|
97 | return;
|
98 | }
|
99 |
|
100 | if (this.settings.userID) {
|
101 | ga('set', 'userId', "" + this.settings.userID);
|
102 | }
|
103 | }
|
104 |
|
105 | isGoogleAnalyticsEnabled() {
|
106 | return hasGoogleAnalytics();
|
107 | }
|
108 |
|
109 | isNewRelicEnabled() {
|
110 | return hasNewRelic();
|
111 | }
|
112 |
|
113 | isSentryEnabled() {
|
114 | const {
|
115 | sentry,
|
116 | sentryKey,
|
117 | sentryProject
|
118 | } = this.settings;
|
119 | return (sentry == null ? void 0 : sentry.dsn) || sentryKey && sentryProject;
|
120 | }
|
121 |
|
122 | }
|
123 |
|
124 | export default new Metrics(); |
\ | No newline at end of file |