add flow-hash

This commit is contained in:
Emily
2024-06-10 15:05:05 +02:00
parent e0981cef4a
commit 0c3a25b7e0
6 changed files with 31 additions and 10 deletions

View File

@@ -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) {

View File

@@ -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;
}