mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-12 16:48:36 +01:00
new selfhosted version
This commit is contained in:
40
dashboard/server/api/raw/export_visits.ts
Normal file
40
dashboard/server/api/raw/export_visits.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const ctx = await getRequestContext(event, 'pid', 'domain', 'permission:webAnalytics');
|
||||
const { project_id, domain } = ctx;
|
||||
|
||||
|
||||
const websiteMatch = domain ? { website: domain } : {};
|
||||
|
||||
const visits = await VisitModel.find({ project_id, ...websiteMatch });
|
||||
|
||||
const csvHeader = [
|
||||
"browser",
|
||||
"os",
|
||||
"continent",
|
||||
"country",
|
||||
"device",
|
||||
"website",
|
||||
"page",
|
||||
"referrer",
|
||||
"created_at",
|
||||
];
|
||||
|
||||
|
||||
const lines: any[] = [];
|
||||
visits.forEach(line => lines.push(line.toJSON()));
|
||||
|
||||
const result = csvHeader.join(',') + '\n' + lines.map(element => {
|
||||
const content: string[] = [];
|
||||
for (const key of csvHeader) {
|
||||
content.push(element[key]);
|
||||
}
|
||||
return content.join(',');
|
||||
}).join('\n');
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user