generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
lana.js
127 lines (105 loc) · 3.29 KB
/
lana.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
(function iife() {
const MSG_LIMIT = 2000;
const defaultOptions = {
clientId: '',
endpoint: 'https://www.adobe.com/lana/ll',
endpointStage: 'https://www.stage.adobe.com/lana/ll',
errorType: 'e',
sampleRate: 1,
tags: '',
implicitSampleRate: 1,
useProd: true,
isProdDomain: false,
};
const w = window;
function isProd() {
const { host } = window.location;
if (host.substring(host.length - 10) === '.adobe.com'
&& host.substring(host.length - 15) !== '.corp.adobe.com'
&& host.substring(host.length - 16) !== '.stage.adobe.com') {
return true;
}
return false;
}
function mergeOptions(op1, op2) {
if (!op1) {
op1 = {};
}
if (!op2) {
op2 = {};
}
function getOpt(key) {
if (op1[key] !== undefined) {
return op1[key];
}
if (op2[key] !== undefined) {
return op2[key];
}
return defaultOptions[key];
}
return Object.keys(defaultOptions).reduce((options, key) => {
options[key] = getOpt(key);
return options;
}, {});
}
function log(msg, options) {
msg = msg && msg.stack ? msg.stack : (msg || '');
if (msg.length > MSG_LIMIT) {
msg = `${msg.slice(0, MSG_LIMIT)}<trunc>`;
}
const o = mergeOptions(options, w.lana.options);
if (!o.clientId) {
console.warn('LANA ClientID is not set in options.');
return;
}
const sampleRateParam = parseInt(new URL(window.location).searchParams.get('lana-sample'), 10);
const sampleRate = sampleRateParam || (o.errorType === 'i' ? o.implicitSampleRate : o.sampleRate);
if (!w.lana.debug && !w.lana.localhost && sampleRate <= Math.random() * 100) return;
const isProdDomain = isProd() || o.isProdDomain;
const endpoint = (!isProdDomain || !o.useProd) ? o.endpointStage : o.endpoint;
const queryParams = [
`m=${encodeURIComponent(msg)}`,
`c=${encodeURI(o.clientId)}`,
`s=${sampleRate}`,
`t=${encodeURI(o.errorType)}`,
];
if (o.tags) {
queryParams.push(`tags=${encodeURI(o.tags)}`);
}
if (!isProdDomain || w.lana.debug || w.lana.localhost) console.log('LANA Msg: ', msg, '\nOpts:', o);
if (!w.lana.localhost || w.lana.debug) {
const xhr = new XMLHttpRequest();
if (w.lana.debug) {
queryParams.push('d');
xhr.addEventListener('load', () => {
console.log('LANA response:', xhr.responseText);
});
}
xhr.open('GET', `${endpoint}?${queryParams.join('&')}`);
xhr.send();
// eslint-disable-next-line consistent-return
return xhr;
}
}
function sendUnhandledError(e) {
log(e.reason || e.error || e.message, { errorType: 'i' });
}
function hasDebugParam() {
return w.location.search.toLowerCase().indexOf('lanadebug') !== -1;
}
function isLocalhost() {
return w.location.host.toLowerCase().indexOf('localhost') !== -1;
}
w.lana = {
debug: false,
log,
options: mergeOptions(w.lana && w.lana.options),
};
/* c8 ignore next */
if (hasDebugParam()) w.lana.debug = true;
if (isLocalhost()) w.lana.localhost = true;
w.addEventListener('error', sendUnhandledError);
w.addEventListener('unhandledrejection', sendUnhandledError);
}());