forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_web_event.js
32 lines (31 loc) · 1.15 KB
/
record_web_event.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
import {WebEvent, esc} from 'db/models';
export default function recordWebEvent(ctx, event_type, value) {
if (ctx.state.isBot) return;
const s = ctx.session;
const r = ctx.req;
let new_session = true;
if (ctx.last_visit) {
new_session = ((new Date()).getTime() / 1000 - ctx.last_visit) > 1800;
}
const remote_address = r.headers['x-forwarded-for'] || r.connection.remoteAddress;
const ip_match = remote_address ? remote_address.match(/(\d+\.\d+\.\d+\.\d+)/) : null;
const d = {
event_type: esc(event_type, 1000),
user_id: s.user,
uid: s.uid,
account_name: null,
first_visit: ctx.first_visit,
new_session,
ip: ip_match ? ip_match[1] : null,
value: value ? esc(value, 1000) : esc(r.originalUrl),
refurl: esc(r.headers.referrer || r.headers.referer),
user_agent: esc(r.headers['user-agent']),
status: ctx.status,
channel: esc(s.ch, 64),
referrer: esc(s.r, 64),
campaign: esc(s.cn, 64)
};
WebEvent.create(d, {logging: false}).catch(error => {
console.error('!!! Can\'t create web event record', error);
});
}