Skip to content

Commit 1eb7f4c

Browse files
Merge pull request #28 from simpleanalytics/fix/improve-dx-during-dev
fix(client): improve dx during dev
2 parents 8f17a19 + 5a7b106 commit 1eb7f4c

File tree

4 files changed

+27
-48
lines changed

4 files changed

+27
-48
lines changed

packages/analytics/src/client/simple-analytics.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import React, { Suspense } from "react";
3+
import * as React from "react";
44
import Script from "next/script";
55
import type { AnalyticsMetadata } from "../interfaces";
66
import { parseDataProps } from "./utils";
@@ -32,9 +32,9 @@ export const SimpleAnalytics = (props: SimpleAnalyticsProps) => {
3232
const dataProps = parseDataProps(props);
3333

3434
return (
35-
<Suspense fallback={null}>
35+
<React.Suspense fallback={null}>
3636
<Script {...dataProps} src="/proxy.js" />
37-
</Suspense>
37+
</React.Suspense>
3838
);
3939
};
4040

packages/analytics/src/client/utils.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,28 @@ export function isProduction() {
1717
return process.env.NEXT_PUBLIC_VERCEL_ENV === "production";
1818
}
1919

20-
function isBuildTime() {
21-
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD;
22-
}
23-
2420
export function parseDataProps(settings?: SimpleAnalyticsProps) {
25-
if (!process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME) {
26-
console.error("No hostname provided for Simple Analytics");
27-
return {};
28-
}
29-
30-
if (isBuildTime()) {
31-
return {};
32-
}
33-
3421
if (!isProduction()) {
35-
console.log(
36-
"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",
37-
);
3822
return {};
3923
}
4024

41-
if (!settings) {
42-
return {
43-
"data-hostname": process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
44-
};
45-
}
46-
47-
const metrics = settings.ignoreMetrics
25+
const metrics = settings?.ignoreMetrics
4826
? Object.entries(settings.ignoreMetrics)
4927
.filter(([_, value]) => value)
5028
.map(([key]) => `${key}`)
5129
.join(",")
5230
: undefined;
5331

5432
return {
55-
"data-auto-collect": settings.autoCollect,
56-
"data-collect-dnt": settings.collectDnt,
33+
"data-auto-collect": settings?.autoCollect,
34+
"data-collect-dnt": settings?.collectDnt,
5735
"data-hostname":
58-
settings.hostname ?? process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
59-
"data-mode": settings.mode,
36+
settings?.hostname ?? process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
37+
"data-mode": settings?.mode,
6038
"data-ignore-metrics": metrics === "" ? undefined : metrics,
61-
"data-ignore-pages": settings.ignorePages?.join(","),
62-
"data-allow-params": settings.allowParams?.join(","),
63-
"data-non-unique-params": settings.nonUniqueParams?.join(","),
64-
"data-strict-utm": settings.strictUtm,
39+
"data-ignore-pages": settings?.ignorePages?.join(","),
40+
"data-allow-params": settings?.allowParams?.join(","),
41+
"data-non-unique-params": settings?.nonUniqueParams?.join(","),
42+
"data-strict-utm": settings?.strictUtm,
6543
};
6644
}

packages/analytics/src/plugin/with-simple-analytics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export function withSimpleAnalytics(
3838
): NextConfig {
3939
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
4040

41+
if (!hostname) {
42+
console.warn("No hostname provided for Simple Analytics, plugin disabled.");
43+
return nextConfig;
44+
}
45+
4146
const clientHints = buildClientHintHeaders(options?.clientHints);
4247

4348
const nextAnalyticsConfig: NextConfig = {

packages/analytics/src/server/simple-analytics.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@ export async function trackEvent(
2323
eventName: string,
2424
options: TrackEventOptions,
2525
) {
26-
const hostname = options.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
26+
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
2727

2828
if (!hostname) {
29-
console.error("No hostname provided for Simple Analytics");
3029
return;
3130
}
3231

3332
const headers =
3433
"request" in options ? options.request.headers : options.headers;
3534

3635
if (isDoNotTrackEnabled(headers) && !options.collectDnt) {
37-
console.log("Do not track enabled, not tracking event");
3836
return;
3937
}
4038

@@ -46,14 +44,13 @@ export async function trackEvent(
4644
...parseHeaders(headers, options.ignoreMetrics),
4745
};
4846

49-
if (isBuildTime()) {
47+
if (isBuildTime() || !isProduction()) {
5048
return;
5149
}
5250

53-
if (!isProduction()) {
54-
console.log(
55-
"Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment",
56-
);
51+
// Only show a warning in production.
52+
if (!hostname) {
53+
console.log("No hostname provided for Simple Analytics, event not tracked");
5754
return;
5855
}
5956

@@ -86,10 +83,9 @@ const PROXY_PATHS = /^\/(proxy\.js|auto-events\.js|simple\/.*)$/;
8683
type TrackPageviewOptions = TrackingOptions & ServerContext;
8784

8885
export async function trackPageview(options: TrackPageviewOptions) {
89-
const hostname = options.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
86+
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
9087

9188
if (!hostname) {
92-
console.error("No hostname provided for Simple Analytics");
9389
return;
9490
}
9591

@@ -109,7 +105,6 @@ export async function trackPageview(options: TrackPageviewOptions) {
109105
}
110106

111107
if (isDoNotTrackEnabled(headers) && !options.collectDnt) {
112-
console.log("Do not track enabled, not tracking pageview");
113108
return;
114109
}
115110

@@ -130,13 +125,14 @@ export async function trackPageview(options: TrackPageviewOptions) {
130125
: {}),
131126
};
132127

133-
if (isBuildTime()) {
128+
if (isBuildTime() || !isProduction()) {
134129
return;
135130
}
136131

137-
if (!isProduction()) {
132+
// Only show a warning in production.
133+
if (!hostname) {
138134
console.log(
139-
"Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment",
135+
"No hostname provided for Simple Analytics, pageview not tracked",
140136
);
141137
return;
142138
}

0 commit comments

Comments
 (0)