import NewsletterSignup from '../../components/NewsletterSignup.astro';

I love using Umami Analytics — it's privacy-friendly, fast, and self-hostable. But I found myself repeatedly wiring up the same event tracking logic for things like scroll depth, time on page, and click tracking.

So I built umami-kit: a drop-in JavaScript utility that adds rich, configurable analytics to any Umami-powered site — with minimal effort and no cookies.

<aside className="my-6 rounded-lg border border-blue-200 bg-blue-50 p-5 dark:border-blue-800 dark:bg-blue-950/40">
  <p className="mb-2 font-semibold text-blue-900 dark:text-blue-100">🔄 Taking Umami analytics further with AI</p>
  <p className="text-blue-800 dark:text-blue-200">
    If you're looking to go beyond tracking and start chatting with your analytics data, check out{' '}
    <a href="/blog/chat-with-your-analytics-attribution-mcp/" className="font-semibold underline hover:text-blue-600 dark:hover:text-blue-300">Chat with Your Analytics: attribution-mcp Evolution</a>{' '}
    — a multi-platform analytics collection connecting Umami, YouTube, Bluesky, Mastodon, LinkedIn, Instagram, and Google Search Console to your LLM.
  </p>
</aside>

<NewsletterSignup ctaName="blog-post-bottom" ctaName="blog-post-bottom" title="Want More Privacy-First Dev Solutions?" description="I share practical tutorials on self-hosted tools, privacy-focused development, and building better web experiences. Join developers who get actionable insights weekly—no spam, just real solutions."/>

## 🚀 What It Tracks Automatically

Once added to your site, umami-kit can track:
- ✅ Scroll depth (customizable thresholds)
- ✅ Time on page with heartbeat & idle detection
- ✅ Clicks (either explicitly marked or all clickable elements)
- ✅ Element visibility (track when key content enters viewport)
- ✅ External link clicks
- ✅ Page exit stats (total time, max scroll)
- ✅ And more, including forms, downloads, searches, and ecommerce events via the programmatic API

## 🛠 How to Use It

Drop-in setup using HTML attributes:
```js
<!-- Include your Umami instance script -->
<script async src="https://analytics.yoursite.com/script.js" data-website-id="your-site-id"></script>

<!-- Load umami-kit -->
<script defer src="/path/to/umami-kit.js"></script>

<!-- Auto-track with config -->
<body 
  data-umami-auto-track
  data-umami-auto-track-all-clicks="true"
  data-umami-scroll-thresholds="25,50,75,90"
  data-umami-heartbeat="30000"
  data-umami-debug="true">
```

Or initialize manually for full control:
```js
const kit = new UmamiKit({
  autoTrackAllClicks: true,
  scrollDepthThresholds: [25, 50, 75, 90],
  heartbeatInterval: 30000,
  debug: true
});
```

You can also track visibility and clicks declaratively:
```
<!-- Track CTA button -->
<button data-umami-track="subscribe-click">Subscribe</button>

<!-- Track when element becomes visible -->
<section data-umami-visible="pricing-visible"> ... </section>
```

## 🧪 How I Use It

I self-host Umami on Vercel, using a custom subdomain per site for clean, first-party analytics. umami-kit builds on that by collecting richer behavioral data without any cookies, third-party requests, or PII.

This lets me understand how people engage with my sites while maintaining full respect for user privacy — something Google Analytics has never made easy.

## 🔗 Check it out on GitHub:
https://github.com/rhelmer/umami-kit

💡 Let me know what you think — feedback, PRs, and suggestions are welcome.