UNPKG

@vutolabs/analytics

Version:

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

152 lines (146 loc) 3.92 kB
"use client"; // 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 isProduction() { return getMode() === "production"; } function isDevelopment() { return getMode() === "development"; } function removeKey(key, { [key]: _, ...rest }) { return rest; } function parseProperties(properties, options) { if (!properties) return void 0; let props = properties; const errorProperties = []; for (const [key, value] of Object.entries(properties)) { if (typeof value === "object" && value !== null) { if (options.strip) { props = removeKey(key, props); } else { errorProperties.push(key); } } } if (errorProperties.length > 0 && !options.strip) { throw Error( `The following properties are not valid: ${errorProperties.join( ", " )}. Only strings, numbers, booleans, and null are allowed.` ); } return props; } 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); } function track(name2, properties, options) { var _a, _b; if (!isBrowser()) { const msg = "[Vuto Web Analytics] Please import `track` from `@vutolabs/analytics/server` when using this function in a server environment"; if (isProduction()) { console.warn(msg); } else { throw new Error(msg); } return; } if (!properties) { (_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2, options }); return; } try { const props = parseProperties(properties, { strip: isProduction() }); (_b = window.va) == null ? void 0 : _b.call(window, "event", { name: name2, data: props, options }); } catch (err) { if (err instanceof Error && isDevelopment()) { console.error(err); } } } // src/react/index.tsx function Analytics(props) { const config = { framework: props.framework || "react", mode: props.mode, projectId: props.projectId }; useEffect(() => { inject(config); }, []); return null; } export { Analytics, track }; //# sourceMappingURL=index.mjs.map