π 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
solid.js integration
Blue Mouse5a7e192fcfc6
diff --git a/docs/docs/integration-guides.md b/docs/docs/integration-guides.mdindex c6d93b781..679037754 100644--- a/docs/docs/integration-guides.md+++ b/docs/docs/integration-guides.md@@ -43,6 +43,7 @@ Swetrix integrates with a wide range of platforms, frameworks, and tools. Choose- [Nuxt](/nuxt-integration)- [React (Vite / CRA)](/react-integration)- [Remix](/remix-integration)+- [SolidJS / SolidStart](/solidjs-integration)- [SvelteKit](/sveltekit-integration)- [VitePress](/vitepress-integration)- [Vue](/vue-integration)diff --git a/docs/docs/integrations/solidjs.md b/docs/docs/integrations/solidjs.mdnew file mode 100644index 000000000..5fe32774c--- /dev/null+++ b/docs/docs/integrations/solidjs.md@@ -0,0 +1,227 @@+---+title: SolidJS / SolidStart+slug: /solidjs-integration+---++Integrate Swetrix with your [SolidJS](https://www.solidjs.com/) or [SolidStart](https://start.solidjs.com/) application to track page views, monitor errors, and capture custom events β all while staying privacy-friendly and GDPR-compliant.++This guide covers both **SolidJS** SPAs (with Vite) and **SolidStart** apps with server-side rendering.++## Installation++Install the Swetrix npm package:++```bash+npm install swetrix+```++### SolidStart (recommended)++SolidStart is the official meta-framework for SolidJS with file-based routing and SSR support.++**1. Create the analytics component**++Create a new file at `src/components/Analytics.tsx`:++```tsx+import { onMount } from 'solid-js'+import * as Swetrix from 'swetrix'++export default function Analytics() {+ onMount(() => {+ Swetrix.init('YOUR_PROJECT_ID')+ Swetrix.trackViews()+ })++ return null+}+```++`trackViews()` automatically detects client-side route changes, so you only need to call it once.++**2. Add it to your root layout**++Open `src/app.tsx` and render the `Analytics` component inside the `<Router>`:++```tsx+import { Router } from '@solidjs/router'+import { FileRoutes } from '@solidjs/start/router'+import Analytics from '~/components/Analytics'++export default function App() {+ return (+ <Router root={(props) => (+ <>+ <Analytics />+ {props.children}+ </>+ )}>+ <FileRoutes />+ </Router>+ )+}+```++:::caution+Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboard](https://swetrix.com/projects), otherwise tracking won't work.+:::++### SolidJS with Vite (SPA)++For a standalone SolidJS SPA without SolidStart, initialise Swetrix in your entry point. `trackViews()` automatically detects client-side route changes (including `@solidjs/router`), so a single call is all you need.++Open `src/index.tsx`:++```tsx+import { render } from 'solid-js/web'+import * as Swetrix from 'swetrix'+import App from './App'++Swetrix.init('YOUR_PROJECT_ID')+Swetrix.trackViews()++render(() => <App />, document.getElementById('root')!)+```++### Noscript fallback (optional)++To track visitors who have JavaScript disabled, add a noscript image pixel to your `index.html`:++```html+<body>+ <div id="root"></div>++ <noscript>+ <img+ src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"+ alt=""+ referrerpolicy="no-referrer-when-downgrade"+ />+ </noscript>+</body>+```++## Disable tracking in development++By default, Swetrix ignores `localhost` traffic. You can also explicitly disable it based on your environment:++```tsx+Swetrix.init('YOUR_PROJECT_ID', {+ disabled: import.meta.env.DEV,+})+```++:::tip+If you want to verify tracking locally during development, set `devMode: true` instead:++```tsx+Swetrix.init('YOUR_PROJECT_ID', {+ devMode: true,+})+```++Remember to remove this before deploying to production.+:::++## Check your installation++Build and deploy your application (or temporarily enable `devMode`) and visit a few pages. Within a minute you should see new pageviews appearing in your Swetrix dashboard.++## Error tracking++Enable automatic client-side error monitoring by adding `trackErrors()` to your initialisation. This captures unhandled JavaScript errors and reports them to Swetrix.++Update your analytics component:++```tsx+onMount(() => {+ Swetrix.init('YOUR_PROJECT_ID')+ Swetrix.trackViews()+ Swetrix.trackErrors()+})+```++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, sign-ups, purchases, and more. Import `swetrix` in any component and call `track()`.++### Example: tracking button clicks++```tsx+import * as Swetrix from 'swetrix'++export default function SignUpButton() {+ const handleClick = () => {+ Swetrix.track({+ ev: 'SIGNUP_CTA_CLICK',+ meta: {+ location: 'hero',+ },+ })+ }++ return <button onClick={handleClick}>Sign up</button>+}+```++### Example: tracking form submissions++```tsx+import * as Swetrix from 'swetrix'++export default function ContactForm() {+ const handleSubmit = (e: SubmitEvent) => {+ e.preventDefault()++ Swetrix.track({+ ev: 'CONTACT_FORM_SUBMITTED',+ meta: {+ source: 'support_page',+ },+ })++ // ...submit logic+ }++ return (+ <form onSubmit={handleSubmit}>+ <input type="email" placeholder="[email protected]" required />+ <textarea placeholder="How can we help?" required />+ <button type="submit">Send</button>+ </form>+ )+}+```++### 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++We recommend `UPPER_SNAKE_CASE` for consistency (e.g. `SIGNUP_CTA_CLICK`, `CONTACT_FORM_SUBMITTED`).++## Using environment variables for your Project ID++Rather than hardcoding your Project ID, you can use an environment variable. Since both SolidJS (Vite) and SolidStart use Vite under the hood, prefix the variable with `VITE_`:++**1. Add to your `.env` file:**++```+VITE_SWETRIX_PID=YOUR_PROJECT_ID+```++**2. Reference it in your analytics component:**++```tsx+Swetrix.init(import.meta.env.VITE_SWETRIX_PID)+```++## Further reading++- [Tracking script reference](/swetrix-js-reference) β full API documentation for `init()`, `track()`, `trackViews()`, `trackErrors()`, and more.+- [Swetrix npm package](https://www.npmjs.com/package/swetrix) β package details and changelog.diff --git a/docs/sidebars.js b/docs/sidebars.jsindex b61f767fa..373325505 100644--- a/docs/sidebars.js+++ b/docs/sidebars.js@@ -45,6 +45,7 @@ module.exports = {},'integrations/ruby-on-rails','integrations/shopify',+ 'integrations/solidjs','integrations/squarespace','integrations/sveltekit','integrations/thrivecart',