UNPKG

1.44 kBJavaScriptView Raw
1'use strict';
2
3var request = require('request');
4var chalk = require('chalk');
5var ProgressBar = require('progress');
6var pkg = require('../package.json');
7
8var UA = 'GAERbot/' + pkg.version + ' (+' + pkg.homepage + ')';
9var GA_URL_ENDPOINT = 'https://ssl.google-analytics.com/collect';
10var GA_VERSION = 1;
11var GA_CLIENT_ID = Math.round(2147483647 * Math.random());
12var GA_HIT_TYPE = 'event';
13var GA_EVENT_CATEGORY = 'GAER';
14
15
16/**
17 * Argument is file path or not
18 * @param {String} Tracking ID
19 * @param {String} Report name
20 * @param {Object} Report data
21 */
22function gear(id, action, obj) {
23 var options = {
24 url: GA_URL_ENDPOINT,
25 headers: {'User-Agent': UA},
26 form: {
27 v: GA_VERSION,
28 tid: id,
29 cid: GA_CLIENT_ID,
30 t: GA_HIT_TYPE,
31 ec: GA_EVENT_CATEGORY,
32 ea: action
33 }
34 };
35 var bar = new ProgressBar('Sending [:bar] :percent', {
36 complete: '|',
37 incomplete: '+',
38 width: 24,
39 total: Object.keys(obj).length
40 });
41 Object.keys(obj).forEach(function (key, value) {
42 options.form = {
43 el: key,
44 ev: value
45 };
46 request.post(options, function (error, response) {
47 if (!error && response.statusCode == 200) {
48 bar.tick();
49 if (bar.complete) {
50 console.log(chalk.green('Completed: The data is sent to ' + id));
51 }
52 } else {
53 util.error(error);
54 }
55 }
56 );
57 });
58}
59
60module.exports = gear;
\No newline at end of file