Updated encoding logic in tracker and send.

This commit is contained in:
Mike Cao 2024-03-26 21:46:57 -07:00
parent 4ee7deafdd
commit 91f49ba506
2 changed files with 30 additions and 8 deletions

View File

@ -6,7 +6,15 @@ import { getIpAddress } from 'lib/detect';
import { useCors, useSession, useValidate } from 'lib/middleware'; import { useCors, useSession, useValidate } from 'lib/middleware';
import { CollectionType, YupRequest } from 'lib/types'; import { CollectionType, YupRequest } from 'lib/types';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { badRequest, createToken, forbidden, methodNotAllowed, ok, send } from 'next-basics'; import {
badRequest,
createToken,
forbidden,
methodNotAllowed,
ok,
safeDecodeURI,
send,
} from 'next-basics';
import { saveEvent, saveSessionData } from 'queries'; import { saveEvent, saveSessionData } from 'queries';
import * as yup from 'yup'; import * as yup from 'yup';
@ -88,8 +96,8 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
} }
const { type, payload } = req.body; const { type, payload } = req.body;
const { url, referrer, name: eventName, data: eventData, title } = payload;
const { url, referrer, name: eventName, data: eventData, title: pageTitle } = payload; const pageTitle = safeDecodeURI(title);
await useSession(req, res); await useSession(req, res);
@ -105,8 +113,8 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
if (type === COLLECTION_TYPE.event) { if (type === COLLECTION_TYPE.event) {
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let [urlPath, urlQuery] = url?.split('?') || []; let [urlPath, urlQuery] = safeDecodeURI(url)?.split('?') || [];
let [referrerPath, referrerQuery] = referrer?.split('?') || []; let [referrerPath, referrerQuery] = safeDecodeURI(referrer)?.split('?') || [];
let referrerDomain = ''; let referrerDomain = '';
if (!urlPath) { if (!urlPath) {

View File

@ -32,6 +32,20 @@
/* Helper functions */ /* Helper functions */
const encode = str => {
try {
const result = decodeURI(str);
if (result !== str) {
return result;
}
} catch {
return str;
}
return encodeURI(str);
};
const parseURL = url => { const parseURL = url => {
return excludeSearch ? url.split('?')[0] : url; return excludeSearch ? url.split('?')[0] : url;
}; };
@ -41,9 +55,9 @@
hostname, hostname,
screen, screen,
language, language,
title: encodeURIComponent(title), title: encode(title),
url: encodeURI(currentUrl), url: encode(currentUrl),
referrer: encodeURI(currentRef), referrer: encode(currentRef),
}); });
/* Event handlers */ /* Event handlers */