An analytics server that doesn't undermine user's privacy
An analytics server that doesn't undermine user's privacy
An analytics server that doesn't undermine user's privacy
Google Analytics is the de-facto standard in the web and mobile analytics service world.
But it has several serious privacy implications:
Inspired by an interesting article from @staltz, and from the awesome work done by the micro-analytics team, I decided to start working on a Google Analytics alternative.
Fair Analytics is an open, transparent, distributed and fair Google Analytics alternative.
There are 2 ways of running Fair Analytics
npm install -g fair-analytics
fair-analytics
The command accepts some options:
…The instance is now running at http://localhost:3000
Add fair-analytics as a dependency to your project
const path = require('path')
const fa = require('fair-analytics')
const server = fa({
storageDirectory: path.resolve(__dirname)
})
const { feed } = server
feed.on('ready', () => {
server.listen(3000, '0.0.0.0')
})The instance is now running at http://localhost:3000
TODO
The quickest way to start tracking usage is to use fair-analytics-client-api
Example usage:
import fairAnalytics from 'fair-analytics-client-api'
// create a fa instance
const fa = fairAnalytics({
url: 'https://fa.yoursite.com' // the URL of your hosted Fair Analytics instance
})
// track events
fa.send({
event: 'pageView', // event is mandatory and can be anything
pathname: window.location.pathname
})
.then(res => {
if (res.ok) {
console.log('success')
}
})
.catch(err => {
console.error(err.message)
})Please refer to the fair-analytics-client-api documentation for further details
Fair Analytics responds to 3 endpoints:
Responds with a basic homepage, displaying the feed.key
Used to POST tracked events.
Responds with 204 in case of success (the body MUST be an object containing at least an event parameter)
Gets realtime updates via server sent events Useful to create real-time dashboards
Consuming real-time data is as easy as:
if (window.EventSource) {
const source = new window.EventSource('https://fa.mysite.com/_live')
source.addEventListener('fair-analytics-event', (e) => {
console.log(e)
})
source.addEventListener('open', () => {
console.log('Connection was opened')
})
source.addEventListener('error', e => {
if (e.readyState === window.EventSource.CLOSED) {
console.log('Connection was closed')
}
})
}Provides an aggregated view of all the events stored, grouped by event and pathname
In this case data is persisted to a local JSON file using lowdb
Here is an example response:
{
"pageView":{
"/home":{
"times":640,
"last":"2017-05-04T12:36:31.514Z"
},
"/about":{
"times":40,
"last":"2017-05-04T12:36:31.514Z"
}
}
}As we said Fair Analytics is distributed. It's easily possible to replicate raw data.
…$ npm testThis project adheres to Semantic Versioning.
Every release, along with the migration instructions, is documented in the CHANGELOG.md file.
MIT
No open issues yet, or sync has not completed.