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!

Hexo integration

Blue Mouse77d4900b14b5
diff --git a/docs/docs/integration-guides.md b/docs/docs/integration-guides.md
index 4c77ac07b..9ba6e32ad 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)
+- [Hexo](/hexo-integration)
- [htmx](/htmx-integration)
- [Hugo](/hugo-integration)
- [Jekyll](/jekyll-integration)
diff --git a/docs/docs/integrations/hexo.md b/docs/docs/integrations/hexo.md
new file mode 100644
index 000000000..beb10e8d8
--- /dev/null
+++ b/docs/docs/integrations/hexo.md
@@ -0,0 +1,231 @@
+---
+title: Hexo
+slug: /hexo-integration
+---
+
+Integrate Swetrix with your [Hexo](https://hexo.io/) site to track page views, monitor errors, and capture custom events β€” all while staying privacy-friendly and GDPR-compliant.
+
+Hexo is a fast, simple Node.js-powered static site generator. Since it produces plain HTML pages with no client-side routing, Swetrix tracks each page view automatically on every navigation.
+
+## Installation
+
+The recommended approach is to create a partial in your Hexo theme for the Swetrix tracking script and include it in your site's base layout.
+
+### 1. Create a partial
+
+In your active theme's directory, create the file `themes/<your-theme>/layout/_partial/swetrix-analytics.ejs` with the following content:
+
+```html
+<script src="https://swetrix.org/swetrix.js" defer></script>
+<script>
+ document.addEventListener('DOMContentLoaded', function () {
+ swetrix.init('<%= theme.swetrix_project_id || "YOUR_PROJECT_ID" %>')
+ swetrix.trackViews()
+ })
+</script>
+
+<noscript>
+ <img
+ src="https://api.swetrix.com/log/noscript?pid=<%= theme.swetrix_project_id || '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.
+:::
+
+### 2. Include it in your layout
+
+Open your theme's main layout file β€” typically `themes/<your-theme>/layout/layout.ejs`. Add the partial just before the closing `</body>` tag:
+
+```html
+ <%- partial('_partial/swetrix-analytics') %>
+ </body>
+</html>
+```
+
+If your theme uses a different template engine like Pug or Swig, the include syntax will differ:
+
+**Pug (`layout.pug`):**
+
+```pug
+ include _partial/swetrix-analytics
+ body
+```
+
+**Swig / Nunjucks (`layout.swig`):**
+
+```html
+ {% include '_partial/swetrix-analytics.swig' %}
+ </body>
+</html>
+```
+
+### 3. Build and deploy
+
+Generate your site for production:
+
+```bash
+hexo generate
+```
+
+Or with the shorthand:
+
+```bash
+hexo g
+```
+
+The generated output in the `public/` directory will include the tracking script on every page.
+
+## Using config variables (optional)
+
+To keep your Project ID out of the template and make it easy to manage, store it in your theme's `_config.yml`.
+
+**1. Add to your theme config (`themes/<your-theme>/_config.yml`):**
+
+```yaml
+swetrix_project_id: "YOUR_PROJECT_ID"
+```
+
+Alternatively, if you're using Hexo 5+ with theme configuration in the root `_config.yml`:
+
+```yaml
+theme_config:
+ swetrix_project_id: "YOUR_PROJECT_ID"
+```
+
+**2. Update the partial to read from config:**
+
+The partial shown in step 1 already uses `theme.swetrix_project_id` as the primary value. If the config variable is set, it will be used automatically β€” no changes needed.
+
+## Disable tracking in development
+
+By default, Swetrix ignores traffic from `localhost`, so page views during `hexo server` won't be sent. If you want to avoid loading the script entirely during development, you can wrap the partial in a conditional.
+
+**EJS:**
+
+```html
+<% if (config.env === 'production' || !config.env) { %>
+<script src="https://swetrix.org/swetrix.js" defer></script>
+<script>
+ document.addEventListener('DOMContentLoaded', function () {
+ swetrix.init('<%= theme.swetrix_project_id || "YOUR_PROJECT_ID" %>')
+ swetrix.trackViews()
+ })
+</script>
+
+<noscript>
+ <img
+ src="https://api.swetrix.com/log/noscript?pid=<%= theme.swetrix_project_id || 'YOUR_PROJECT_ID' %>"
+ alt=""
+ referrerpolicy="no-referrer-when-downgrade"
+ />
+</noscript>
+<% } %>
+```
+
+Then run your dev server with the environment variable:
+
+```bash
+NODE_ENV=development hexo server
+```
+
+And build for production with:
+
+```bash
+NODE_ENV=production hexo generate
+```
+
+:::tip
+Since Swetrix ignores `localhost` traffic by default, the environment-based conditional is optional. It's useful if you want to avoid loading the script entirely during development.
+:::
+
+## Error tracking
+
+Enable automatic client-side error monitoring by adding `trackErrors()` alongside your existing initialisation:
+
+```html
+<script src="https://swetrix.org/swetrix.js" defer></script>
+<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>
+```
+
+### Example: tracking form submissions
+
+```html
+<form id="contact-form" action="/thank-you">
+ <input type="email" placeholder="[email protected]" required />
+ <button type="submit">Subscribe</button>
+</form>
+
+<script>
+ document.addEventListener('DOMContentLoaded', function () {
+ var form = document.getElementById('contact-form')
+ if (!form || typeof swetrix === 'undefined') return
+
+ form.addEventListener('submit', function () {
+ 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
+
+## Using the Swetrix npm package
+
+Since Hexo runs on Node.js, you can also install the `swetrix` npm package if you want to use it in custom scripts or helper plugins:
+
+```bash
+npm install swetrix
+```
+
+Then use it in a client-side script bundled with your theme:
+
+```js
+import * as Swetrix from 'swetrix'
+
+Swetrix.init('YOUR_PROJECT_ID')
+Swetrix.trackViews()
+```
+
+## Check your installation
+
+After adding the tracking script, generate your site with `hexo generate` and deploy it. Visit your live website and navigate through a few pages. Within a minute you should see new pageviews appearing in your Swetrix dashboard.
+
+## 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.
+- [Hexo documentation](https://hexo.io/docs/) β€” official Hexo docs for themes, templates, plugins, and more.
diff --git a/docs/sidebars.js b/docs/sidebars.js
index 399882417..bcc6f077b 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/hexo',
'integrations/htmx',
'integrations/hugo',
'integrations/jekyll',