Skip to content

Commit

Permalink
Added option to exclude hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Dec 26, 2024
1 parent 2d27333 commit 4ddf8d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/pages/api/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
methodNotAllowed,
ok,
safeDecodeURI,
send,
} from 'next-basics';
import { COLLECTION_TYPE, HOSTNAME_REGEX, IP_REGEX } from 'lib/constants';
import { secret, visitSalt, uuid } from 'lib/crypto';
Expand Down Expand Up @@ -103,7 +102,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {

const session = req.session;

if (!session?.id) {
if (!session?.id || !session?.websiteId) {
return;
}

Expand Down Expand Up @@ -162,9 +161,9 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
});
}

const token = createToken(session, secret());
const cache = createToken(session, secret());

return send(res, token);
return ok(res, { cache });
}

return methodNotAllowed(res);
Expand Down
16 changes: 10 additions & 6 deletions src/tracker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
const tag = attr(_data + 'tag');
const autoTrack = attr(_data + 'auto-track') !== _false;
const excludeSearch = attr(_data + 'exclude-search') === _true;
const excludeHash = attr(_data + 'exclude-hash') === _true;
const domain = attr(_data + 'domains') || '';
const domains = domain.split(',').map(n => n.trim());
const host =
Expand Down Expand Up @@ -53,13 +54,12 @@

const parseURL = url => {
try {
// use location.origin as the base to handle cases where the url is a relative path
const { pathname, search, hash } = new URL(url, location.href);
url = pathname + search + hash;

return pathname + (excludeSearch ? '' : search) + (excludeHash ? '' : hash);
} catch (e) {
/* empty */
return url;
}
return excludeSearch ? url.split('?')[0] : url;
};

const getPayload = () => ({
Expand Down Expand Up @@ -194,6 +194,7 @@
/* Tracking functions */

const trackingDisabled = () =>
disabled ||
!website ||
(localStorage && localStorage.getItem('umami.disabled')) ||
(domain && !domains.includes(hostname));
Expand All @@ -215,9 +216,11 @@
body: JSON.stringify({ type, payload }),
headers,
});
const text = await res.text();

return (cache = text);
const data = await res.json();

disabled = res.status === 429;
cache = data?.cache;
} catch (e) {
/* empty */
}
Expand Down Expand Up @@ -264,6 +267,7 @@
let title = document.title;
let cache;
let initialized;
let disabled = false;

if (autoTrack && !trackingDisabled()) {
if (document.readyState === 'complete') {
Expand Down

0 comments on commit 4ddf8d0

Please sign in to comment.