Skip to content

Commit

Permalink
feat(*): Collect anonymus usage statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Nov 26, 2016
1 parent 13e933b commit dc2446c
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 17 deletions.
29 changes: 18 additions & 11 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const meow = require('meow');
const chalk = require('chalk');
const insight = require('./lib/analytics');
const aikDevServer = require('./lib/dev-server-command').default;
const aikBuild = require('./lib/build-command').default;
const pkg = require('./package.json');
Expand Down Expand Up @@ -52,14 +53,20 @@ const cli = meow({
const input = cli.input || [];
const flags = cli.flags || {};

if (!input.length || flags.help) {
console.log(cli.help); // eslint-disable-line
} else if (flags.version) {
console.log(pkg.version); // eslint-disable-line
} else if (flags.build) {
aikBuild(input, flags)
.catch(err => err && console.error(chalk.red(err))); // eslint-disable-line
} else {
aikDevServer(input, flags)
.catch(err => err && console.error(chalk.red(err))); // eslint-disable-line
}
insight.askPermission(() => {
if (!input.length || flags.help) {
insight.track([], input, flags);
console.log(cli.help); // eslint-disable-line
} else if (flags.version) {
insight.track([], input, flags);
console.log(pkg.version); // eslint-disable-line
} else if (flags.build) {
insight.track(['build'], input, flags);
aikBuild(input, flags)
.catch(err => err && console.error(chalk.red(err))); // eslint-disable-line
} else {
insight.track(['dev-server'], input, flags);
aikDevServer(input, flags)
.catch(err => err && console.error(chalk.red(err))); // eslint-disable-line
}
});
3 changes: 3 additions & 0 deletions flow-typed/npm-custom/insight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'insight' {
declare module.exports: Function
}
53 changes: 53 additions & 0 deletions lib/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.askPermission = askPermission;
exports.track = track;

var _querystring = require('querystring');

var _querystring2 = _interopRequireDefault(_querystring);

var _insight = require('insight');

var _insight2 = _interopRequireDefault(_insight);

var _package = require('../package.json');

var _package2 = _interopRequireDefault(_package);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }

const trackingCode = 'UA-88006586-1';
const trackingProvider = 'google';
const insight = new _insight2.default({ trackingCode: trackingCode, trackingProvider: trackingProvider, pkg: _package2.default });

function askPermission(cb) {
if (insight.optOut === undefined) {
// eslint-disable-line
return insight.askPermission(null, cb);
}
cb();
}

function track(path, input, flags) {
if (insight.optOut) return;

const filteredFlags = Object.keys(flags).reduce((acc, flag) => {
if (flag.length > 1) {
acc[flag] = flags[flag];
}

return acc;
}, {});

if (!input[0]) {
return flags.version ? setImmediate(() => insight.track('aik', 'version')) : setImmediate(() => insight.track('aik', 'help'));
}

setImmediate(() => insight.track.apply(insight, ['aik'].concat(_toConsumableArray(path), ['?' + _querystring2.default.stringify(filteredFlags)])));
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"gzip-size": "^3.0.0",
"html-loader": "^0.4.4",
"html-webpack-plugin": "^2.24.1",
"insight": "^0.8.4",
"json-loader": "^0.5.4",
"meow": "^3.7.0",
"ngrok": "^2.2.3",
Expand Down
36 changes: 36 additions & 0 deletions src/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* @flow */

import querystring from 'querystring';
import Insight from 'insight';
import pkg from '../package.json';

const trackingCode = 'UA-88006586-1';
const trackingProvider = 'google';
const insight = new Insight({ trackingCode, trackingProvider, pkg });

export function askPermission(cb: Function) {
if (insight.optOut === undefined) { // eslint-disable-line
return insight.askPermission(null, cb);
}
cb();
}

export function track(path: string[], input: string[], flags: CLIFlags) {
if (insight.optOut) return;

const filteredFlags = Object.keys(flags).reduce((acc, flag) => {
if (flag.length > 1) {
acc[flag] = flags[flag];
}

return acc;
}, {});

if (!input[0]) {
return flags.version
? setImmediate(() => insight.track('aik', 'version'))
: setImmediate(() => insight.track('aik', 'help'));
}

setImmediate(() => insight.track('aik', ...path, '?' + querystring.stringify(filteredFlags)));
}
119 changes: 113 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async@^0.9.0:
version "0.9.2"
resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d"

async@^1.3.0, async@^1.4.0, async@^1.5.0:
async@^1.3.0, async@^1.4.0, async@^1.4.2, async@^1.5.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"

Expand Down Expand Up @@ -1036,6 +1036,10 @@ cli-table@^0.3.1:
dependencies:
colors "1.0.3"

cli-width@^1.0.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-1.1.1.tgz#a4d293ef67ebb7b88d4a4d42c0ccf00c4d1e366d"

cli-width@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.1.0.tgz#b234ca209b29ef66fc518d9b98d5847b00edf00a"
Expand Down Expand Up @@ -1168,6 +1172,19 @@ concat-stream@^1.4.6, concat-stream@^1.4.7:
readable-stream "~2.0.0"
typedarray "~0.0.5"

configstore@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/configstore/-/configstore-1.4.0.tgz#c35781d0501d268c25c54b8b17f6240e8a4fb021"
dependencies:
graceful-fs "^4.1.2"
mkdirp "^0.5.0"
object-assign "^4.0.1"
os-tmpdir "^1.0.0"
osenv "^0.1.0"
uuid "^2.0.1"
write-file-atomic "^1.1.2"
xdg-basedir "^2.0.0"

connect-history-api-fallback@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/connect-history-api-fallback/-/connect-history-api-fallback-1.3.0.tgz#e51d17f8f0ef0db90a64fdb47de3051556e9f169"
Expand Down Expand Up @@ -2589,6 +2606,23 @@ ini@^1.3.2, ini@~1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

inquirer@^0.10.0:
version "0.10.1"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-0.10.1.tgz#ea25e4ce69ca145e05c99e46dcfec05e4012594a"
dependencies:
ansi-escapes "^1.1.0"
ansi-regex "^2.0.0"
chalk "^1.0.0"
cli-cursor "^1.0.1"
cli-width "^1.0.1"
figures "^1.3.5"
lodash "^3.3.1"
readline2 "^1.0.1"
run-async "^0.1.0"
rx-lite "^3.1.2"
strip-ansi "^3.0.0"
through "^2.3.6"

inquirer@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-0.12.0.tgz#1ef2bfd63504df0bc75785fff8c2c41df12f077e"
Expand All @@ -2607,6 +2641,21 @@ inquirer@^0.12.0:
strip-ansi "^3.0.0"
through "^2.3.6"

insight@^0.8.4:
version "0.8.4"
resolved "https://registry.yarnpkg.com/insight/-/insight-0.8.4.tgz#671caf65b47c9fe8c3d1b3206cf45bb211b75884"
dependencies:
async "^1.4.2"
chalk "^1.0.0"
configstore "^1.0.0"
inquirer "^0.10.0"
lodash.debounce "^3.0.1"
object-assign "^4.0.1"
os-name "^1.0.0"
request "^2.74.0"
tough-cookie "^2.0.0"
uuid "^3.0.0"

interpret@^0.6.4:
version "0.6.6"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-0.6.6.tgz#fecd7a18e7ce5ca6abfb953e1f86213a49f1625b"
Expand Down Expand Up @@ -2960,6 +3009,10 @@ lock@^0.1.2:
version "0.1.3"
resolved "https://registry.yarnpkg.com/lock/-/lock-0.1.3.tgz#f66c1caa0c41956e1426608039ed59b71bdf1854"

lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand All @@ -2968,6 +3021,12 @@ lodash.camelcase@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"

lodash.debounce@^3.0.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-3.1.1.tgz#812211c378a94cc29d5aa4e3346cf0bfce3a7df5"
dependencies:
lodash._getnative "^3.0.0"

lodash.indexof@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/lodash.indexof/-/lodash.indexof-4.0.5.tgz#53714adc2cddd6ed87638f893aa9b6c24e31ef3c"
Expand Down Expand Up @@ -2997,6 +3056,10 @@ lodash.templatesettings@^4.0.0:
dependencies:
lodash._reinterpolate "~3.0.0"

lodash@^3.3.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.0.0, lodash@^4.15.0, lodash@^4.16.2, lodash@^4.16.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
Expand Down Expand Up @@ -3127,7 +3190,7 @@ minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

minimist@^1.1.3, minimist@^1.2.0:
minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

Expand Down Expand Up @@ -3425,14 +3488,34 @@ os-homedir@^1.0.0, os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"

os-name@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/os-name/-/os-name-1.0.3.tgz#1b379f64835af7c5a7f498b357cb95215c159edf"
dependencies:
osx-release "^1.0.0"
win-release "^1.0.0"

os-shim@^0.1.2:
version "0.1.3"
resolved "https://registry.yarnpkg.com/os-shim/-/os-shim-0.1.3.tgz#6b62c3791cf7909ea35ed46e17658bb417cb3917"

os-tmpdir@^1.0.1:
os-tmpdir@^1.0.0, os-tmpdir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"

osenv@^0.1.0:
version "0.1.3"
resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.3.tgz#83cf05c6d6458fc4d5ac6362ea325d92f2754217"
dependencies:
os-homedir "^1.0.0"
os-tmpdir "^1.0.0"

osx-release@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/osx-release/-/osx-release-1.1.0.tgz#f217911a28136949af1bf9308b241e2737d3cd6c"
dependencies:
minimist "^1.1.0"

output-file-sync@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/output-file-sync/-/output-file-sync-1.1.2.tgz#d0a33eefe61a205facb90092e826598d5245ce76"
Expand Down Expand Up @@ -4275,7 +4358,7 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"

request@^2.55.0, request@^2.75.0:
request@^2.55.0, request@^2.74.0, request@^2.75.0:
version "2.79.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.79.0.tgz#4dfe5bf6be8b8cdc37fcf93e04b65577722710de"
dependencies:
Expand Down Expand Up @@ -4451,6 +4534,10 @@ slice-ansi@0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-0.0.4.tgz#edbf8903f66f7ce2f8eafd6ceed65e264c831b35"

slide@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707"

sntp@1.x.x:
version "1.0.9"
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"
Expand Down Expand Up @@ -4759,7 +4846,7 @@ touch@0.0.3:
dependencies:
nopt "~1.0.10"

tough-cookie@~2.3.0:
tough-cookie@^2.0.0, tough-cookie@~2.3.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"
dependencies:
Expand Down Expand Up @@ -4909,7 +4996,7 @@ utils-merge@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.0.tgz#0294fb922bb9375153541c4f7096231f287c8af8"

uuid@^2.0.2:
uuid@^2.0.1, uuid@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a"

Expand Down Expand Up @@ -5045,6 +5132,12 @@ wide-align@^1.1.0:
dependencies:
string-width "^1.0.1"

win-release@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/win-release/-/win-release-1.1.1.tgz#5fa55e02be7ca934edfc12665632e849b72e5209"
dependencies:
semver "^5.0.1"

window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"
Expand Down Expand Up @@ -5076,12 +5169,26 @@ wreck@^6.3.0:
boom "2.x.x"
hoek "2.x.x"

write-file-atomic@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-1.2.0.tgz#14c66d4e4cb3ca0565c28cf3b7a6f3e4d5938fab"
dependencies:
graceful-fs "^4.1.2"
imurmurhash "^0.1.4"
slide "^1.1.5"

write@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/write/-/write-0.2.1.tgz#5fc03828e264cea3fe91455476f7a3c566cb0757"
dependencies:
mkdirp "^0.5.1"

xdg-basedir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-2.0.0.tgz#edbc903cc385fc04523d966a335504b5504d1bd2"
dependencies:
os-homedir "^1.0.0"

xml-char-classes@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/xml-char-classes/-/xml-char-classes-1.0.0.tgz#64657848a20ffc5df583a42ad8a277b4512bbc4d"
Expand Down

0 comments on commit dc2446c

Please sign in to comment.