Skip to content

πŸ“ˆ 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!

docs: remove redundant trackViews() calls from integration guides

Blue Mousea8d17ab872bc
diff --git a/docs/docs/integrations/angular.md b/docs/docs/integrations/angular.md
index 674bea2e5..9fbbb8570 100644
--- a/docs/docs/integrations/angular.md
+++ b/docs/docs/integrations/angular.md
@@ -41,61 +41,9 @@ Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboar
### With Angular Router
-Most Angular apps use the built-in [Router](https://angular.dev/guide/routing) for client-side navigation. Since route changes don't trigger full page loads, you need to notify Swetrix about each navigation so page views are tracked correctly.
+`trackViews()` automatically detects client-side route changes (including Angular Router navigations), so you only need to call it once in your root component β€” no router subscription needed.
-The recommended approach is to create a dedicated service.
-
-**1. Create an analytics service**
-
-Create `src/app/services/analytics.service.ts`:
-
-```typescript
-import { Injectable } from '@angular/core'
-import { NavigationEnd, Router } from '@angular/router'
-import { filter } from 'rxjs/operators'
-import * as Swetrix from 'swetrix'
-
-@Injectable({ providedIn: 'root' })
-export class AnalyticsService {
- constructor(private router: Router) {}
-
- init() {
- Swetrix.init('YOUR_PROJECT_ID')
- Swetrix.trackViews()
-
- this.router.events
- .pipe(filter((event) => event instanceof NavigationEnd))
- .subscribe(() => {
- Swetrix.trackViews()
- })
- }
-}
-```
-
-**2. Call it from your root component**
-
-Open `src/app/app.component.ts`:
-
-```typescript
-import { Component, OnInit } from '@angular/core'
-import { AnalyticsService } from './services/analytics.service'
-
-@Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
-})
-export class AppComponent implements OnInit {
- constructor(private analytics: AnalyticsService) {}
-
- ngOnInit() {
- this.analytics.init()
- }
-}
-```
-
-:::tip
-If you're using **standalone components** (Angular 14+), the same pattern works β€” just make sure `AnalyticsService` is provided at the application level via `providedIn: 'root'`.
-:::
+The same `AppComponent` setup from above works for apps with routing.
### Noscript fallback (optional)
@@ -168,21 +116,15 @@ Build and deploy your application (or temporarily enable `devMode`) and visit a
## Error tracking
-Enable automatic client-side error monitoring by adding `trackErrors()` to your analytics service. This captures unhandled JavaScript errors and reports them to Swetrix.
+Enable automatic client-side error monitoring by adding `trackErrors()` to your initialisation. This captures unhandled JavaScript errors and reports them to Swetrix.
-Update your `AnalyticsService`:
+Update your `AppComponent`:
```typescript
-init() {
+ngOnInit() {
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
Swetrix.trackErrors()
-
- this.router.events
- .pipe(filter((event) => event instanceof NavigationEnd))
- .subscribe(() => {
- Swetrix.trackViews()
- })
}
```
diff --git a/docs/docs/integrations/astro.md b/docs/docs/integrations/astro.md
index 0e6df6a49..3c28e67ac 100644
--- a/docs/docs/integrations/astro.md
+++ b/docs/docs/integrations/astro.md
@@ -139,44 +139,7 @@ Astro processes `<script>` tags β€” they're bundled and deduplicated automatical
## View Transitions support
-If you're using [Astro's View Transitions](https://docs.astro.build/en/guides/view-transitions/) for client-side navigation, page changes won't trigger full page loads. You need to tell Swetrix about these navigations so page views are tracked correctly.
-
-### CDN script approach
-
-Update your layout's inline script to listen for the `astro:after-swap` event:
-
-```html
-<script src="https://swetrix.org/swetrix.js" defer></script>
-<script>
- document.addEventListener('DOMContentLoaded', function () {
- swetrix.init('YOUR_PROJECT_ID')
- swetrix.trackViews()
- })
-
- document.addEventListener('astro:after-swap', function () {
- swetrix.trackViews()
- })
-</script>
-```
-
-### npm package approach
-
-Update your analytics script:
-
-```typescript
-import * as Swetrix from 'swetrix'
-
-Swetrix.init('YOUR_PROJECT_ID')
-Swetrix.trackViews()
-
-document.addEventListener('astro:after-swap', () => {
- Swetrix.trackViews()
-})
-```
-
-:::tip
-If you're **not** using View Transitions, you don't need the `astro:after-swap` listener β€” standard MPA navigation handles page view tracking automatically.
-:::
+If you're using [Astro's View Transitions](https://docs.astro.build/en/guides/view-transitions/) for client-side navigation, no extra configuration is needed. Swetrix's `trackViews()` automatically detects route changes via the History API, so View Transition navigations are tracked out of the box.
### Noscript fallback (optional)
diff --git a/docs/docs/integrations/nextjs.md b/docs/docs/integrations/nextjs.md
index a85460072..e5b67226a 100644
--- a/docs/docs/integrations/nextjs.md
+++ b/docs/docs/integrations/nextjs.md
@@ -17,7 +17,7 @@ npm install swetrix
### App Router (recommended)
-Create a client component that initialises Swetrix and tracks page views across route changes.
+Create a client component that initialises Swetrix. `trackViews()` automatically detects client-side route changes, so you only need to call it once.
**1. Create the analytics component**
@@ -27,32 +27,23 @@ Create a new file at `src/components/Analytics.tsx` (or `src/components/Analytic
'use client'
import { useEffect } from 'react'
-import { usePathname, useSearchParams } from 'next/navigation'
import * as Swetrix from 'swetrix'
export default function Analytics() {
- const pathname = usePathname()
- const searchParams = useSearchParams()
-
useEffect(() => {
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
}, [])
- useEffect(() => {
- Swetrix.trackViews()
- }, [pathname, searchParams])
-
return null
}
```
**2. Add it to your root layout**
-Open `src/app/layout.tsx` and render the `Analytics` component. Wrap it in `<Suspense>` because `useSearchParams()` requires it:
+Open `src/app/layout.tsx` and render the `Analytics` component:
```tsx
-import { Suspense } from 'react'
import Analytics from '@/components/Analytics'
export default function RootLayout({
@@ -64,9 +55,7 @@ export default function RootLayout({
<html lang="en">
<body>
{children}
- <Suspense fallback={null}>
- <Analytics />
- </Suspense>
+ <Analytics />
</body>
</html>
)
@@ -79,31 +68,18 @@ Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboar
### Pages Router
-If you're using the Pages Router, add Swetrix to your `_app.tsx` (or `_app.jsx`) file and listen for route changes via Next.js's router events.
+If you're using the Pages Router, add Swetrix to your `_app.tsx` (or `_app.jsx`) file. `trackViews()` automatically detects route changes, so a single call is all you need.
```tsx
import { useEffect } from 'react'
-import { useRouter } from 'next/router'
import type { AppProps } from 'next/app'
import * as Swetrix from 'swetrix'
export default function App({ Component, pageProps }: AppProps) {
- const router = useRouter()
-
useEffect(() => {
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
-
- const handleRouteChange = () => {
- Swetrix.trackViews()
- }
-
- router.events.on('routeChangeComplete', handleRouteChange)
-
- return () => {
- router.events.off('routeChangeComplete', handleRouteChange)
- }
- }, [router.events])
+ }, [])
return <Component {...pageProps} />
}
@@ -197,9 +173,7 @@ useEffect(() => {
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
Swetrix.trackErrors()
-
- // ...router event handlers
-}, [router.events])
+}, [])
```
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`.
diff --git a/docs/docs/integrations/nuxt.md b/docs/docs/integrations/nuxt.md
index e886cb9f1..e06b8461f 100644
--- a/docs/docs/integrations/nuxt.md
+++ b/docs/docs/integrations/nuxt.md
@@ -23,18 +23,12 @@ Nuxt plugins run once when your app initialises, making them the ideal place to
import * as Swetrix from 'swetrix'
export default defineNuxtPlugin(() => {
- const router = useRouter()
-
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
-
- router.afterEach(() => {
- Swetrix.trackViews()
- })
})
```
-That's it β€” Nuxt auto-registers files in the `plugins/` directory. Page views are now tracked on every route change.
+That's it β€” Nuxt auto-registers files in the `plugins/` directory. `trackViews()` automatically detects client-side route changes, so page views are tracked on every navigation.
:::caution
Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboard](https://swetrix.com/projects), otherwise tracking won't work.
@@ -70,17 +64,12 @@ Update `plugins/swetrix.client.ts`:
import * as Swetrix from 'swetrix'
export default defineNuxtPlugin(() => {
- const router = useRouter()
const config = useRuntimeConfig()
Swetrix.init(config.public.swetrixPid, {
disabled: process.dev,
})
Swetrix.trackViews()
-
- router.afterEach(() => {
- Swetrix.trackViews()
- })
})
```
@@ -110,15 +99,9 @@ Update `plugins/swetrix.client.ts`:
import * as Swetrix from 'swetrix'
export default defineNuxtPlugin(() => {
- const router = useRouter()
-
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
Swetrix.trackErrors()
-
- router.afterEach(() => {
- Swetrix.trackViews()
- })
})
```
diff --git a/docs/docs/integrations/react.md b/docs/docs/integrations/react.md
index 28941ee28..9dc5b2d25 100644
--- a/docs/docs/integrations/react.md
+++ b/docs/docs/integrations/react.md
@@ -43,55 +43,7 @@ Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboar
### With React Router
-Most React SPAs use [React Router](https://reactrouter.com/) for client-side navigation. Since page transitions don't trigger full page loads, you need to call `trackViews()` on every route change.
-
-**1. Create an analytics component**
-
-Create `src/components/Analytics.tsx`:
-
-```tsx
-import { useEffect } from 'react'
-import { useLocation } from 'react-router-dom'
-import * as Swetrix from 'swetrix'
-
-export default function Analytics() {
- const location = useLocation()
-
- useEffect(() => {
- Swetrix.init('YOUR_PROJECT_ID')
- Swetrix.trackViews()
- }, [])
-
- useEffect(() => {
- Swetrix.trackViews()
- }, [location])
-
- return null
-}
-```
-
-**2. Add it inside your Router**
-
-The `Analytics` component must be rendered inside the `<BrowserRouter>` (or whichever router you use) so that `useLocation()` has access to the routing context:
-
-```tsx
-import { BrowserRouter, Routes, Route } from 'react-router-dom'
-import Analytics from './components/Analytics'
-import Home from './pages/Home'
-import About from './pages/About'
-
-export default function App() {
- return (
- <BrowserRouter>
- <Analytics />
- <Routes>
- <Route path="/" element={<Home />} />
- <Route path="/about" element={<About />} />
- </Routes>
- </BrowserRouter>
- )
-}
-```
+`trackViews()` automatically detects client-side route changes (including React Router navigations), so you only need to call it once. Initialise Swetrix in your entry point the same way as above β€” no route-change listener needed.
### Noscript fallback (optional)
diff --git a/docs/docs/integrations/remix.md b/docs/docs/integrations/remix.md
index 4c6804c7b..f76512adf 100644
--- a/docs/docs/integrations/remix.md
+++ b/docs/docs/integrations/remix.md
@@ -15,7 +15,7 @@ npm install swetrix
### Set up the analytics component
-Since Swetrix runs in the browser, you need a client-only component that initialises tracking and responds to route changes. Remix uses React Router under the hood, so you can use `useLocation()` to detect navigations.
+Since Swetrix runs in the browser, you need a client-only component that initialises tracking. `trackViews()` automatically detects client-side route changes, so you only need to call it once.
**1. Create the analytics component**
@@ -23,21 +23,14 @@ Create `app/components/Analytics.tsx`:
```tsx
import { useEffect } from 'react'
-import { useLocation } from '@remix-run/react'
import * as Swetrix from 'swetrix'
export default function Analytics() {
- const location = useLocation()
-
useEffect(() => {
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
}, [])
- useEffect(() => {
- Swetrix.trackViews()
- }, [location])
-
return null
}
```
diff --git a/docs/docs/integrations/ruby-on-rails.md b/docs/docs/integrations/ruby-on-rails.md
index 887b52c67..fc7397518 100644
--- a/docs/docs/integrations/ruby-on-rails.md
+++ b/docs/docs/integrations/ruby-on-rails.md
@@ -100,24 +100,7 @@ Remember to remove this before deploying.
Rails 7 and later ship with [Turbo](https://turbo.hotwired.dev/) by default. Turbo intercepts link clicks and form submissions, performing navigations without full page reloads. This means the `DOMContentLoaded` event only fires once, on the initial page load.
-Swetrix's `trackViews()` listens for History API changes, so most Turbo Drive navigations are tracked automatically. If you notice any gaps, you can explicitly listen for Turbo's navigation event:
-
-```html
-<script>
- document.addEventListener('DOMContentLoaded', function () {
- swetrix.init('YOUR_PROJECT_ID')
- swetrix.trackViews()
- })
-
- document.addEventListener('turbo:load', function () {
- swetrix.trackViews()
- })
-</script>
-```
-
-:::tip
-If your app does **not** use Turbo (e.g. you removed it or are on Rails 6 or earlier), you don't need the `turbo:load` listener β€” standard full-page navigation handles page view tracking automatically.
-:::
+Swetrix's `trackViews()` automatically detects History API changes, so Turbo Drive navigations are tracked out of the box β€” no extra event listeners needed.
## Store your Project ID in credentials (optional)
diff --git a/docs/docs/integrations/sveltekit.md b/docs/docs/integrations/sveltekit.md
index 22a2cada3..84e81c04f 100644
--- a/docs/docs/integrations/sveltekit.md
+++ b/docs/docs/integrations/sveltekit.md
@@ -14,25 +14,21 @@ After you sign up on Swetrix and create a new project, the only thing left is to
```html
<script>
import { onMount } from 'svelte'
- import { browser, dev } from '$app/environment'
- import { page } from '$app/stores'
+ import { dev } from '$app/environment'
import * as Swetrix from 'swetrix'
onMount(() => {
Swetrix.init('YOUR_PROJECT_ID', { devMode: dev, disabled: dev })
Swetrix.trackViews()
})
-
- // Trigger a pageview everytime the user navigates.
- // We use the `browser` env variable to ensure that we don't try to
- // call trackViews while the component is doing SSR.
- $: ($page.url.pathname, browser && Swetrix.trackViews())
</script>
<!-- Your site's content gets injected here -->
<slot />
```
+`trackViews()` automatically detects client-side route changes, so you only need to call it once.
+
:::caution
It's very important not to forget to replace `YOUR_PROJECT_ID` with your actual Project ID you can find in the Dashboard, otherwise tracking won't work!
:::
diff --git a/docs/docs/integrations/vitepress.md b/docs/docs/integrations/vitepress.md
index f8b1c5d5e..8b5dab743 100644
--- a/docs/docs/integrations/vitepress.md
+++ b/docs/docs/integrations/vitepress.md
@@ -54,25 +54,20 @@ Then extend your VitePress theme to initialise Swetrix. Create or edit `.vitepre
```ts
import DefaultTheme from 'vitepress/theme'
import type { Theme } from 'vitepress'
-import { watch } from 'vue'
import * as Swetrix from 'swetrix'
export default {
extends: DefaultTheme,
- enhanceApp({ router }) {
+ enhanceApp() {
if (typeof window === 'undefined') return
Swetrix.init('YOUR_PROJECT_ID')
Swetrix.trackViews()
-
- watch(() => router.route.path, () => {
- Swetrix.trackViews()
- })
},
} satisfies Theme
```
-This initialises Swetrix once and re-tracks page views whenever VitePress navigates to a new route.
+`trackViews()` automatically detects client-side route changes, so you only need to call it once.
### Noscript fallback (optional)
@@ -93,9 +88,7 @@ export default defineConfig({
## SPA navigation
-VitePress uses client-side routing β€” clicking a link doesn't trigger a full page reload. The Swetrix tracking script detects these navigations automatically via the History API, so **no extra configuration is needed** if you're using Option A (script tag).
-
-If you're using Option B (npm package), the `watch` on `router.route.path` in the theme setup handles this for you.
+VitePress uses client-side routing β€” clicking a link doesn't trigger a full page reload. Swetrix's `trackViews()` detects these navigations automatically via the History API, so **no extra configuration is needed** regardless of which option you use.
## Disable tracking in development
diff --git a/docs/docs/integrations/vue.md b/docs/docs/integrations/vue.md
index c3b9fab0d..79ffec280 100644
--- a/docs/docs/integrations/vue.md
+++ b/docs/docs/integrations/vue.md
@@ -38,44 +38,7 @@ Replace `YOUR_PROJECT_ID` with your actual Project ID from the [Swetrix dashboar
### With Vue Router
-Most Vue SPAs use [Vue Router](https://router.vuejs.org/) for client-side navigation. Since page transitions don't trigger full page loads, you need to call `trackViews()` on every route change.
-
-**1. Create a router plugin**
-
-Create `src/plugins/swetrix.ts`:
-
-```ts
-import type { App } from 'vue'
-import type { Router } from 'vue-router'
-import * as Swetrix from 'swetrix'
-
-export function swetrixPlugin(app: App, router: Router) {
- Swetrix.init('YOUR_PROJECT_ID')
- Swetrix.trackViews()
-
- router.afterEach(() => {
- Swetrix.trackViews()
- })
-}
-```
-
-**2. Register it in your entry point**
-
-Update `src/main.ts`:
-
-```ts
-import { createApp } from 'vue'
-import App from './App.vue'
-import router from './router'
-import { swetrixPlugin } from './plugins/swetrix'
-
-const app = createApp(App)
-
-app.use(router)
-swetrixPlugin(app, router)
-
-app.mount('#app')
-```
+`trackViews()` automatically detects client-side route changes (including Vue Router navigations), so you only need to call it once. The same entry point setup from above works β€” no router hook needed.
### Noscript fallback (optional)