Skip to content

Commit 72c37d5

Browse files
committed
diff command;
1 parent 5e01fdf commit 72c37d5

File tree

12 files changed

+196
-528
lines changed

12 files changed

+196
-528
lines changed

apps/docs/app/layout.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import "./global.css";
2-
import { Databuddy } from "@databuddy/sdk/react";
32
import { RootProvider } from "fumadocs-ui/provider";
43
import type { Metadata, Viewport } from "next";
54
import { Geist, Geist_Mono, Manrope } from "next/font/google";
5+
import Script from "next/script";
66
import { ThemeProvider } from "next-themes";
77
import type { ReactNode } from "react";
88
import { Toaster } from "@/components/ui/sonner";
@@ -91,12 +91,18 @@ export default function Layout({ children }: { children: ReactNode }) {
9191
lang="en"
9292
suppressHydrationWarning
9393
>
94-
<Databuddy
94+
{/* <Databuddy
9595
clientId="OXmNQsViBT-FOS_wZCTHc"
96+
disabled={process.env.NODE_ENV === "development"}
9697
trackAttributes
9798
trackErrors
9899
trackOutgoingLinks
99100
trackWebVitals
101+
/> */}
102+
<Script
103+
async
104+
data-client-id="OXmNQsViBT-FOS_wZCTHc"
105+
src="https://databuddy.b-cdn.net/databuddy.js"
100106
/>
101107
<body>
102108
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@biomejs/biome": "^2.3.6",
2222
"@types/bun": "latest",
2323
"@typescript/native-preview": "^7.0.0-dev.20251119.1",
24+
"chalk": "^5.6.2",
2425
"dotenv-cli": "^10.0.0",
2526
"turbo": "^2.6.1",
2627
"typescript": "^5.9.3",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"@biomejs/biome": "^2.3.6",
66
"@types/bun": "latest",
77
"@typescript/native-preview": "^7.0.0-dev.20251119.1",
8+
"chalk": "^5.6.2",
89
"dotenv-cli": "^10.0.0",
910
"turbo": "^2.6.1",
1011
"typescript": "^5.9.3",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { file, hash, spawn } from "bun";
2+
import chalk from "chalk";
3+
4+
const REMOTE_BASE_URL = "https://databuddy.b-cdn.net";
5+
6+
const FILES_TO_COMPARE = ["databuddy.js", "vitals.js", "errors.js"];
7+
8+
function getHash(content: string): string {
9+
return hash(content).toString();
10+
}
11+
12+
async function getRemoteFile(filename: string) {
13+
const url = `${REMOTE_BASE_URL}/${filename}`;
14+
try {
15+
const response = await fetch(url);
16+
if (!response.ok) {
17+
throw new Error(`Failed to fetch ${filename}: ${response.statusText}`);
18+
}
19+
const lastModified = response.headers.get("last-modified");
20+
const content = await response.text();
21+
return { content, lastModified, url };
22+
} catch (error) {
23+
console.error(chalk.red(`Error fetching ${filename}:`), error);
24+
return null;
25+
}
26+
}
27+
28+
async function buildLocalScript() {
29+
const proc = spawn(["bun", "build.ts"], {
30+
stdout: "pipe",
31+
stderr: "pipe",
32+
});
33+
await proc.exited;
34+
35+
if (proc.exitCode !== 0) {
36+
throw new Error("Build failed");
37+
}
38+
}
39+
40+
async function getLocalFile(filename: string) {
41+
const fileContent = file(`./dist/${filename}`);
42+
if (!(await fileContent.exists())) {
43+
throw new Error(`Local file not found: ./dist/${filename}`);
44+
}
45+
return await fileContent.text();
46+
}
47+
48+
async function compareFile(filename: string) {
49+
console.log(chalk.blue(`\nℹ️ Checking ${chalk.bold(filename)}...`));
50+
51+
const remote = await getRemoteFile(filename);
52+
if (!remote) {
53+
return;
54+
}
55+
56+
let localContent = "";
57+
try {
58+
localContent = await getLocalFile(filename);
59+
} catch (error) {
60+
console.error(chalk.red(`Failed to read local ${filename}:`), error);
61+
return;
62+
}
63+
64+
const remoteHash = getHash(remote.content);
65+
const localHash = getHash(localContent);
66+
67+
console.log(chalk.gray(` Remote URL: ${remote.url}`));
68+
console.log(
69+
chalk.gray(
70+
` Last Modified: ${remote.lastModified ? new Date(remote.lastModified).toLocaleString() : "Unknown"}`
71+
)
72+
);
73+
console.log(chalk.gray(` Remote Hash: ${remoteHash}`));
74+
console.log(chalk.gray(` Local Hash: ${localHash}`));
75+
76+
if (remoteHash === localHash) {
77+
console.log(chalk.green(" ✅ Match"));
78+
} else {
79+
console.log(chalk.yellow(" ⚠️ Mismatch"));
80+
}
81+
}
82+
83+
async function main() {
84+
console.log(chalk.bold("🔍 Databuddy Release Diff"));
85+
console.log(chalk.gray("──────────────────────────"));
86+
87+
console.log(chalk.blue("ℹ️ Building local scripts..."));
88+
try {
89+
await buildLocalScript();
90+
} catch (error) {
91+
console.error(chalk.red("Build failed:"), error);
92+
process.exit(1);
93+
}
94+
95+
for (const filename of FILES_TO_COMPARE) {
96+
await compareFile(filename);
97+
}
98+
}
99+
100+
main();

packages/tracker/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "bun build.ts",
88
"deploy": "bun deploy.ts",
99
"release": "bun run build && bun run deploy",
10+
"diff": "bun compare-release.ts",
1011
"dev": "bun build.ts --watch",
1112
"typecheck": "tsc --noEmit",
1213
"test:e2e": "playwright test",

packages/tracker/src/core/tracker.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class BaseTracker {
1010
api: HttpClient;
1111

1212
// State
13-
anonymousId: string | null = null;
14-
sessionId: string | null = null;
13+
anonymousId?: string;
14+
sessionId?: string;
1515
sessionStartTime = 0;
1616
lastActivityTime: number = Date.now();
1717

@@ -253,13 +253,13 @@ export class BaseTracker {
253253
protected getConnectionInfo() {
254254
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
255255
if (!connection) {
256-
return { connection_type: null, rtt: null, downlink: null };
256+
return {};
257257
}
258258

259259
return {
260-
connection_type: connection.effectiveType || connection.type || null,
261-
rtt: connection.rtt || null,
262-
downlink: connection.downlink || null,
260+
connection_type: connection.effectiveType || connection.type || undefined,
261+
rtt: connection.rtt || undefined,
262+
downlink: connection.downlink || undefined,
263263
};
264264
}
265265

@@ -269,11 +269,11 @@ export class BaseTracker {
269269
}
270270
const urlParams = new URLSearchParams(window.location.search);
271271
return {
272-
utm_source: urlParams.get("utm_source"),
273-
utm_medium: urlParams.get("utm_medium"),
274-
utm_campaign: urlParams.get("utm_campaign"),
275-
utm_term: urlParams.get("utm_term"),
276-
utm_content: urlParams.get("utm_content"),
272+
utm_source: urlParams.get("utm_source") || undefined,
273+
utm_medium: urlParams.get("utm_medium") || undefined,
274+
utm_campaign: urlParams.get("utm_campaign") || undefined,
275+
utm_term: urlParams.get("utm_term") || undefined,
276+
utm_content: urlParams.get("utm_content") || undefined,
277277
};
278278
}
279279

@@ -285,27 +285,27 @@ export class BaseTracker {
285285
const utmParams = this.getUtmParams();
286286
const connectionInfo = this.getConnectionInfo();
287287

288-
let width: number | null = window.innerWidth;
289-
let height: number | null = window.innerHeight;
288+
let width: number | undefined = window.innerWidth;
289+
let height: number | undefined = window.innerHeight;
290290
if (width < 240 || width > 10_000 || height < 240 || height > 10_000) {
291-
width = null;
292-
height = null;
291+
width = undefined;
292+
height = undefined;
293293
}
294-
const viewport_size = width && height ? `${width}x${height}` : null;
294+
const viewport_size = width && height ? `${width}x${height}` : undefined;
295295

296-
let screenWidth: number | null = window.screen.width;
297-
let screenHeight: number | null = window.screen.height;
296+
let screenWidth: number | undefined = window.screen.width;
297+
let screenHeight: number | undefined = window.screen.height;
298298
if (
299299
screenWidth < 240 ||
300300
screenWidth > 10_000 ||
301301
screenHeight < 240 ||
302302
screenHeight > 10_000
303303
) {
304-
screenWidth = null;
305-
screenHeight = null;
304+
screenWidth = undefined;
305+
screenHeight = undefined;
306306
}
307307
const screen_resolution =
308-
screenWidth && screenHeight ? `${screenWidth}x${screenHeight}` : null;
308+
screenWidth && screenHeight ? `${screenWidth}x${screenHeight}` : undefined;
309309

310310
const maskedPathname = this.getMaskedPath();
311311
const path =
@@ -314,7 +314,7 @@ export class BaseTracker {
314314
window.location.search +
315315
window.location.hash;
316316

317-
let timezone: string | null = null;
317+
let timezone: string | undefined;
318318
try {
319319
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
320320
} catch { }
@@ -335,24 +335,22 @@ export class BaseTracker {
335335
}
336336

337337
send(event: any): Promise<any> {
338-
const eventData =
339-
event.type === "track" && event.payload ? event.payload : event;
340338

341339
if (this.shouldSkipTracking()) {
342340
return Promise.resolve();
343341
}
344-
if (this.options.filter && !this.options.filter(eventData)) {
345-
logger.log("Event filtered", eventData);
342+
if (this.options.filter && !this.options.filter(event)) {
343+
logger.log("Event filtered", event);
346344
return Promise.resolve();
347345
}
348346

349347
if (this.options.enableBatching && !event.isForceSend) {
350-
logger.log("Queueing event for batch", eventData);
351-
return this.addToBatch(eventData);
348+
logger.log("Queueing event for batch", event);
349+
return this.addToBatch(event);
352350
}
353351

354-
logger.log("Sending event", eventData);
355-
return this.api.fetch("/", eventData, { keepalive: true });
352+
logger.log("Sending event", event);
353+
return this.api.fetch("/", event, { keepalive: true });
356354
}
357355

358356
addToBatch(event: any): Promise<void> {
@@ -396,7 +394,7 @@ export class BaseTracker {
396394
} catch (_error) {
397395
logger.error("Batch failed, retrying individually", _error);
398396
for (const evt of batchEvents) {
399-
this.send({ type: "track", payload: evt, isForceSend: true });
397+
this.send({ ...evt, isForceSend: true });
400398
}
401399
return null;
402400
}
@@ -425,8 +423,7 @@ export class BaseTracker {
425423
return null;
426424
}
427425
try {
428-
const eventData =
429-
event.type === "track" && event.payload ? event.payload : event;
426+
const eventData = event;
430427
if (this.shouldSkipTracking()) {
431428
return null;
432429
}

packages/tracker/src/core/types.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,36 @@ export type TrackerOptions = {
4040
export type EventContext = {
4141
path: string;
4242
title: string;
43-
referrer: string | null;
44-
screen_resolution: string | null;
45-
viewport_size: string | null;
46-
timezone: string | null;
43+
referrer?: string;
44+
screen_resolution?: string;
45+
viewport_size?: string;
46+
timezone?: string;
4747
language: string;
48-
connection_type: string | null;
49-
rtt: number | null;
50-
downlink: number | null;
51-
utm_source?: string | null;
52-
utm_medium?: string | null;
53-
utm_campaign?: string | null;
54-
utm_term?: string | null;
55-
utm_content?: string | null;
48+
connection_type?: string;
49+
rtt?: number;
50+
downlink?: number;
51+
utm_source?: string;
52+
utm_medium?: string;
53+
utm_campaign?: string;
54+
utm_term?: string;
55+
utm_content?: string;
5656
dbid?: string;
5757
}
5858

5959
export type BaseEvent = {
6060
eventId: string;
6161
name?: string;
62-
anonymousId: string | null;
63-
sessionId: string | null;
62+
anonymousId?: string;
63+
sessionId?: string;
6464
sessionStartTime?: number;
6565
timestamp: number;
6666
type?: string;
67-
payload?: any;
67+
[key: string]: any;
6868
}
6969

70-
export interface TrackEvent extends BaseEvent {
71-
type: 'track';
72-
payload: {
73-
name: string;
74-
[key: string]: any;
75-
};
76-
}
70+
export type TrackEvent = BaseEvent & {
71+
name: string;
72+
};
7773

7874
export type DatabuddyGlobal = {
7975
track: (name: string, props?: any) => void;

packages/tracker/src/core/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export function getTrackerConfig(): TrackerOptions {
4646
let script = document.currentScript as HTMLScriptElement;
4747

4848
if (!script) {
49-
// Fallback for scripts where document.currentScript is null
5049
const scripts = document.getElementsByTagName('script');
5150
for (let i = 0; i < scripts.length; i++) {
5251
const src = scripts[i].src;

packages/tracker/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class Databuddy extends BaseTracker {
166166
...this.getBaseContext(),
167167
...props,
168168
};
169-
this.send({ type: "track", payload });
169+
this.send(payload);
170170
}
171171
}
172172

0 commit comments

Comments
 (0)