2020-03-08 22:22:47 +01:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* Strapi telemetry package.
|
|
|
|
* You can learn more at https://strapi.io/documentation/3.0.0-beta.x/global-strapi/usage-information.html#commitment-to-our-users-data-collection
|
|
|
|
*/
|
|
|
|
const os = require('os');
|
2020-03-27 10:30:43 +01:00
|
|
|
|
2020-03-25 20:02:29 +01:00
|
|
|
const isDocker = require('is-docker');
|
|
|
|
const { machineIdSync } = require('node-machine-id');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const ciEnv = require('ci-info');
|
2020-03-27 10:30:43 +01:00
|
|
|
const { scheduleJob } = require('node-schedule');
|
|
|
|
|
2020-03-27 12:14:12 +01:00
|
|
|
const createMiddleware = require('./middleware');
|
|
|
|
const isTruthyEnvVar = require('./truthy-var');
|
2020-03-08 22:22:47 +01:00
|
|
|
|
2020-03-25 20:02:29 +01:00
|
|
|
const createTelemetryInstance = strapi => {
|
|
|
|
const uuid = strapi.config.uuid;
|
|
|
|
const deviceId = machineIdSync();
|
2020-03-08 22:22:47 +01:00
|
|
|
|
2020-03-27 10:30:43 +01:00
|
|
|
const isDisabled = !uuid || isTruthyEnvVar(process.env.STRAPI_TELEMETRY_DISABLED);
|
2020-03-25 20:02:29 +01:00
|
|
|
|
|
|
|
const anonymous_metadata = {
|
|
|
|
environment: strapi.config.environment,
|
|
|
|
os: os.type(),
|
|
|
|
osPlatform: os.platform(),
|
|
|
|
osRelease: os.release(),
|
|
|
|
nodeVersion: process.version,
|
|
|
|
docker: process.env.DOCKER || isDocker(),
|
|
|
|
isCI: ciEnv.isCI,
|
|
|
|
version: strapi.config.info.strapi,
|
|
|
|
strapiVersion: strapi.config.info.strapi,
|
|
|
|
};
|
|
|
|
|
|
|
|
const sendEvent = async (event, payload) => {
|
|
|
|
// do not send anything when user has disabled analytics
|
|
|
|
if (isDisabled) return true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch('https://analytics.strapi.io/track', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
event,
|
|
|
|
uuid,
|
|
|
|
deviceId,
|
|
|
|
properties: {
|
|
|
|
...payload,
|
|
|
|
...anonymous_metadata,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
timeout: 1000,
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2020-03-08 22:22:47 +01:00
|
|
|
});
|
2020-03-25 20:02:29 +01:00
|
|
|
|
|
|
|
return res.ok;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-27 12:14:12 +01:00
|
|
|
const initPing = () => {
|
|
|
|
if (isDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
|
2020-03-25 20:02:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2020-03-27 12:14:12 +01:00
|
|
|
initPing,
|
|
|
|
send: sendEvent,
|
|
|
|
middleware: createMiddleware({ sendEvent, isDisabled }),
|
2020-03-08 22:22:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-03-27 10:30:43 +01:00
|
|
|
module.exports = createTelemetryInstance;
|