Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tired-cougars-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@simpleanalytics/next": major
---

# 1.0.0

Initial release of the Simple Analytics for Next.js package.
26 changes: 13 additions & 13 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
"postinstall": "fumadocs-mdx"
},
"dependencies": {
"@simpleanalytics/next": "workspace:*",
"clsx": "^2.1.1",
"fumadocs-core": "14.7.7",
"fumadocs-docgen": "^1.3.5",
"fumadocs-mdx": "11.3.1",
"fumadocs-typescript": "^3.0.3",
"fumadocs-mdx": "11.5.7",
"fumadocs-typescript": "^3.1.0",
"fumadocs-ui": "14.7.7",
"lucide-react": "^0.474.0",
"next": "15.1.5",
"lucide-react": "^0.483.0",
"next": "15.2.3",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"source-map-support": "^0.5.21",
"tailwind-merge": "^2.6.0",
"@simpleanalytics/next": "workspace:*"
"tailwind-merge": "^3.0.2"
},
"devDependencies": {
"@types/mdx": "^2.0.13",
"@types/node": "22.10.7",
"@types/react": "^19.0.7",
"@types/react-dom": "^19.0.3",
"autoprefixer": "^10.4.20",
"@types/node": "22.13.11",
"@types/react": "^19.0.12",
"@types/react-dom": "^19.0.4",
"autoprefixer": "^10.4.21",
"eslint": "^8",
"eslint-config-next": "15.1.5",
"postcss": "^8.5.1",
"eslint-config-next": "15.2.3",
"postcss": "^8.5.3",
"tailwindcss": "^3.4.17",
"typescript": "^5.7.3"
"typescript": "^5.8.2"
}
}
12 changes: 6 additions & 6 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
},
"dependencies": {
"@simpleanalytics/next": "workspace:*",
"next": "15.1.5",
"next": "15.2.3",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@types/node": "^22.10.7",
"@types/react": "^19.0.7",
"@types/react-dom": "^19.0.3",
"postcss": "^8.5.1",
"@types/node": "^22.13.11",
"@types/react": "^19.0.12",
"@types/react-dom": "^19.0.4",
"postcss": "^8.5.3",
"tailwindcss": "^3.4.1",
"typescript": "^5.7.3"
"typescript": "^5.8.2"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
"turbo": "latest"
"turbo": "^2.4.4"
},
"packageManager": "pnpm@9.15.0"
}
12 changes: 6 additions & 6 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@simpleanalytics/next",
"version": "0.0.1",
"version": "1.0.0",
"description": "Simple Analytics for Next.js",
"keywords": [
"analytics",
Expand Down Expand Up @@ -33,12 +33,12 @@
"server-only": "^0.0.1"
},
"devDependencies": {
"@types/node": "^22.10.7",
"@types/react": "^19.0.7",
"@types/node": "^22.13.11",
"@types/react": "^19.0.12",
"esbuild-plugin-preserve-directives": "^0.0.11",
"prettier": "3.4.2",
"tsup": "^8.3.5",
"webpack": "latest"
"prettier": "3.5.3",
"tsup": "^8.4.0",
"webpack": "^5.98.0"
},
"peerDependencies": {
"next": ">= 13",
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function parseDataProps(settings?: SimpleAnalyticsProps) {
}

if (!isProduction()) {
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting NEXT_PUBLIC_ENABLE_ANALYTICS_IN_DEV=1 in your environment");
console.log(
"Simple Analytics is disabled by default in development and preview environments, enable it by setting NEXT_PUBLIC_ENABLE_ANALYTICS_IN_DEV=1 in your environment",
);
return {};
}

Expand Down
8 changes: 5 additions & 3 deletions packages/analytics/src/middleware/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ interface RoutesModule {

const INTERNAL_ANALYTICS_PATHS = /^\/(proxy\.js|auto-events\.js|simple\/.*)$/;

export async function isIndexedRoute(path: string) {
export async function isValidPageview(path: string) {
if (INTERNAL_ANALYTICS_PATHS.test(path)) {
return false;
}

// @ts-expect-error
const { pattern }: RoutesModule = await import("DO_NOT_USE_OR_JEAN_WILL_GET_FIRED");
const { pattern }: RoutesModule = await import(
// @ts-expect-error
"DO_NOT_USE_OR_JEAN_WILL_GET_FIRED"
);

return pattern.test(path);
}
21 changes: 16 additions & 5 deletions packages/analytics/src/middleware/track-pageview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import type {
ServerContext,
TrackingOptions,
} from "../server/interfaces";
import { isBuildTime, isDoNotTrackEnabled, isProduction, parseRequest, isEnhancedBotDetectionEnabled } from "../server/utils";
import {
isBuildTime,
isDoNotTrackEnabled,
isProduction,
parseRequest,
isEnhancedBotDetectionEnabled,
} from "../server/utils";
import { parseUtmParameters } from "../server/utm";
import { isIndexedRoute } from "./routes";
import { isValidPageview } from "./routes";

type TrackPageviewOptions = TrackingOptions & ServerContext;

Expand Down Expand Up @@ -43,11 +49,13 @@ export async function trackPageview(options: TrackPageviewOptions) {
}

if (!isProduction()) {
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment");
console.log(
"Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment",
);
return;
}

if (!(await isIndexedRoute(path))) {
if (!(await isValidPageview(path))) {
return;
}

Expand All @@ -68,7 +76,10 @@ export async function trackPageview(options: TrackPageviewOptions) {
method: "POST",
headers: {
"Content-Type": "application/json",
...(headers.has("X-Forwarded-For") && isEnhancedBotDetectionEnabled(options) && { "X-Forwarded-For": headers.get("X-Forwarded-For")! }),
...(headers.has("X-Forwarded-For") &&
isEnhancedBotDetectionEnabled(options) && {
"X-Forwarded-For": headers.get("X-Forwarded-For")!,
}),
},
body: JSON.stringify(payload),
});
Expand Down
6 changes: 4 additions & 2 deletions packages/analytics/src/plugin/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,7 @@ export function resolveRoutes(options = { useAbsolutePath: false }): string {
// Write collected routes to file
writeRoutesToFile(process.cwd(), routes);

return options.useAbsolutePath ? resolve(resolve(process.cwd(), ".simpleanalytics"), "routes.js") : "./.simpleanalytics/routes.js";
}
return options.useAbsolutePath
? resolve(resolve(process.cwd(), ".simpleanalytics"), "routes.js")
: "./.simpleanalytics/routes.js";
}
16 changes: 9 additions & 7 deletions packages/analytics/src/plugin/with-simple-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export function withSimpleAnalytics(
...nextConfig?.experimental?.turbo?.resolveAlias,
// Turbo aliases don't work with absolute
// paths (see error handling above)
"DO_NOT_USE_OR_JEAN_WILL_GET_FIRED": resolveRoutes()
}
}
DO_NOT_USE_OR_JEAN_WILL_GET_FIRED: resolveRoutes(),
},
},
},
webpack(config: Configuration, options: WebpackConfigContext) {
config = {
Expand All @@ -64,11 +64,13 @@ export function withSimpleAnalytics(
...config.resolve,
alias: {
...config.resolve?.alias,
"DO_NOT_USE_OR_JEAN_WILL_GET_FIRED": resolveRoutes({ useAbsolutePath: true })
}
}
DO_NOT_USE_OR_JEAN_WILL_GET_FIRED: resolveRoutes({
useAbsolutePath: true,
}),
},
},
};

return nextConfig.webpack?.(config, options) ?? config;
},
}),
Expand Down
26 changes: 21 additions & 5 deletions packages/analytics/src/server/simple-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import type {
HeaderOnlyContext,
ServerContext,
} from "./interfaces";
import { isBuildTime, isProduction, isDoNotTrackEnabled, parseRequest, isEnhancedBotDetectionEnabled } from "./utils";
import {
isBuildTime,
isProduction,
isDoNotTrackEnabled,
parseRequest,
isEnhancedBotDetectionEnabled,
} from "./utils";
import { parseHeaders } from "./headers";
import { parseUtmParameters } from "./utm";

Expand Down Expand Up @@ -45,15 +51,20 @@ export async function trackEvent(
}

if (!isProduction()) {
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment");
console.log(
"Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment",
);
return;
}

const response = await fetch("https://queue.simpleanalyticscdn.com/events", {
method: "POST",
headers: {
"Content-Type": "application/json",
...(headers.has("X-Forwarded-For") && isEnhancedBotDetectionEnabled(options) && { "X-Forwarded-For": headers.get("X-Forwarded-For")! }),
...(headers.has("X-Forwarded-For") &&
isEnhancedBotDetectionEnabled(options) && {
"X-Forwarded-For": headers.get("X-Forwarded-For")!,
}),
},
body: JSON.stringify(payload),
});
Expand Down Expand Up @@ -124,15 +135,20 @@ export async function trackPageview(options: TrackPageviewOptions) {
}

if (!isProduction()) {
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment");
console.log(
"Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment",
);
return;
}

const response = await fetch("https://queue.simpleanalyticscdn.com/events", {
method: "POST",
headers: {
"Content-Type": "application/json",
...(headers.has("X-Forwarded-For") && isEnhancedBotDetectionEnabled(options) && { "X-Forwarded-For": headers.get("X-Forwarded-For")! }),
...(headers.has("X-Forwarded-For") &&
isEnhancedBotDetectionEnabled(options) && {
"X-Forwarded-For": headers.get("X-Forwarded-For")!,
}),
},
body: JSON.stringify(payload),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ export function isBuildTime() {

export function isEnhancedBotDetectionEnabled(options: TrackingOptions) {
return options.enhancedBotDetection === true;
}
}
Loading
Loading