forked from NicoMiceli/slackalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (87 loc) · 2.23 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Set up Reqs
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var qs = require('querystring');
//set up heroku environment variables
var env_var = {
ga_key: process.env.GOOGLE_ANALYTICS_UAID
};
//Server Details
var app = express();
var port = process.env.PORT || 3000;
//Set Body Parser
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
//Routes
app.get('/', function(req, res){
res.send('here');
});
app.post('/collect', function(req, res){
var channel = {
id: req.body.channel_id,
name: req.body.channel_name
};
var user = {
id: req.body.user_id
};
var msgText = req.body.text;
var teamDomain = req.body.team_domain;
function searchM(regex){
var searchStr = msgText.match(regex);
if(searchStr != null){
return searchStr.length;
}
return 0;
};
function searchS(regex){
var searchStr = msgText.split(regex);
if(searchStr != undefined){
return searchStr.length;
}
return 0;
};
var wordCount = searchS(/\s+\b/);
var emojiCount = searchM(/:[a-z_0-9]*:/g);
var exclaCount = searchM(/!/g);
var questionMark = searchM(/\?/g);
var elipseCount = searchM(/\.\.\./g);
//Structure Data
var data = {
v: 1,
tid: env_var.ga_key,
cid: user.id,
ds: "slack", //data source
cs: "slack", // campaign source
cd1: user.id,
cd2: channel.name,
cd3: msgText,
cm1: wordCount,
cm2: emojiCount,
cm3: exclaCount,
// cm4: letterCount,
cm5: elipseCount,
cm6: questionMark, //need to set up in GA
dh: teamDomain+".slack.com",
dp: "/"+channel.name,
dt: "Slack Channel: "+channel.name,
t: "event",
ec: "slack: "+ channel.name + "|" + channel.id,
ea: "post by " + user.id,
el: msgText,
ev: 1
};
console.log(JSON.stringify(data));
console.log(req.body);
//Make Post Request
request.post("https://www.google-analytics.com/collect?" + qs.stringify(data),
function(error, resp, body){
console.log(error);
})
res.send("OK")
});
//Start Server
app.listen(port, function () {
console.log('Listening on port ' + port);
});