1 | 'use strict';
|
2 |
|
3 | var request = require('request');
|
4 | var chalk = require('chalk');
|
5 | var ProgressBar = require('progress');
|
6 | var pkg = require('../package.json');
|
7 |
|
8 | var UA = 'GAERbot/' + pkg.version + ' (+' + pkg.homepage + ')';
|
9 | var GA_URL_ENDPOINT = 'https://ssl.google-analytics.com/collect';
|
10 | var GA_VERSION = 1;
|
11 | var GA_CLIENT_ID = Math.round(2147483647 * Math.random());
|
12 | var GA_HIT_TYPE = 'event';
|
13 | var GA_EVENT_CATEGORY = 'GAER';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function 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 |
|
60 | module.exports = gear; |
\ | No newline at end of file |