-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
t6events.js
90 lines (81 loc) · 3.2 KB
/
t6events.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
"use strict";
var t6events = module.exports = {};
var measurement = "events";
var retention = "autogen";
t6events.setMeasurement = function(m) {
measurement = m;
};
t6events.getMeasurement = function() {
return measurement;
};
t6events.setRP = function(rp) {
retention = rp;
};
t6events.getRP = function() {
return retention;
};
t6events.addAudit = function(where, what, who, client_id=null, params=null) {
where = where + ":" + process.env.NODE_ENV;
params.error_id = (typeof params.error_id!=="undefined" && params.error_id!==null)?params.error_id.toString():"";
//TODO : make sure 'what' does not contains multiple lines
if ( db_type.influxdb ) {
var tags = {rp: retention, what: what.replace(/(\r\n|\n|\r)/gm, ""), where: where};
var fields = {who: typeof who!=="undefined"?who:"", status: parseFloat((params!==null && typeof params.status!=="undefined")?params.status:""), error_id: params.error_id};
let dbWrite = typeof dbTelegraf!=="undefined"?dbTelegraf:dbInfluxDB;
dbWrite.writePoints([{
measurement: measurement,
tags: tags,
fields: fields,
}], { retentionPolicy: retention }).then((err) => {
if(err) {
t6console.warning("addAudit, t6events.addAudit: Error", err);
} else {
t6console.debug("addAudit, t6events.addAudit: Ok", measurement, tags, fields);
}
}).catch((err) => {
t6console.warning("addAudit, Error writting event to influxDb:", {measurement, fields, tags, retention}, err);
});
}
}
t6events.addStat = function(where, what, who, client_id=null, params=undefined) {
where = where + ":" + process.env.NODE_ENV;
let d = (logLevel.indexOf("DEBUG")>-1)?"debug/":"";
if(client_id!==null) {
let user_id = typeof who!=="undefined"?who:null;
client_id = typeof client_id!=="undefined"?client_id:"";
params = typeof params==="object"?params:{};
params.environment = where;
params.user_id = user_id;
var options = {
method: "POST",
url: `https://www.google-analytics.com/${d}mp/collect?v=2&firebase_app_id=${trackings.firebaseConfig.server.appId}&measurement_id=${trackings.firebaseConfig.server.measurementId}&api_secret=${trackings.firebaseConfig.server.api_secret}`,
body: JSON.stringify({
"user_id": user_id,
"client_id": client_id,
"nonPersonalizedAds": true,
"consent": {
"ad_user_data": "DENIED",
"ad_personalization": "DENIED"
},
"events": [{
name: what.replace(/[^a-zA-Z]/g,"_"),
params: params,
}]
})
};
request(options, function(error, response, body) {
if ( !error && typeof response!=="undefined" && typeof response.statusCode!=="undefined" && response.statusCode !== 404 ) {
t6console.info(`GA4 Event "${what.replace(/[^a-zA-Z]/g,"_")}" on measurement_id: ${trackings.firebaseConfig.server.measurementId}`);
t6console.info("GA4 user_id:", user_id);
t6console.info("GA4 client_id:", client_id);
t6console.info("GA4 environment:", where);
t6console.info("GA4 params:", params);
t6console.info("GA4 statusCode:", response.statusCode);
if (d==="debug/") { t6console.debug("GA4 body:", body); }
} else {
t6console.error("GA4 Error:", error, typeof response!=="undefined"?response.statusCode:"response is undefined or 404.", options);
}
});
}
};
module.exports = t6events;