-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
const ua = require('universal-analytics'); | ||
const isDocker = require('is-docker'); | ||
const Configstore = require('configstore'); | ||
const uuidv4 = require('uuid/v4'); | ||
const pkg = require('../package.json'); | ||
|
||
const trackingID = 'UA-129582046-1'; | ||
|
||
// Usage stats | ||
function Stats(enabled) { | ||
this.timer = {}; | ||
|
||
if (enabled === true) { | ||
const config = new Configstore(pkg.name); | ||
let clientID = uuidv4(); | ||
if (config.has('clientID')) { | ||
clientID = config.get('clientID'); | ||
} else { | ||
config.set('clientID', clientID); | ||
} | ||
|
||
const tracker = ua(trackingID, clientID); | ||
tracker.set('aip', 1); // Anonymize IP | ||
tracker.set('an', 'frontail'); // Application Name | ||
tracker.set('av', pkg.version); // Application Version | ||
tracker.set('ds', 'app'); // Data Source | ||
tracker.set('cd1', process.platform); // platform | ||
tracker.set('cd2', process.arch); // arch | ||
tracker.set('cd3', process.version.match(/^v(\d+\.\d+)/)[1]); // Node version | ||
tracker.set('cd4', isDocker()); // is Docker | ||
this.tracker = tracker; | ||
} | ||
|
||
return this; | ||
} | ||
|
||
Stats.prototype.track = function track(category, action) { | ||
if (!this.tracker) { | ||
return; | ||
} | ||
this.tracker.event(category, action).send(); | ||
}; | ||
|
||
Stats.prototype.time = function time(category, action) { | ||
if (!this.tracker) { | ||
return; | ||
} | ||
|
||
if (!this.timer[category]) { | ||
this.timer[category] = {}; | ||
} | ||
this.timer[category][action] = Date.now(); | ||
}; | ||
|
||
Stats.prototype.timeEnd = function timeEnd(category, action) { | ||
if (!this.tracker) { | ||
return; | ||
} | ||
|
||
this.tracker.timing(category, action, Date.now() - this.timer[category][action]).send(); | ||
}; | ||
|
||
module.exports = enabled => new Stats(enabled); |
Oops, something went wrong.