UNPKG

@vutolabs/analytics

Version:

Vuto Analytics - instant, Stripe-native analytics for SaaS apps built with Next.js or React.

149 lines (140 loc) 4.18 kB
"use client"; // src/nextjs/index.tsx import React, { Suspense } from "react"; // src/react/index.tsx import { useEffect } from "react"; // package.json var name = "@vutolabs/analytics"; var version = "0.0.0-alpha.4"; // src/queue.ts var initQueue = () => { if (window.va) return; window.va = function a(...params) { (window.vaq = window.vaq || []).push(params); }; }; // src/utils.ts function isBrowser() { return typeof window !== "undefined"; } function detectEnvironment() { try { const env = process.env.NODE_ENV; if (env === "development" || env === "test") { return "development"; } } catch (e) { } return "production"; } function setMode(mode = "auto") { if (mode === "auto") { window.vam = detectEnvironment(); return; } window.vam = mode; } function getMode() { const mode = isBrowser() ? window.vam : detectEnvironment(); return mode || "production"; } function isDevelopment() { return getMode() === "development"; } function computeRoute(pathname, pathParams) { if (!pathname || !pathParams) { return pathname; } let result = pathname; try { const entries = Object.entries(pathParams); for (const [key, value] of entries) { if (!Array.isArray(value)) { const matcher = turnValueToRegExp(value); if (matcher.test(result)) { result = result.replace(matcher, `/[${key}]`); } } } for (const [key, value] of entries) { if (Array.isArray(value)) { const matcher = turnValueToRegExp(value.join("/")); if (matcher.test(result)) { result = result.replace(matcher, `/[...${key}]`); } } } return result; } catch (e) { return pathname; } } function turnValueToRegExp(value) { return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`); } function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function getScriptSrc(props) { if (isDevelopment()) { return "http://localhost:3000/analytics/script.js"; } return "https://vuto-backend-production.up.railway.app/analytics/script.js"; } // src/generic.ts function inject(props) { if (!isBrowser()) return; setMode(props.mode); initQueue(); const src = getScriptSrc(props); if (document.head.querySelector(`script[src*="${src}"]`)) return; const script = document.createElement("script"); script.src = src; script.defer = true; script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : ""); script.dataset.sdkv = version; const scriptOrigin = new URL(src).origin; script.dataset.endpoint = `${scriptOrigin}/api/analytics`; script.dataset.projectId = props.projectId; script.onerror = () => { const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Please check if the project is enabled in the Vuto Web Analytics dashboard."; console.log(`[Vuto Web Analytics] Failed to load script from ${src}. ${errorMessage}`); }; document.head.appendChild(script); } // src/react/index.tsx function Analytics(props) { const config = { framework: props.framework || "react", mode: props.mode, projectId: props.projectId }; useEffect(() => { inject(config); }, []); return null; } // src/nextjs/utils.ts import { useParams, usePathname, useSearchParams } from "next/navigation"; var useRoute = () => { const params = useParams(); const searchParams = useSearchParams(); const path = usePathname(); if (!params) { return { route: null, path }; } const finalParams = Object.keys(params).length ? params : Object.fromEntries(searchParams.entries()); return { route: computeRoute(path, finalParams), path }; }; // src/nextjs/index.tsx function AnalyticsComponent(props) { const { route, path } = useRoute(); return /* @__PURE__ */ React.createElement(Analytics, { path, route, ...props, framework: "next" }); } function Analytics2(props) { return /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(AnalyticsComponent, { ...props })); } export { Analytics2 as Analytics }; //# sourceMappingURL=index.mjs.map