2017-08-10 19:02:13 +02:00
|
|
|
import testPilotGA from 'testpilot-ga/src/TestPilotGA';
|
2017-08-24 23:54:02 +02:00
|
|
|
import storage from './storage';
|
2017-08-10 03:16:10 +02:00
|
|
|
|
|
|
|
let hasLocalStorage = false;
|
|
|
|
try {
|
2017-08-24 23:54:02 +02:00
|
|
|
hasLocalStorage = typeof localStorage !== 'undefined';
|
2017-08-10 03:16:10 +02:00
|
|
|
} catch (e) {
|
2017-08-24 23:54:02 +02:00
|
|
|
// when disabled, any mention of localStorage throws an error
|
2017-08-10 03:16:10 +02:00
|
|
|
}
|
2017-08-05 21:23:58 +02:00
|
|
|
|
|
|
|
const analytics = new testPilotGA({
|
|
|
|
an: 'Firefox Send',
|
|
|
|
ds: 'web',
|
|
|
|
tid: window.GOOGLE_ANALYTICS_ID
|
|
|
|
});
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
let appState = null;
|
2017-09-12 02:09:29 +02:00
|
|
|
let experiment = null;
|
2017-08-05 21:23:58 +02:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
export default function initialize(state, emitter) {
|
|
|
|
appState = state;
|
|
|
|
emitter.on('DOMContentLoaded', () => {
|
2018-01-20 00:07:05 +01:00
|
|
|
addExitHandlers();
|
2017-09-12 02:09:29 +02:00
|
|
|
experiment = storage.enrolled[0];
|
|
|
|
sendEvent(category(), 'visit', {
|
|
|
|
cm5: storage.totalUploads,
|
|
|
|
cm6: storage.files.length,
|
|
|
|
cm7: storage.totalDownloads
|
|
|
|
});
|
2017-08-24 23:54:02 +02:00
|
|
|
//TODO restart handlers... somewhere
|
|
|
|
});
|
2018-01-20 00:07:05 +01:00
|
|
|
emitter.on('exit', exitEvent);
|
|
|
|
emitter.on('experiment', experimentEvent);
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function category() {
|
2017-09-12 02:09:29 +02:00
|
|
|
switch (appState.route) {
|
|
|
|
case '/':
|
|
|
|
case '/share/:id':
|
|
|
|
return 'sender';
|
|
|
|
case '/download/:id/:key':
|
|
|
|
case '/download/:id':
|
|
|
|
case '/completed':
|
|
|
|
return 'recipient';
|
|
|
|
default:
|
|
|
|
return 'other';
|
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
2017-08-05 21:23:58 +02:00
|
|
|
|
|
|
|
function sendEvent() {
|
2017-09-12 02:09:29 +02:00
|
|
|
const args = Array.from(arguments);
|
|
|
|
if (experiment && args[2]) {
|
|
|
|
args[2].xid = experiment[0];
|
|
|
|
args[2].xvar = experiment[1];
|
|
|
|
}
|
2017-08-10 03:16:10 +02:00
|
|
|
return (
|
2017-09-12 02:09:29 +02:00
|
|
|
hasLocalStorage && analytics.sendEvent.apply(analytics, args).catch(() => 0)
|
2017-08-10 03:16:10 +02:00
|
|
|
);
|
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-11-08 00:54:42 +01:00
|
|
|
case 'https://www.mozilla.org/firefox/new/?utm_campaign=send-acquisition&utm_medium=referral&utm_source=send.firefox.com':
|
|
|
|
return 'promo';
|
2017-08-05 21:23:58 +02:00
|
|
|
default:
|
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setReferrer(state) {
|
2017-08-24 23:54:02 +02:00
|
|
|
if (category() === 'sender') {
|
2017-08-05 21:23:58 +02:00
|
|
|
if (state) {
|
|
|
|
storage.referrer = `${state}-upload`;
|
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
} 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) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('sender', 'upload-started', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
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');
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('sender', 'upload-stopped', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd1: params.type,
|
|
|
|
cd2: 'cancelled'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function completedUpload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
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,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd1: params.type,
|
|
|
|
cd2: 'completed'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:43:36 +02:00
|
|
|
function addedPassword(params) {
|
|
|
|
return sendEvent('sender', 'password-added', {
|
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
|
|
|
cm6: storage.files.length,
|
|
|
|
cm7: storage.totalDownloads
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:23:58 +02:00
|
|
|
function startedDownload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('recipient', 'download-started', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm4: params.ttl,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stoppedDownload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('recipient', 'download-stopped', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
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');
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('recipient', 'download-stopped', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd2: 'cancelled'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stoppedUpload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('sender', 'upload-stopped', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd1: params.type,
|
|
|
|
cd2: 'errored',
|
|
|
|
cd6: params.err
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-30 22:41:09 +01:00
|
|
|
function changedDownloadLimit(params) {
|
|
|
|
return sendEvent('sender', 'download-limit-changed', {
|
|
|
|
cm1: params.size,
|
|
|
|
cm5: storage.totalUploads,
|
|
|
|
cm6: storage.files.length,
|
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cm8: params.dlimit
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:23:58 +02:00
|
|
|
function completedDownload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
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,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd2: 'completed'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deletedUpload(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
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,
|
2017-08-24 23:54:02 +02:00
|
|
|
cm6: storage.files.length,
|
2017-08-05 21:23:58 +02:00
|
|
|
cm7: storage.totalDownloads,
|
|
|
|
cd1: params.type,
|
|
|
|
cd4: params.location
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsupported(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent(category(), 'unsupported', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cd6: params.err
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copiedLink(params) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent('sender', 'copied', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cd4: params.location
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function exitEvent(target) {
|
2017-08-24 23:54:02 +02:00
|
|
|
return sendEvent(category(), 'exited', {
|
2017-08-05 21:23:58 +02:00
|
|
|
cd3: urlToMetric(target.currentTarget.href)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-20 00:07:05 +01:00
|
|
|
function experimentEvent(params) {
|
|
|
|
return sendEvent(category(), 'experiment', params);
|
|
|
|
}
|
|
|
|
|
2017-11-08 00:54:42 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2017-08-05 21:23:58 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
function restart(state) {
|
2017-08-05 21:23:58 +02:00
|
|
|
setReferrer(state);
|
2017-08-24 23:54:02 +02:00
|
|
|
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,
|
2017-11-30 22:41:09 +01:00
|
|
|
changedDownloadLimit,
|
2017-08-05 21:23:58 +02:00
|
|
|
deletedUpload,
|
|
|
|
startedDownload,
|
|
|
|
cancelledDownload,
|
|
|
|
stoppedDownload,
|
|
|
|
completedDownload,
|
2017-08-31 18:43:36 +02:00
|
|
|
addedPassword,
|
2017-08-24 23:54:02 +02:00
|
|
|
restart,
|
2017-08-05 21:23:58 +02:00
|
|
|
unsupported
|
|
|
|
};
|