add session to visits and events

This commit is contained in:
Emily
2024-06-10 04:15:31 +02:00
parent 5a48ef53b4
commit f35e24efa8
3 changed files with 9 additions and 1 deletions

View File

@@ -77,12 +77,15 @@ async function process_visit(data: Record<string, string>, sessionHash: string)
browser: userAgentParsed.browser.name || 'NO_BROWSER', browser: userAgentParsed.browser.name || 'NO_BROWSER',
os: userAgentParsed.os.name || 'NO_OS', os: userAgentParsed.os.name || 'NO_OS',
device: userAgentParsed.device.type, device: userAgentParsed.device.type,
session: sessionHash,
continent: geoLocation[0], continent: geoLocation[0],
country: geoLocation[1], country: geoLocation[1],
}); });
await visit.save(); await visit.save();
await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } }, { upsert: true }); await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } }, { upsert: true });
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } }); await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } });
@@ -118,7 +121,7 @@ async function process_event(data: Record<string, string>, sessionHash: string)
metadataObject = { error: 'Error parsing metadata' } metadataObject = { error: 'Error parsing metadata' }
} }
const event = new EventModel({ project_id: pid, name, metadata: metadataObject }); const event = new EventModel({ project_id: pid, name, metadata: metadataObject, session: sessionHash });
await event.save(); await event.save();
await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'events': 1 } }, { upsert: true }); await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'events': 1 } }, { upsert: true });

View File

@@ -4,6 +4,7 @@ export type TEvent = {
project_id: Schema.Types.ObjectId, project_id: Schema.Types.ObjectId,
name: string, name: string,
metadata: Record<string, string>, metadata: Record<string, string>,
session: string,
created_at: Date created_at: Date
} }
@@ -11,6 +12,7 @@ const EventSchema = new Schema<TEvent>({
project_id: { type: Types.ObjectId, index: 1 }, project_id: { type: Types.ObjectId, index: 1 },
name: { type: String, required: true }, name: { type: String, required: true },
metadata: Schema.Types.Mixed, metadata: Schema.Types.Mixed,
session: { type: String },
created_at: { type: Date, default: () => Date.now() }, created_at: { type: Date, default: () => Date.now() },
}) })

View File

@@ -9,6 +9,7 @@ export type TVisit = {
continent: string, continent: string,
country: string, country: string,
session: string,
device: string, device: string,
website: string, website: string,
@@ -26,6 +27,8 @@ const VisitSchema = new Schema<TVisit>({
continent: { type: String }, continent: { type: String },
country: { type: String }, country: { type: String },
session: { type: String },
device: { type: String }, device: { type: String },