fox-send/app/metrics.js

177 lines
4.0 KiB
JavaScript
Raw Normal View History

import storage from './storage';
2019-02-12 19:50:06 +00:00
import { platform } from './utils';
2017-08-05 19:23:58 +00:00
let appState = null;
2019-02-12 19:50:06 +00:00
// let experiment = null;
const HOUR = 1000 * 60 * 60;
const events = [];
let session_id = Date.now();
const lang = document.querySelector('html').lang;
2017-08-05 19:23:58 +00:00
export default function initialize(state, emitter) {
appState = state;
2019-02-12 19:50:06 +00:00
if (!appState.user.firstAction) {
appState.user.firstAction = appState.route === '/' ? 'upload' : 'download';
}
emitter.on('DOMContentLoaded', () => {
2019-02-12 19:50:06 +00:00
// experiment = storage.enrolled[0];
addEvent('client_visit', {
entrypoint: appState.route === '/' ? 'upload' : 'download'
2017-09-12 00:09:29 +00:00
});
});
emitter.on('experiment', experimentEvent);
2019-02-12 19:50:06 +00:00
window.addEventListener('unload', submitEvents);
}
2019-02-12 19:50:06 +00:00
function sizeOrder(n) {
return Math.floor(Math.log10(n));
}
2017-08-05 19:23:58 +00:00
2019-02-12 19:50:06 +00:00
function submitEvents() {
if (navigator.doNotTrack === '1') {
return;
2017-09-12 00:09:29 +00:00
}
2019-02-12 19:50:06 +00:00
const data = new Blob(
[
JSON.stringify({
now: Date.now(),
session_id,
lang,
platform: platform(),
events
})
],
2019-02-15 19:59:39 +00:00
{ type: 'text/plain' } // see http://crbug.com/490015
);
2019-02-12 19:50:06 +00:00
events.splice(0);
if (!navigator.sendBeacon) {
return;
2017-08-05 19:23:58 +00:00
}
2019-02-15 19:59:39 +00:00
try {
navigator.sendBeacon('/api/metrics', data);
} catch (e) {
console.error(e);
}
2019-02-12 19:50:06 +00:00
}
async function addEvent(event_type, event_properties) {
const user_id = await appState.user.metricId();
const device_id = await appState.user.deviceId();
events.push({
device_id,
event_properties,
event_type,
time: Date.now(),
user_id,
user_properties: {
anonymous: !appState.user.loggedIn,
first_action: appState.user.firstAction,
active_count: storage.files.length
2017-08-05 19:23:58 +00:00
}
});
2019-02-12 19:50:06 +00:00
if (events.length === 25) {
submitEvents();
}
2017-08-05 19:23:58 +00:00
}
2019-02-12 19:50:06 +00:00
function cancelledUpload(archive, duration) {
return addEvent('client_upload', {
download_limit: archive.dlimit,
duration: sizeOrder(duration),
file_count: archive.numFiles,
password_protected: !!archive.password,
size: sizeOrder(archive.size),
status: 'cancel',
time_limit: archive.timeLimit
2017-08-05 19:23:58 +00:00
});
}
2019-02-12 19:50:06 +00:00
function completedUpload(archive, duration) {
return addEvent('client_upload', {
download_limit: archive.dlimit,
duration: sizeOrder(duration),
file_count: archive.numFiles,
password_protected: !!archive.password,
size: sizeOrder(archive.size),
status: 'ok',
time_limit: archive.timeLimit
});
}
2019-02-12 19:50:06 +00:00
function stoppedUpload(archive) {
return addEvent('client_upload', {
download_limit: archive.dlimit,
file_count: archive.numFiles,
password_protected: !!archive.password,
size: sizeOrder(archive.size),
status: 'error',
time_limit: archive.timeLimit
2017-08-05 19:23:58 +00:00
});
}
function stoppedDownload(params) {
2019-02-12 19:50:06 +00:00
return addEvent('client_download', {
duration: sizeOrder(params.duration),
password_protected: params.password_protected,
size: sizeOrder(params.size),
status: 'error'
2017-11-30 21:41:09 +00:00
});
}
2017-08-05 19:23:58 +00:00
function completedDownload(params) {
2019-02-12 19:50:06 +00:00
return addEvent('client_download', {
duration: sizeOrder(params.duration),
password_protected: params.password_protected,
size: sizeOrder(params.size),
status: 'ok'
2017-08-05 19:23:58 +00:00
});
}
2019-02-12 19:50:06 +00:00
function deletedUpload(ownedFile) {
return addEvent('client_delete', {
age: Math.floor((Date.now() - ownedFile.createdAt) / HOUR),
downloaded: ownedFile.dtotal > 0,
status: 'ok'
2017-08-05 19:23:58 +00:00
});
}
2019-02-12 19:50:06 +00:00
function experimentEvent(params) {
return addEvent('client_experiment', params);
2017-08-05 19:23:58 +00:00
}
2019-02-12 19:50:06 +00:00
function submittedSignup(params) {
return addEvent('client_login', {
status: 'ok',
trigger: params.trigger
2017-08-05 19:23:58 +00:00
});
}
2019-02-12 19:50:06 +00:00
function canceledSignup(params) {
return addEvent('client_login', {
status: 'cancel',
trigger: params.trigger
2017-08-05 19:23:58 +00:00
});
}
2019-02-12 19:50:06 +00:00
function loggedOut(params) {
addEvent('client_logout', {
status: 'ok',
trigger: params.trigger
2017-08-05 19:23:58 +00:00
});
2019-02-12 19:50:06 +00:00
// flush events and start new anon session
submitEvents();
session_id = Date.now();
2017-08-05 19:23:58 +00:00
}
2017-08-10 17:02:13 +00:00
export {
2017-08-05 19:23:58 +00:00
cancelledUpload,
stoppedUpload,
completedUpload,
deletedUpload,
stoppedDownload,
completedDownload,
2019-02-12 19:50:06 +00:00
submittedSignup,
canceledSignup,
loggedOut
2017-08-05 19:23:58 +00:00
};