Skip to content

Commit

Permalink
Update error reporting (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Sep 9, 2023
1 parent fcc2ec8 commit 951e2e5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 145 deletions.
5 changes: 4 additions & 1 deletion source/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,10 @@ base.Telemetry = class {
try {
params = Object.assign({ event_name: name }, this._metadata, /* { debug_mode: true },*/ params);
this._metadata = {};
this._update() && (params.engagement_time_msec = this._engagement_time_msec) && (this._engagement_time_msec = 0);
if (this._update()) {
params.engagement_time_msec = this._engagement_time_msec;
this._engagement_time_msec = 0;
}
const build = (entires) => entires.map((entry) => entry[0] + '=' + encodeURIComponent(entry[1])).join('&');
this._cache = this._cache || build(Array.from(this._config));
const key = (name, value) => this._schema.get(name) || ('number' === typeof value && !isNaN(value) ? 'epn.' : 'ep.') + name;
Expand Down
84 changes: 19 additions & 65 deletions source/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,48 +92,24 @@ host.BrowserHost = class {
const error = event instanceof ErrorEvent && event.error && event.error instanceof Error ? event.error : new Error(event && event.message ? event.message : JSON.stringify(event));
this.exception(error, true);
});
const ga4 = async () => {
const measurement_id = '848W2NVWVH';
const user = this._getCookie('_ga').replace(/^(GA1\.\d\.)*/, '');
const session = this._getCookie('_ga' + measurement_id);
await this._telemetry.start('G-' + measurement_id, user, session);
this._telemetry.set('page_location', this._document.location && this._document.location.href ? this._document.location.href : null);
this._telemetry.set('page_title', this._document.title ? this._document.title : null);
this._telemetry.set('page_referrer', this._document.referrer ? this._document.referrer : null);
this._telemetry.send('page_view', {
app_name: this.type,
app_version: this.version,
});
this._telemetry.send('scroll', {
percent_scrolled: 90,
app_name: this.type,
app_version: this.version
});
this._setCookie('_ga', 'GA1.2.' + this._telemetry.get('client_id'), 1200);
this._setCookie('_ga' + measurement_id, 'GS1.1.' + this._telemetry.session, 1200);
};
const ua = async () => {
return new Promise((resolve) => {
this._telemetry_ua = true;
const script = this.document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'https://www.google-analytics.com/analytics.js');
script.onload = () => {
if (this.window.ga) {
this.window.ga.l = 1 * new Date();
this.window.ga('create', 'UA-54146-13', 'auto');
this.window.ga('set', 'anonymizeIp', true);
}
resolve();
};
script.onerror = () => {
resolve();
};
this.document.body.appendChild(script);
});
};
await ga4();
await ua();
const measurement_id = '848W2NVWVH';
const user = this._getCookie('_ga').replace(/^(GA1\.\d\.)*/, '');
const session = this._getCookie('_ga' + measurement_id);
await this._telemetry.start('G-' + measurement_id, user, session);
this._telemetry.set('page_location', this._document.location && this._document.location.href ? this._document.location.href : null);
this._telemetry.set('page_title', this._document.title ? this._document.title : null);
this._telemetry.set('page_referrer', this._document.referrer ? this._document.referrer : null);
this._telemetry.send('page_view', {
app_name: this.type,
app_version: this.version,
});
this._telemetry.send('scroll', {
percent_scrolled: 90,
app_name: this.type,
app_version: this.version
});
this._setCookie('_ga', 'GA1.2.' + this._telemetry.get('client_id'), 1200);
this._setCookie('_ga' + measurement_id, 'GS1.1.' + this._telemetry.session, 1200);
}
};
const capabilities = async () => {
Expand Down Expand Up @@ -314,10 +290,9 @@ host.BrowserHost = class {
}

exception(error, fatal) {
if ((this._telemetry_ua || this._telemetry) && error) {
if (this._telemetry && error) {
const name = error.name ? error.name + ': ' : '';
const message = error.message ? error.message : JSON.stringify(error);
const description = name + message;
let context = '';
let stack = '';
if (error.stack) {
Expand Down Expand Up @@ -352,14 +327,6 @@ host.BrowserHost = class {
if (error.context) {
context = typeof error.context === 'string' ? error.context : JSON.stringify(error.context);
}
if (this._telemetry_ua && this.window.ga) {
this.window.ga('send', 'exception', {
exDescription: stack ? description + ' @ ' + stack : description,
exFatal: fatal,
appName: this.type,
appVersion: this.version
});
}
this._telemetry.send('exception', {
app_name: this.type,
app_version: this.version,
Expand All @@ -372,19 +339,6 @@ host.BrowserHost = class {
}
}

event_ua(category, action, label, value) {
if (this._telemetry_ua && this.window.ga && category && action && label) {
this.window.ga('send', 'event', {
eventCategory: category,
eventAction: action,
eventLabel: label,
eventValue: value,
appName: this.type,
appVersion: this.version
});
}
}

event(name, params) {
if (name && params) {
params.app_name = this.type;
Expand Down
75 changes: 1 addition & 74 deletions source/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ host.ElectronHost = class {
});
this.set('configuration', 'user', this._telemetry.get('client_id'));
this.set('configuration', 'session', this._telemetry.session);
this._telemetry_ua = new host.Telemetry('UA-54146-13', this._telemetry.get('client_id'), navigator.userAgent, this.type, this.version);
}
};
await age();
Expand Down Expand Up @@ -332,11 +331,10 @@ host.ElectronHost = class {
}

exception(error, fatal) {
if ((this._telemetry_ua || this._telemetry) && error) {
if (this._telemetry && error) {
try {
const name = error.name ? error.name + ': ' : '';
const message = error.message ? error.message : JSON.stringify(error);
const description = name + message;
let context = '';
let stack = '';
if (error.stack) {
Expand All @@ -361,9 +359,6 @@ host.ElectronHost = class {
if (error.context) {
context = typeof error.context === 'string' ? error.context : JSON.stringify(error.context);
}
if (this._telemetry_ua) {
this._telemetry_ua.exception(stack ? description + ' @ ' + stack : description, fatal);
}
this._telemetry.send('exception', {
app_name: this.type,
app_version: this.version,
Expand All @@ -379,16 +374,6 @@ host.ElectronHost = class {
}
}

event_ua(category, action, label, value) {
if (this._telemetry_ua && category && action && label) {
try {
this._telemetry_ua.event(category, action, label, value);
} catch (e) {
// continue regardless of error
}
}
}

event(name, params) {
if (name && params) {
params.app_name = this.type;
Expand Down Expand Up @@ -599,64 +584,6 @@ host.ElectronHost = class {
}
};

host.Telemetry = class {

constructor(trackingId, clientId, userAgent, applicationName, applicationVersion) {
this._params = {
aip: '1', // anonymizeIp
tid: trackingId,
cid: clientId,
ua: userAgent,
an: applicationName,
av: applicationVersion
};
}

event(category, action, label, value) {
const params = Object.assign({}, this._params);
params.ec = category;
params.ea = action;
params.el = label;
params.ev = value;
this._send('event', params);
}

exception(description, fatal) {
const params = Object.assign({}, this._params);
params.exd = description;
if (fatal) {
params.exf = '1';
}
this._send('exception', params);
}

_send(type, params) {
params.t = type;
params.v = '1';
for (const param in params) {
if (params[param] === null || params[param] === undefined) {
delete params[param];
}
}
const body = Object.entries(params).map((entry) => encodeURIComponent(entry[0]) + '=' + encodeURIComponent(entry[1])).join('&');
const options = {
method: 'POST',
host: 'www.google-analytics.com',
path: '/collect',
headers: { 'Content-Length': Buffer.byteLength(body) }
};
const request = https.request(options, (response) => {
response.on('error', (/* error */) => {});
});
request.setTimeout(5000, () => {
request.destroy();
});
request.on('error', (/* error */) => {});
request.write(body);
request.end();
}
};

host.ElectronHost.FileStream = class {

constructor(file, start, length, mtime) {
Expand Down
1 change: 0 additions & 1 deletion source/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' www.google-analytics.com;">
<meta name="version" content="0.0.0">
<meta name="date" content="">
<title>Netron</title>
Expand Down
1 change: 0 additions & 1 deletion source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ view.View = class {
format.push('(' + model.producer + ')');
}
if (format.length > 0) {
this._host.event_ua('Model', 'Format', format.join(' '));
this._host.event('model_open', {
model_format: model.format || '',
model_producer: model.producer || ''
Expand Down
3 changes: 0 additions & 3 deletions test/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ host.TestHost = class {
return new base.BinaryStream(buffer);
}

event_ua(/* category, action, label, value */) {
}

event(/* name, params */) {
}

Expand Down

0 comments on commit 951e2e5

Please sign in to comment.