-
Notifications
You must be signed in to change notification settings - Fork 2
/
MixpanelOverChecker.gs
134 lines (114 loc) · 3.28 KB
/
MixpanelOverChecker.gs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
API_KEY = "YOUR_MIXPANEL_API_KEY";
API_SECRET = "YOUR_MIXPANEL_API_SECRET";
MAIL_ADDRESS = "YOUR_MAIL_ADDRESS";
EVENTS = ["EVENT1", "EVENT2", "EVENT3"];
RATIO = 1.5;
data = {
'unit' : "day",
'interval' : 15,
'type' : "general"
}
function main() {
for(var i = 0; i < EVENTS.length; i++) {
check(EVENTS[i]);
}
}
function check(event) {
var API_URL = "http://mixpanel.com/api/2.0/events/?";
var response, jsonString, object, series = [], values = [], i = 0, ave = 0;
try {
data['event'] = '["' + event + '"]';
data['sig'] = getSig(data);
data['event'] = encodeURIComponent(data['event']);
response = UrlFetchApp.fetch(API_URL + makeParam(data));
if (!response) {
Logger.log("no response");
return;
}
jsonString = response.getContentText();
object = Utilities.jsonParse(jsonString);
series = object.data.series;
values = object.data.values[event];
for(i = 0; i < series.length - 1; i++) {
ave += values[series[i]];
}
ave /= (series.length - 1);
ave = Math.floor(ave);
lastValue = values[series[series.length - 1]];
if (lastValue >= ave * RATIO && !exist(event, ave)) {
write(event, ave, lastValue);
sendMail(event, ave, lastValue);
}
Logger.log("done!");
} catch(e) {
Logger.log(e);
response = null;
}
}
function getSig(hash) {
var keys = [];
var kv = "";
var currentDate = new Date;
var expire = Math.floor(currentDate.getTime() / 1000) + 600;
delete hash['sig'];
hash['api_key'] = API_KEY;
hash['expire'] = expire;
hash['format'] = "json";
for (var k in hash) keys.push(k);
keys.sort();
var length = keys.length;
for(var i = 0; i < length; i++){
kv += keys[i] + "=" + hash[keys[i]];
}
return getMD5Hash(kv + API_SECRET);
}
function getMD5Hash(input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
var txtHash = '';
for (j = 0; j < rawHash.length; j++) {
var hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
return txtHash;
}
function makeParam(hash) {
var param = "";
for(var key in hash) {
param += key + "=" + hash[key] + "&";
}
param = param.substring(0, param.length-1);
return param;
}
function exist(event, ave) {
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getLastRow();
var cell, i, _event, _ave, _date;
for(i = 1; i <= row; i++) {
_event = sheet.getRange('A'+ i).getValue();
_ave = sheet.getRange('B'+ i).getValue();
_date = sheet.getRange('D'+ i).getValue();
if(event == _event && ave == _ave && getDateWithFormat() == _date) {
return true;
}
}
return false;
}
function write(event, ave, lastValue) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([event, ave, lastValue, getDateWithFormat()]);
}
function sendMail(event, ave, lastValue) {
MailApp.sendEmail(
MAIL_ADDRESS,
"#alert " + event + " event is greather than the average value",
"event name: " + event + ", average: " + ave + ", lastValue: " + lastValue
);
}
function getDateWithFormat() {
var date = Utilities.formatDate(new Date(), "JST", "yyyy/MM/dd");
return '"' + date + '"';
}