mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
add flow-hash
This commit is contained in:
@@ -3,7 +3,7 @@ import { RedisStreamService } from "@services/RedisStreamService";
|
||||
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import { createSessionHash, getIPFromRequest } from "./utils";
|
||||
import { createFlowSessionHash, createSessionHash, getIPFromRequest } from "./utils";
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -20,7 +20,8 @@ app.post('/event', express.json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
const ip = getIPFromRequest(req);
|
||||
const sessionHash = createSessionHash(req.body.website, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'event', sessionHash, ip });
|
||||
const flowHash = createFlowSessionHash(req.body.pid, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'event', sessionHash, ip, flowHash });
|
||||
return res.sendStatus(200);
|
||||
} catch (ex: any) {
|
||||
return res.status(500).json({ error: ex.message });
|
||||
@@ -31,7 +32,8 @@ app.post('/visit', express.json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
const ip = getIPFromRequest(req);
|
||||
const sessionHash = createSessionHash(req.body.website, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'visit', sessionHash, ip });
|
||||
const flowHash = createFlowSessionHash(req.body.pid, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, { ...req.body, _type: 'visit', sessionHash, ip, flowHash });
|
||||
return res.sendStatus(200);
|
||||
} catch (ex: any) {
|
||||
return res.status(500).json({ error: ex.message });
|
||||
@@ -42,9 +44,11 @@ app.post('/keep_alive', express.json(jsonOptions), async (req, res) => {
|
||||
try {
|
||||
const ip = getIPFromRequest(req);
|
||||
const sessionHash = createSessionHash(req.body.website, ip, req.body.userAgent);
|
||||
const flowHash = createFlowSessionHash(req.body.pid, ip, req.body.userAgent);
|
||||
await RedisStreamService.addToStream(streamName, {
|
||||
...req.body, _type: 'keep_alive', sessionHash, ip,
|
||||
instant: req.body.instant + ''
|
||||
instant: req.body.instant + '',
|
||||
flowHash
|
||||
});
|
||||
return res.sendStatus(200);
|
||||
} catch (ex: any) {
|
||||
|
||||
@@ -14,3 +14,12 @@ export function createSessionHash(website: string, ip: string, userAgent: string
|
||||
const sessionHash = crypto.createHash('md5').update(sessionClean).digest("hex");
|
||||
return sessionHash;
|
||||
}
|
||||
|
||||
|
||||
// Track user flow from referrers to cto
|
||||
export function createFlowSessionHash(project_id: string, ip: string, userAgent: string) {
|
||||
const dailySalt = new Date().toLocaleDateString('it-IT');
|
||||
const sessionClean = dailySalt + project_id + ip + userAgent;
|
||||
const sessionHash = crypto.createHash('md5').update(sessionClean).digest("hex");
|
||||
return sessionHash;
|
||||
}
|
||||
Reference in New Issue
Block a user