Skip to content

Commit

Permalink
Improve variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Dec 11, 2024
1 parent 3d648e2 commit 98a6716
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions app/analytics/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getMidnightDate(): Date {
return midnight;
}

function getNextModifiedDate(current: Date | null): Date {
function getNextLastModifiedDate(current: Date | null): Date {
// in case date is an 'Invalid Date'
if (current && isNaN(current.getTime())) {
current = null;
Expand All @@ -28,13 +28,16 @@ function getNextModifiedDate(current: Date | null): Date {
return next;
}

function getBounce(current: Date | null): number {
if (!current) {
function getBounceValue(nextLastModifiedDate: Date | null): number {
if (!nextLastModifiedDate) {
return 0;
}

const midnight = getMidnightDate();
const visits = (current.getTime() - midnight.getTime()) / 1000 - 1;

// NOTE: minus one because this is the response last modified date
const visits =
(nextLastModifiedDate.getTime() - midnight.getTime()) / 1000 - 1;

switch (visits) {
case 0:
Expand Down Expand Up @@ -93,7 +96,7 @@ export function collectRequestHandler(request: Request, env: Env) {

const ifModifiedSince = request.headers.get("if-modified-since");
const { newVisitor } = checkVisitorSession(ifModifiedSince);
const modifiedDate = getNextModifiedDate(
const nextLastModifiedDate = getNextLastModifiedDate(
ifModifiedSince ? new Date(ifModifiedSince) : null,
);

Expand All @@ -104,7 +107,7 @@ export function collectRequestHandler(request: Request, env: Env) {
referrer: params.r,
newVisitor: newVisitor ? 1 : 0,
newSession: 0, // dead column
bounce: newVisitor ? 1 : getBounce(modifiedDate),
bounce: newVisitor ? 1 : getBounceValue(nextLastModifiedDate),
// user agent stuff
userAgent: userAgent,
browserName: parsedUserAgent.getBrowser().name,
Expand Down Expand Up @@ -136,7 +139,7 @@ export function collectRequestHandler(request: Request, env: Env) {
Expires: "Mon, 01 Jan 1990 00:00:00 GMT",
"Cache-Control": "no-cache",
Pragma: "no-cache",
"Last-Modified": modifiedDate.toUTCString(),
"Last-Modified": nextLastModifiedDate.toUTCString(),
Tk: "N", // not tracking
},
status: 200,
Expand Down

0 comments on commit 98a6716

Please sign in to comment.