1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-14 15:12:48 +01:00
send/app/metrics.js

187 lines
4.3 KiB
JavaScript
Raw Normal View History

import storage from './storage';
2019-04-26 22:30:33 +02:00
import { platform, locale } from './utils';
import { sendMetrics } from './api';
2017-08-05 21:23:58 +02:00
let appState = null;
let experiment = null;
2019-02-12 20:50:06 +01:00
const HOUR = 1000 * 60 * 60;
const events = [];
let session_id = Date.now();
2019-04-26 22:30:33 +02:00
const lang = locale();
2017-08-05 21:23:58 +02:00
export default function initialize(state, emitter) {
appState = state;
emitter.on('DOMContentLoaded', () => {
experiment = storage.enrolled;
if (!appState.user.firstAction) {
appState.user.firstAction =
appState.route === '/' ? 'upload' : 'download';
}
2019-03-05 21:58:40 +01:00
const query = appState.query;
2019-02-12 20:50:06 +01:00
addEvent('client_visit', {
2019-03-05 21:58:40 +01:00
entrypoint: appState.route === '/' ? 'upload' : 'download',
referrer: document.referrer,
utm_campaign: query.utm_campaign,
utm_content: query.utm_content,
utm_medium: query.utm_medium,
utm_source: query.utm_source,
utm_term: query.utm_term
2017-09-12 02:09:29 +02:00
});
});
emitter.on('experiment', experimentEvent);
2019-02-12 20:50:06 +01:00
window.addEventListener('unload', submitEvents);
}
2019-02-12 20:50:06 +01:00
function sizeOrder(n) {
return Math.floor(Math.log10(n));
}
2017-08-05 21:23:58 +02:00
2019-02-12 20:50:06 +01:00
function submitEvents() {
if (navigator.doNotTrack === '1') {
return;
2017-09-12 02:09:29 +02:00
}
sendMetrics(
new Blob(
[
JSON.stringify({
now: Date.now(),
session_id,
lang,
platform: platform(),
events
})
],
{ type: 'text/plain' } // see http://crbug.com/490015
)
);
2019-02-12 20:50:06 +01:00
events.splice(0);
}
async function addEvent(event_type, event_properties) {
const user_id = await appState.user.metricId();
const device_id = await appState.user.deviceId();
const ab_id = Object.keys(experiment)[0];
if (ab_id) {
event_properties.experiment = ab_id;
event_properties.variant = experiment[ab_id];
}
2019-02-12 20:50:06 +01:00
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 21:23:58 +02:00
}
});
2019-02-12 20:50:06 +01:00
if (events.length === 25) {
submitEvents();
}
2017-08-05 21:23:58 +02:00
}
2019-02-12 20:50:06 +01: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 21:23:58 +02:00
});
}
2019-02-12 20:50:06 +01: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
});
}
function stoppedUpload(archive, duration = 0) {
2019-02-12 20:50:06 +01:00
return addEvent('client_upload', {
download_limit: archive.dlimit,
duration: sizeOrder(duration),
2019-02-12 20:50:06 +01:00
file_count: archive.numFiles,
password_protected: !!archive.password,
size: sizeOrder(archive.size),
status: 'error',
time_limit: archive.timeLimit
2017-08-05 21:23:58 +02:00
});
}
function stoppedDownload(params) {
2019-02-12 20:50:06 +01:00
return addEvent('client_download', {
duration: sizeOrder(params.duration),
password_protected: params.password_protected,
size: sizeOrder(params.size),
status: 'error'
2017-11-30 22:41:09 +01:00
});
}
2017-08-05 21:23:58 +02:00
function completedDownload(params) {
2019-02-12 20:50:06 +01:00
return addEvent('client_download', {
duration: sizeOrder(params.duration),
password_protected: params.password_protected,
size: sizeOrder(params.size),
status: 'ok'
2017-08-05 21:23:58 +02:00
});
}
2019-02-12 20:50:06 +01: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 21:23:58 +02:00
});
}
2019-02-12 20:50:06 +01:00
function experimentEvent(params) {
return addEvent('client_experiment', params);
2017-08-05 21:23:58 +02:00
}
2019-02-12 20:50:06 +01:00
function submittedSignup(params) {
return addEvent('client_login', {
status: 'ok',
trigger: params.trigger
2017-08-05 21:23:58 +02:00
});
}
2019-02-12 20:50:06 +01:00
function canceledSignup(params) {
return addEvent('client_login', {
status: 'cancel',
trigger: params.trigger
2017-08-05 21:23:58 +02:00
});
}
2019-02-12 20:50:06 +01:00
function loggedOut(params) {
addEvent('client_logout', {
status: 'ok',
trigger: params.trigger
2017-08-05 21:23:58 +02:00
});
2019-02-12 20:50:06 +01:00
// flush events and start new anon session
submitEvents();
session_id = Date.now();
2017-08-05 21:23:58 +02:00
}
2017-08-10 19:02:13 +02:00
export {
2017-08-05 21:23:58 +02:00
cancelledUpload,
stoppedUpload,
completedUpload,
deletedUpload,
stoppedDownload,
completedDownload,
2019-02-12 20:50:06 +01:00
submittedSignup,
canceledSignup,
loggedOut
2017-08-05 21:23:58 +02:00
};