Skip to content

Commit 90fdab4

Browse files
authored
Merge pull request #108 from devakone/develop
Release: develop → main
2 parents 42d9704 + 47ba988 commit 90fdab4

9 files changed

Lines changed: 1914 additions & 631 deletions

File tree

apps/web/next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default withSentryConfig(nextConfig, {
3939
// For all available options, see:
4040
// https://www.npmjs.com/package/@sentry/webpack-plugin#options
4141
org: "100kode",
42-
project: "javascript-nextjs",
42+
project: "vibe-coding-profiler",
4343

4444
// Only print logs for uploading source maps in CI
4545
silent: !process.env.CI,

apps/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
"tw-animate-css": "^1.4.0",
4949
"typescript": "^5",
5050
"vite": "^7",
51-
"vitest": "^3"
51+
"vitest": "^3.2.6"
5252
}
5353
}

apps/web/src/components/PlausibleProvider.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { Suspense, useEffect, useRef } from "react";
44
import { usePathname, useSearchParams } from "next/navigation";
5-
import { init, track } from "@plausible-analytics/tracker";
65

76
/**
87
* Inner component that uses useSearchParams (requires Suspense boundary).
@@ -11,6 +10,7 @@ function PlausibleTracker() {
1110
const pathname = usePathname();
1211
const searchParams = useSearchParams();
1312
const isInitialized = useRef(false);
13+
const initialization = useRef<Promise<void> | null>(null);
1414

1515
// Initialize Plausible once
1616
useEffect(() => {
@@ -23,27 +23,24 @@ function PlausibleTracker() {
2323
return;
2424
}
2525

26-
if (!isInitialized.current) {
27-
init({
28-
domain,
29-
// Don't track localhost unless explicitly enabled
30-
captureOnLocalhost: process.env.NEXT_PUBLIC_PLAUSIBLE_CAPTURE_LOCALHOST === "true",
31-
// Track outbound link clicks
32-
outboundLinks: true,
33-
// Track file downloads
34-
fileDownloads: true,
35-
// Track form submissions
36-
formSubmissions: true,
37-
// Disable auto capture - we handle it manually for Next.js App Router
38-
autoCapturePageviews: false,
39-
});
40-
isInitialized.current = true;
41-
}
26+
initialization.current = import("@plausible-analytics/tracker").then(({ init }) => {
27+
if (!isInitialized.current) {
28+
init({
29+
domain,
30+
captureOnLocalhost: process.env.NEXT_PUBLIC_PLAUSIBLE_CAPTURE_LOCALHOST === "true",
31+
outboundLinks: true,
32+
fileDownloads: true,
33+
formSubmissions: true,
34+
autoCapturePageviews: false,
35+
});
36+
isInitialized.current = true;
37+
}
38+
});
4239
}, []);
4340

4441
// Track page views on route changes
4542
useEffect(() => {
46-
if (!process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN || !isInitialized.current) {
43+
if (!process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN || !initialization.current) {
4744
return;
4845
}
4946

@@ -53,7 +50,10 @@ function PlausibleTracker() {
5350
: pathname;
5451

5552
// Track pageview with the current URL
56-
track("pageview", { url });
53+
void initialization.current.then(async () => {
54+
const { track } = await import("@plausible-analytics/tracker");
55+
track("pageview", { url });
56+
});
5757
}, [pathname, searchParams]);
5858

5959
return null;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
describe("analytics modules", () => {
4+
it("can load during server rendering without browser globals", async () => {
5+
await expect(import("../analytics")).resolves.toBeDefined();
6+
await expect(import("../../components/PlausibleProvider")).resolves.toBeDefined();
7+
});
8+
});

apps/web/src/lib/analytics.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { track } from "@plausible-analytics/tracker";
2-
31
/**
42
* Track a custom event in Plausible Analytics.
53
*
@@ -20,10 +18,16 @@ export function trackEvent(
2018
return;
2119
}
2220

23-
track(eventName, {
24-
props,
25-
interactive: options?.interactive,
26-
revenue: options?.revenue,
21+
if (typeof window === "undefined") {
22+
return;
23+
}
24+
25+
void import("@plausible-analytics/tracker").then(({ track }) => {
26+
track(eventName, {
27+
props,
28+
interactive: options?.interactive,
29+
revenue: options?.revenue,
30+
});
2731
});
2832
}
2933

apps/web/vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default defineConfig({
1010
resolve: {
1111
alias: {
1212
"@": resolve(__dirname, "./src"),
13+
"@plausible-analytics/tracker": resolve(
14+
__dirname,
15+
"../../node_modules/@plausible-analytics/tracker/plausible.js"
16+
),
1317
},
1418
},
1519
});

0 commit comments

Comments
 (0)