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

247 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-08-10 19:02:13 +02:00
import testPilotGA from 'testpilot-ga/src/TestPilotGA';
import storage from './storage';
let hasLocalStorage = false;
try {
hasLocalStorage = typeof localStorage !== 'undefined';
} catch (e) {
// when disabled, any mention of localStorage throws an error
}
2017-08-05 21:23:58 +02:00
const analytics = new testPilotGA({
an: 'Firefox Send',
ds: 'web',
tid: window.GOOGLE_ANALYTICS_ID
});
let appState = null;
2017-08-05 21:23:58 +02:00
export default function initialize(state, emitter) {
appState = state;
emitter.on('DOMContentLoaded', () => {
addExitHandlers();
//TODO restart handlers... somewhere
});
}
function category() {
return appState.route === '/' ? 'sender' : 'recipient';
}
2017-08-05 21:23:58 +02:00
function sendEvent() {
return (
hasLocalStorage &&
analytics.sendEvent.apply(analytics, arguments).catch(() => 0)
);
2017-08-05 21:23:58 +02:00
}
function urlToMetric(url) {
switch (url) {
case 'https://www.mozilla.org/':
return 'mozilla';
case 'https://www.mozilla.org/about/legal':
return 'legal';
case 'https://testpilot.firefox.com/about':
return 'about';
case 'https://testpilot.firefox.com/privacy':
return 'privacy';
case 'https://testpilot.firefox.com/terms':
return 'terms';
case 'https://www.mozilla.org/privacy/websites/#cookies':
return 'cookies';
case 'https://github.com/mozilla/send':
return 'github';
case 'https://twitter.com/FxTestPilot':
return 'twitter';
case 'https://www.mozilla.org/firefox/new/?scene=2':
return 'download-firefox';
2017-08-10 01:44:09 +02:00
case 'https://qsurvey.mozilla.com/s3/txp-firefox-send':
return 'survey';
case 'https://testpilot.firefox.com/':
case 'https://testpilot.firefox.com/experiments/send':
return 'testpilot';
2017-08-05 21:23:58 +02:00
default:
return 'other';
}
}
function setReferrer(state) {
if (category() === 'sender') {
2017-08-05 21:23:58 +02:00
if (state) {
storage.referrer = `${state}-upload`;
}
} else if (category() === 'recipient') {
2017-08-05 21:23:58 +02:00
if (state) {
storage.referrer = `${state}-download`;
}
}
}
function externalReferrer() {
if (/^https:\/\/testpilot\.firefox\.com/.test(document.referrer)) {
return 'testpilot';
}
return 'external';
}
function takeReferrer() {
const referrer = storage.referrer || externalReferrer();
storage.referrer = null;
return referrer;
}
function startedUpload(params) {
return sendEvent('sender', 'upload-started', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm5: storage.totalUploads,
cm6: storage.files.length + 1,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd1: params.type,
cd5: takeReferrer()
});
}
function cancelledUpload(params) {
setReferrer('cancelled');
return sendEvent('sender', 'upload-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd1: params.type,
cd2: 'cancelled'
});
}
function completedUpload(params) {
return sendEvent('sender', 'upload-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm2: params.time,
cm3: params.speed,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd1: params.type,
cd2: 'completed'
});
}
function startedDownload(params) {
return sendEvent('recipient', 'download-started', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm4: params.ttl,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads
});
}
function stoppedDownload(params) {
return sendEvent('recipient', 'download-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd2: 'errored',
cd6: params.err
});
}
function cancelledDownload(params) {
setReferrer('cancelled');
return sendEvent('recipient', 'download-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd2: 'cancelled'
});
}
function stoppedUpload(params) {
return sendEvent('sender', 'upload-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd1: params.type,
cd2: 'errored',
cd6: params.err
});
}
function completedDownload(params) {
return sendEvent('recipient', 'download-stopped', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm2: params.time,
cm3: params.speed,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd2: 'completed'
});
}
function deletedUpload(params) {
return sendEvent(category(), 'upload-deleted', {
2017-08-05 21:23:58 +02:00
cm1: params.size,
cm2: params.time,
cm3: params.speed,
cm4: params.ttl,
cm5: storage.totalUploads,
cm6: storage.files.length,
2017-08-05 21:23:58 +02:00
cm7: storage.totalDownloads,
cd1: params.type,
cd4: params.location
});
}
function unsupported(params) {
return sendEvent(category(), 'unsupported', {
2017-08-05 21:23:58 +02:00
cd6: params.err
});
}
function copiedLink(params) {
return sendEvent('sender', 'copied', {
2017-08-05 21:23:58 +02:00
cd4: params.location
});
}
function exitEvent(target) {
return sendEvent(category(), 'exited', {
2017-08-05 21:23:58 +02:00
cd3: urlToMetric(target.currentTarget.href)
});
}
function addExitHandlers() {
2017-08-07 04:32:07 +02:00
const links = Array.from(document.querySelectorAll('a'));
2017-08-05 21:23:58 +02:00
links.forEach(l => {
2017-08-10 01:44:09 +02:00
if (/^http/.test(l.getAttribute('href'))) {
2017-08-05 21:23:58 +02:00
l.addEventListener('click', exitEvent);
}
});
}
function restart(state) {
2017-08-05 21:23:58 +02:00
setReferrer(state);
return sendEvent(category(), 'restarted', {
2017-08-05 21:23:58 +02:00
cd2: state
});
}
2017-08-10 19:02:13 +02:00
export {
2017-08-05 21:23:58 +02:00
copiedLink,
startedUpload,
cancelledUpload,
stoppedUpload,
completedUpload,
deletedUpload,
startedDownload,
cancelledDownload,
stoppedDownload,
completedDownload,
restart,
2017-08-05 21:23:58 +02:00
unsupported
};