diff --git a/src/pages/api/send.ts b/src/pages/api/send.ts index ddaaca94f..bf952582e 100644 --- a/src/pages/api/send.ts +++ b/src/pages/api/send.ts @@ -11,7 +11,7 @@ import { } from 'next-basics'; import { COLLECTION_TYPE, HOSTNAME_REGEX, IP_REGEX } from 'lib/constants'; import { secret, visitSalt, uuid } from 'lib/crypto'; -import { hasBlockedIp } from 'lib/detect'; +import { getIpAddress, hasBlockedIp } from 'lib/detect'; import { useCors, useSession, useValidate } from 'lib/middleware'; import { CollectionType, YupRequest } from 'lib/types'; import { saveEvent, saveSessionData } from 'queries'; @@ -137,6 +137,8 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { urlPath = urlPath.replace(/(.+)\/$/, '$1'); } + const ip = req.body?.payload?.ip || getIpAddress(req); + await saveEvent({ urlPath, urlQuery, @@ -149,6 +151,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { ...session, sessionId: session.id, tag, + ip, }); } else if (type === COLLECTION_TYPE.identify) { if (!data) { diff --git a/src/queries/analytics/events/saveEvent.ts b/src/queries/analytics/events/saveEvent.ts index a19d2e7e6..62ce83b12 100644 --- a/src/queries/analytics/events/saveEvent.ts +++ b/src/queries/analytics/events/saveEvent.ts @@ -40,6 +40,7 @@ export async function saveEvent(args: { method: 'POST', headers: { 'Content-Type': 'application/json', + 'X-Forwarded-For': args.ip, }, body: JSON.stringify({ fingerprint: args.sessionId, @@ -53,6 +54,7 @@ export async function saveEvent(args: { device: args.device, screen: args.screen, language: args.language, + ip: args.ip, }), });