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
6 changes: 3 additions & 3 deletions packages/analytics/src/client/simple-analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

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

return (
<Suspense fallback={null}>
<React.Suspense fallback={null}>
<Script {...dataProps} src="/proxy.js" />
</Suspense>
</React.Suspense>
);
};

Expand Down
40 changes: 9 additions & 31 deletions packages/analytics/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,28 @@ export function isProduction() {
return process.env.NEXT_PUBLIC_VERCEL_ENV === "production";
}

function isBuildTime() {
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD;
}

export function parseDataProps(settings?: SimpleAnalyticsProps) {
if (!process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME) {
console.error("No hostname provided for Simple Analytics");
return {};
}

if (isBuildTime()) {
return {};
}

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",
);
return {};
}

if (!settings) {
return {
"data-hostname": process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
};
}

const metrics = settings.ignoreMetrics
const metrics = settings?.ignoreMetrics
? Object.entries(settings.ignoreMetrics)
.filter(([_, value]) => value)
.map(([key]) => `${key}`)
.join(",")
: undefined;

return {
"data-auto-collect": settings.autoCollect,
"data-collect-dnt": settings.collectDnt,
"data-auto-collect": settings?.autoCollect,
"data-collect-dnt": settings?.collectDnt,
"data-hostname":
settings.hostname ?? process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
"data-mode": settings.mode,
settings?.hostname ?? process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,
"data-mode": settings?.mode,
"data-ignore-metrics": metrics === "" ? undefined : metrics,
"data-ignore-pages": settings.ignorePages?.join(","),
"data-allow-params": settings.allowParams?.join(","),
"data-non-unique-params": settings.nonUniqueParams?.join(","),
"data-strict-utm": settings.strictUtm,
"data-ignore-pages": settings?.ignorePages?.join(","),
"data-allow-params": settings?.allowParams?.join(","),
"data-non-unique-params": settings?.nonUniqueParams?.join(","),
"data-strict-utm": settings?.strictUtm,
};
}
5 changes: 5 additions & 0 deletions packages/analytics/src/plugin/with-simple-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export function withSimpleAnalytics(
): NextConfig {
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;

if (!hostname) {
console.warn("No hostname provided for Simple Analytics, plugin disabled.");
return nextConfig;
}

const clientHints = buildClientHintHeaders(options?.clientHints);

const nextAnalyticsConfig: NextConfig = {
Expand Down
24 changes: 10 additions & 14 deletions packages/analytics/src/server/simple-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ export async function trackEvent(
eventName: string,
options: TrackEventOptions,
) {
const hostname = options.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;

if (!hostname) {
console.error("No hostname provided for Simple Analytics");
return;
}

const headers =
"request" in options ? options.request.headers : options.headers;

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

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

if (isBuildTime()) {
if (isBuildTime() || !isProduction()) {
return;
}

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",
);
// Only show a warning in production.
if (!hostname) {
console.log("No hostname provided for Simple Analytics, event not tracked");
return;
}

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

export async function trackPageview(options: TrackPageviewOptions) {
const hostname = options.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
const hostname = options?.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;

if (!hostname) {
console.error("No hostname provided for Simple Analytics");
return;
}

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

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

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

if (isBuildTime()) {
if (isBuildTime() || !isProduction()) {
return;
}

if (!isProduction()) {
// Only show a warning in production.
if (!hostname) {
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",
"No hostname provided for Simple Analytics, pageview not tracked",
);
return;
}
Expand Down
Loading