BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/atroui/analytics

Analytics

atroui/analytics
FreeUtility

trackEvent and experiment helpers.

Install it

npx shadcn@latest add https://atroui.com/r/analytics.json

Source

registry/default/lib/analytics.tsatroui.com/r/analytics.json
1/** Lightweight analytics + A/B testing - no external SDK required. */
2
3export type AnalyticsEvent = {
4 name: string;
5 properties?: Record<string, string | number | boolean>;
6};
7
8export type Experiment = {
9 id: string;
10 variants: string[];
11 weights?: number[];
12};
13
14const STORAGE_PREFIX = "ms_";
15const EXPERIMENT_PREFIX = `${STORAGE_PREFIX}exp_`;
16
17function isBrowser(): boolean {
18 return typeof window !== "undefined";
19}
20
21/** Assign and persist a variant for an experiment (sticky per user). */
22export function getExperimentVariant(experiment: Experiment): string {
23 if (!isBrowser()) return experiment.variants[0] ?? "";
24
25 const key = `${EXPERIMENT_PREFIX}${experiment.id}`;
26 const stored = localStorage.getItem(key);
27 if (stored && experiment.variants.includes(stored)) return stored;
28
29 const weights = experiment.weights ?? experiment.variants.map(() => 1);
30 const total = weights.reduce((a, b) => a + b, 0);
31 let roll = Math.random() * total;
32 let variant = experiment.variants[0] ?? "";
33 for (let i = 0; i < experiment.variants.length; i++) {
34 roll -= weights[i] ?? 0;
35 if (roll <= 0) {
36 variant = experiment.variants[i] ?? variant;
37 break;
38 }
39 }
40
41 localStorage.setItem(key, variant);
42 trackEvent("experiment_assigned", {
43 experiment_id: experiment.id,
44 variant,
45 });
46 return variant;
47}
48
49/** Track an event - forwards to Plausible/GA if configured, always logs in dev. */
50export function trackEvent(
51 name: string,
52 properties?: Record<string, string | number | boolean>
53): void {
54 if (!isBrowser()) return;
55
56 const payload: AnalyticsEvent = { name, properties };
57
58 if (process.env.NODE_ENV === "development") {
59 console.debug("[analytics]", payload);
60 }
61
62 // Plausible custom events
63 const plausible = (
64 window as Window & { plausible?: (e: string, o?: { props: Record<string, string | number | boolean> }) => void }
65 ).plausible;
66 if (plausible) {
67 plausible(name, properties ? { props: properties } : undefined);
68 }
69
70 // Google Analytics 4
71 const gtag = (
72 window as Window & { gtag?: (...args: unknown[]) => void }
73 ).gtag;
74 if (gtag) {
75 gtag("event", name, properties);
76 }
77
78 // Local event buffer for debugging
79 try {
80 const key = `${STORAGE_PREFIX}events`;
81 const existing = JSON.parse(localStorage.getItem(key) ?? "[]") as AnalyticsEvent[];
82 existing.push({ ...payload, properties: { ...properties, ts: Date.now() } as Record<string, string | number | boolean> });
83// … truncated

Files it writes

  • registry/default/lib/analytics.ts

Facts

Registry
atroui
Type
registry:lib
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-11
Last confirmed
today

Go to the source

atroui.com atroui/atroui on GitHub Raw registry item This item as JSON

More from atroui

All 81 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
BrandbrandatrouiUtilitiesFreeno deps
SEO helpersseoatrouiUtilitiesFreeno deps
Site URLsite-urlatrouiUtilitiesFreeno deps
UtilsutilsatrouiUtilitiesFree2 deps
BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex