π Open source, privacy-first web analytics β a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
htmx integration
Blue Mouse58b6b206e8a0
diff --git a/docs/docs/integration-guides.md b/docs/docs/integration-guides.mdindex 868b08a3d..4c77ac07b 100644--- a/docs/docs/integration-guides.md+++ b/docs/docs/integration-guides.md@@ -37,6 +37,7 @@ Swetrix integrates with a wide range of platforms, frameworks, and tools. Choose- [Docusaurus](/docusaurus-integration)- [Eleventy (11ty)](/eleventy-integration)- [Gatsby](/gatsby-integration)+- [htmx](/htmx-integration)- [Hugo](/hugo-integration)- [Jekyll](/jekyll-integration)- [Next.js](/nextjs-integration)diff --git a/docs/docs/integrations/htmx.md b/docs/docs/integrations/htmx.mdnew file mode 100644index 000000000..c49f17aec--- /dev/null+++ b/docs/docs/integrations/htmx.md@@ -0,0 +1,196 @@+---+title: htmx+slug: /htmx-integration+---++Integrate Swetrix with your [htmx](https://htmx.org/) application to track page views, monitor errors, and capture custom events β all while staying privacy-friendly and GDPR-compliant.++htmx enhances server-rendered HTML with AJAX-powered page transitions. Even though htmx swaps content without full page reloads, Swetrix's `trackViews()` automatically detects URL changes (including those made by `hx-boost` and `hx-push-url`), so no extra wiring is needed for page view tracking.++## Installation++Add the Swetrix tracking script to your base HTML layout β the template that wraps every page.++Place the following just before the closing `</body>` tag in your base template:++```html+<script src="https://swetrix.org/swetrix.js" defer></script>+<script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ })+</script>++<noscript>+ <img+ src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"+ alt=""+ referrerpolicy="no-referrer-when-downgrade"+ />+</noscript>+```++:::caution+Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboard](https://swetrix.com/projects), otherwise tracking won't work.+:::++That's it. `trackViews()` polls for path changes automatically, so htmx navigations that update the URL are tracked without any additional code.++### Full example++Here's a complete base layout with htmx and Swetrix:++```html+<!doctype html>+<html lang="en">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <title>My App</title>+ <script src="https://unpkg.com/htmx.org"></script>+ </head>+ <body hx-boost="true">+ <nav>+ <a href="/">Home</a>+ <a href="/about">About</a>+ </nav>++ <main><!-- page content --></main>++ <script src="https://swetrix.org/swetrix.js" defer></script>+ <script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ })+ </script>++ <noscript>+ <img+ src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"+ alt=""+ referrerpolicy="no-referrer-when-downgrade"+ />+ </noscript>+ </body>+</html>+```++## Server framework examples++htmx is backend-agnostic. Below are snippets showing where to place the tracking code in popular server frameworks.++### Django++Add the scripts to your base template (e.g. `templates/base.html`):++```html+{% if not debug %}+<script src="https://swetrix.org/swetrix.js" defer></script>+<script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ })+</script>+{% endif %}+```++### Flask / Jinja2++```html+{% if not config.DEBUG %}+<script src="https://swetrix.org/swetrix.js" defer></script>+<script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ })+</script>+{% endif %}+```++### Go (html/template)++```html+{{ if eq .Env "production" }}+<script src="https://swetrix.org/swetrix.js" defer></script>+<script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ })+</script>+{{ end }}+```++## Error tracking++Enable automatic client-side error monitoring by adding `trackErrors()` alongside your existing initialisation:++```html+<script>+ document.addEventListener('DOMContentLoaded', function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ swetrix.trackErrors()+ })+</script>+```++Errors will appear in the **Errors** tab of your project dashboard. See the [tracking script reference](/swetrix-js-reference#trackerrors) for options like `sampleRate` and `callback`.++## Tracking custom events++Custom events let you track specific user interactions β button clicks, form submissions, downloads, and more.++### Inline event tracking++```html+<button onclick="swetrix.track({ ev: 'SIGNUP_CLICK' })">+ Sign up+</button>+```++### Tracking htmx-triggered actions++You can hook into htmx events to track actions like form submissions that happen over AJAX:++```html+<form hx-post="/subscribe" hx-target="#result">+ <input type="email" name="email" placeholder="[email protected]" required />+ <button type="submit">Subscribe</button>+</form>+<div id="result"></div>++<script>+ document.querySelector('form[hx-post="/subscribe"]')+ .addEventListener('htmx:afterRequest', function (event) {+ if (event.detail.successful && typeof swetrix !== 'undefined') {+ swetrix.track({+ ev: 'NEWSLETTER_SIGNUP',+ meta: { source: 'footer' },+ })+ }+ })+</script>+```++### Event naming rules++Event names must:++- Contain only English letters (a-Z), numbers (0-9), underscores (`_`), and dots (`.`)+- Be fewer than 64 characters+- Start with an English letter++## Check your installation++Deploy your application and visit a few pages. If you're using `hx-boost`, click some links to trigger htmx navigations. Within a minute you should see both the initial page view and the htmx-driven page views appearing in your Swetrix dashboard.++## Further reading++- [Tracking script reference](/swetrix-js-reference) β full API documentation for `init()`, `track()`, `trackViews()`, `trackErrors()`, and more.+- [htmx documentation](https://htmx.org/docs/) β official htmx docs covering attributes, events, and extensions.+- [htmx events reference](https://htmx.org/events/) β complete list of htmx events you can hook into.diff --git a/docs/sidebars.js b/docs/sidebars.jsindex 755781fba..399882417 100644--- a/docs/sidebars.js+++ b/docs/sidebars.js@@ -23,6 +23,7 @@ module.exports = {'integrations/gitbook','integrations/google-search-console','integrations/google-tag-manager',+ 'integrations/htmx','integrations/hugo','integrations/jekyll','integrations/joomla',