Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
qwtel committed Jul 29, 2024
1 parent c6af5c2 commit 05054ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/analytics/collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function generateRequestParams(headers: Record<string, string>) {
sid: "example",
h: "example.com",
p: "/post/123",
r: "https://google.com",
r: "https://search.brave.com",
nv: "1",
ns: "1",
}).toString(),
Expand Down Expand Up @@ -61,7 +61,7 @@ describe("collectRequestHandler", () => {
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", // ua string
"/post/123", // url
"US", // country
"https://google.com", // referrer
"search.brave.com", // referrer
"Chrome", // browser name
"",
"example", // site id
Expand Down
35 changes: 19 additions & 16 deletions app/analytics/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ function checkVisitorSession(ifModifiedSince: string | null): {
return { newVisitor, newSession };
}

function extractParamsFromQueryString(requestUrl: string): {
[key: string]: string;
} {
const url = new URL(requestUrl);
const queryString = url.search.slice(1).split("&");

const params: { [key: string]: string } = {};

queryString.forEach((item) => {
const kv = item.split("=");
if (kv[0]) params[kv[0]] = decodeURIComponent(kv[1]);
});
return params;
function extractParamsFromQueryString(url: URL): Record<string, string> {
return Object.fromEntries(url.searchParams);
}

export function collectRequestHandler(request: Request, env: Env) {
const params = extractParamsFromQueryString(request.url);
const url = new URL(request.url);
const params = extractParamsFromQueryString(url);

const userAgent = request.headers.get("user-agent") || undefined;
const parsedUserAgent = new UAParser(userAgent);
Expand All @@ -63,15 +53,28 @@ export function collectRequestHandler(request: Request, env: Env) {
request.headers.get("if-modified-since"),
);

const search = params.s ? new URLSearchParams(params.s) : undefined;
let referrer = search?.get("ref") || params.r;

if (referrer && URL.canParse(referrer)) {
const url = new URL(referrer);
if (url.protocol === "http:" || url.protocol === "https:") {
referrer = url.hostname;
}
if (referrer.startsWith("www.")) {
referrer = referrer.substring(4);
}
}

const data: DataPoint = {
siteId: params.sid,
host: params.h,
path: params.p,
referrer: params.r,
referrer,
newVisitor: newVisitor ? 1 : 0,
newSession: newSession ? 1 : 0,
// user agent stuff
userAgent: userAgent,
userAgent,
browserName: parsedUserAgent.getBrowser().name,
deviceModel: parsedUserAgent.getDevice().model,
};
Expand Down
13 changes: 8 additions & 5 deletions public/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ SOFTWARE.
*/

(function () {
(function (window, document) {
"use strict";

let queue = (window.counterscale && window.counterscale.q) || [];
let config = {
siteId: "",
siteId: window.location.hostname,
trackerUrl: "",
};
const commands = {
Expand Down Expand Up @@ -116,15 +116,17 @@ SOFTWARE.
req = a;
}

let path = vars.path || req.pathname + req.search;
let path = vars.path || req.pathname;
if (!path) {
path = "/";
}
// strip query string from path
path = path.split("?")[0];

const search = vars.search || req.search;

// determine hostname
let hostname = vars.hostname || req.protocol + "//" + req.hostname;
let hostname = vars.hostname || req.hostname;

// only set referrer if not internal
let referrer = vars.referrer || "";
Expand All @@ -136,6 +138,7 @@ SOFTWARE.

const d = {
p: path,
s: search,
h: hostname,
r: referrer,
sid: config.siteId,
Expand Down Expand Up @@ -175,4 +178,4 @@ SOFTWARE.

// process existing queue
queue.forEach((i) => counterscale.apply(this, i));
})();
})(window, document);

0 comments on commit 05054ed

Please sign in to comment.