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!

next.js proxy guide

Blue Mouse64d0fe30f40c
diff --git a/docs/adblockers/guides/nextjs.md b/docs/adblockers/guides/nextjs.md
new file mode 100644
index 000000000..cd70e7a0c
--- /dev/null
+++ b/docs/adblockers/guides/nextjs.md
@@ -0,0 +1,38 @@
+---
+title: How to proxy Swetrix with Next.js
+slug: /adblockers/guides/nextjs
+---
+
+You can use [Next.js](https://nextjs.org/) to proxy Swetrix analytics traffic via your domain.
+
+## Setup Next.js rewrites rules
+First, you need to add the following rules to the `next.config.js` file. If you don't already have it, you will need to [create it](https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites) in your project's root directory first.
+```javascript
+module.exports = {
+ async rewrites() {
+ return [
+ {
+ source: "/sproxy",
+ destination: "https://api.swetrix.com/log",
+ },
+ ];
+ },
+}
+```
+
+Instead of `/sproxy` route, you can use any other name. We recommend using a generic name or even just random letters to avoid being blocked by adblockers in the future.
+
+:::note
+Make sure you don't use `/log`, `/analytics`, `/analytics.js` or any similar name, because they're blocked by many adblockers by default.
+
+If you are not using the [Swetrix NPM package](/install-script#install-swetrix-via-npm), you will need to add a similar rewrite rule for `https://swetrix.org/swetrix.js` script. Don't forget to also replace it with your proxy URL in the analytics `<script>` tag.
+:::
+
+## Update Swetrix tracking script configuration
+After you set up Next.js redirect rules, you need to update swetrix.js tracking script to send analytics data through it. You can do it by setting the `apiURL` property inside the [init() function](/swetrix-js-reference#init).
+
+```javascript
+swetrix.init('YOUR_PROJECT_ID', {
+ apiURL: 'https://<yourproxydomain>/sproxy',
+})
+```
diff --git a/docs/adblockers/how-to-deal-with-adblockers.md b/docs/adblockers/how-to-deal-with-adblockers.md
index c1ac2a4b5..a337a2b3d 100644
--- a/docs/adblockers/how-to-deal-with-adblockers.md
+++ b/docs/adblockers/how-to-deal-with-adblockers.md
@@ -25,4 +25,5 @@ If you have installed Swetrix using our CDN (`swetrix.org/swetrix.js`), you will
### Managed proxy guides
- [Netlify](/adblockers/guides/netlify)
+ - [Next.js](/adblockers/guides/nextjs)
- [Nginx](/adblockers/guides/nginx)
diff --git a/sidebars.js b/sidebars.js
index 2d9c5c093..2546c1f84 100644
--- a/sidebars.js
+++ b/sidebars.js
@@ -41,14 +41,19 @@ module.exports = {
label: 'Proxy guides',
items: [
{
- id: 'adblockers/guides/nginx',
+ id: 'adblockers/guides/netlify',
type: 'doc',
- label: 'Nginx',
+ label: 'Netlify',
},
{
- id: 'adblockers/guides/netlify',
+ id: 'adblockers/guides/nextjs',
type: 'doc',
- label: 'Netlify',
+ label: 'Next.js',
+ },
+ {
+ id: 'adblockers/guides/nginx',
+ type: 'doc',
+ label: 'Nginx',
},
],
},