Skip to content

Commit

Permalink
feat(config): anonymous telemetry data used to help deliver new features
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed May 24, 2022
1 parent 582b9bc commit 3e35091
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,16 @@ ui:
lines: 500
```

### `telemetry`

```yaml
# telemetry settings (default: shown below)
# self hosted version of plausible.io
# 100% anonymous, used to help improve project
# no cookies and fully compliant with GDPR, CCPA and PECR
telemetry: true
```

## Storing Secrets

**Note:** If using one of the [Home Assistant Add-ons](https://github.com/jakowenko/double-take-hassio-addons) then the default Home Assistant `/config/secrets.yaml` file is used.
Expand Down
2 changes: 2 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const storage = require('./src/util/storage.util');
const database = require('./src/util/db.util');
const config = require('./src/constants/config');
const shutdown = require('./src/util/shutdown.util');
const heartbeat = require('./src/util/heartbeat.util');
const validate = require('./src/schemas/validate');

module.exports.start = async () => {
Expand All @@ -21,6 +22,7 @@ module.exports.start = async () => {
mqtt.connect();
storage.purge();
socket.connect(server);
heartbeat.cron();
};

try {
Expand Down
1 change: 1 addition & 0 deletions api/src/constants/defaults.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
telemetry: true,
auth: false,
token: {
image: '24h',
Expand Down
13 changes: 13 additions & 0 deletions api/src/controllers/analytics.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const axios = require('axios');
const { TELEMETRY } = require('../constants')();

module.exports.analytics = async (req, res) => {
if (process.env.NODE_ENV !== 'production' || !TELEMETRY) return res.send();
const { data } = await axios({
method: 'get',
url: 'https://analytics.jako.io/js/script.local.js',
}).catch((/* error */) => {
return res.send();
});
res.contentType('application/javascript').send(data);
};
8 changes: 8 additions & 0 deletions api/src/routes/analytics.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const { analytics } = require('../controllers/analytics.controller');

const router = express.Router();

router.get('/', analytics);

module.exports = router;
1 change: 1 addition & 0 deletions api/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ router.use('/proxy', require('./proxy.routes'));
router.use('/logger', require('./logger.routes'));
router.use('/status', require('./status.routes'));
router.use('/export', require('./export.routes'));
router.use('/analytics/analytics.js', require('./analytics.routes'));

router.use(STORAGE.TMP.PATH, express.static(STORAGE.TMP.PATH));
router.all('*', (req, res) => res.status(NOT_FOUND).error(`${req.originalUrl} not found`));
Expand Down
28 changes: 28 additions & 0 deletions api/src/util/heartbeat.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const os = require('os');
const schedule = require('node-schedule');
const axios = require('axios');
const { version } = require('../../package.json');
const { TELEMETRY } = require('../constants')();

module.exports.cron = async () => {
if (process.env.NODE_ENV !== 'production' || !TELEMETRY) return;
await this.track('start');
schedule.scheduleJob('*/15 * * * *', () => this.track('heartbeat'));
};

module.exports.track = async (type) => {
await axios({
method: 'post',
url: 'https://analytics.jako.io/api/event',
data: {
name: 'pageview',
url: `http://localhost/${type}`,
domain: 'double-take-api',
props: JSON.stringify({
version,
arch: os.arch(),
}),
},
validateStatus: () => true,
});
};
17 changes: 17 additions & 0 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default {
this.hidden = !status;
this.loaded = !status;
});
this.addAnalytics();
},
mounted() {
this.toolbarHeight = this.$refs.toolbar.getHeight();
Expand Down Expand Up @@ -161,6 +162,22 @@ export default {
}
return runSetup;
},
async addAnalytics() {
if (process.env.NODE_ENV !== 'production') return;
ApiService.get('config')
.then(({ data }) => {
if (data.telemetry) {
const analytics = document.createElement('script');
analytics.type = 'text/javascript';
analytics.src = `${Constants().api}/analytics/analytics.js`;
analytics.defer = true;
analytics.setAttribute('data-domain', 'double-take-frontend');
analytics.setAttribute('data-api', 'https://analytics.jako.io/api/event');
document.head.appendChild(analytics);
}
})
.catch(() => {});
},
},
};
</script>
Expand Down

0 comments on commit 3e35091

Please sign in to comment.