Docusaurus
Add Rybbit analytics to your Docusaurus docs
Docusaurus has a scripts option in docusaurus.config.js that adds a <script> tag to the <head> of every page, so no theme swizzling is needed.
Get your tracking snippet
In your Rybbit dashboard, open Site Settings → Tracking Script and copy your snippet. It looks like this:
<script src="https://app.rybbit.io/api/script.js?siteId=YOUR_SITE_ID" defer></script>YOUR_SITE_ID is the numeric ID of your site. If you self-host Rybbit, app.rybbit.io is the domain of your own instance.
Add the snippet to Docusaurus
Open docusaurus.config.js (or docusaurus.config.ts) at the root of your project and add the script to the scripts array:
/** @type {import('@docusaurus/types').Config} */
const config = {
// ... title, url, baseUrl, presets, themeConfig
scripts: [
{
src: "https://app.rybbit.io/api/script.js?siteId=YOUR_SITE_ID",
defer: true,
},
],
};
module.exports = config;If you already manage tags through headTags, this form is equivalent:
headTags: [
{
tagName: "script",
attributes: {
src: "https://app.rybbit.io/api/script.js?siteId=YOUR_SITE_ID",
defer: "true",
},
},
],Rebuild with npm run build and deploy. The tag is not hot-reloaded into a running npm run start session until you restart it.
Docusaurus builds a single-page app on React Router. Rybbit detects client-side route changes and records them as pageviews, so no extra code is needed for navigation.
Verify installation
Open your live site in a new tab and click through a few pages. Within a few seconds the pageviews appear in the Rybbit dashboard.
If nothing shows up:
- View the page source and search for
script.js?siteId=to confirm the snippet is on the page. - Open the browser Network tab and check that
script.jsreturns200and thatPOSTrequests go to/api/track. - Disable ad blockers, or set up a proxy so the script loads from your own domain.
- See the script troubleshooting guide for other common causes.
Track custom events
Docusaurus pages are React, so you can fire events from a component and use it in MDX:
import React from "react";
export default function TrackedButton({ children, eventName, eventData }) {
const handleClick = () => {
window.rybbit?.event(eventName, eventData);
};
return (
<button onClick={handleClick} className="button button--primary">
{children}
</button>
);
}import TrackedButton from "@site/src/components/TrackedButton";
<TrackedButton eventName="learn_more_clicked" eventData={{ page: "introduction" }}>
Learn more
</TrackedButton>For site-wide listeners, use a client module. onRouteDidUpdate runs after every navigation once the DOM is ready:
export function onRouteDidUpdate() {
document.querySelectorAll('a[target="_blank"]').forEach((link) => {
link.addEventListener("click", () => {
window.rybbit?.event("external_link_click", { href: link.href });
});
});
}Register it in docusaurus.config.js under clientModules: [require.resolve("./src/client-modules/rybbit-events.js")]. Client modules also run during server-side rendering, so guard any window access.
Next steps
- Track custom events such as signups, purchases and button clicks.
- Identify users to connect sessions to accounts.
- Proxy the script through your own domain to bypass ad blockers.
- Script attributes let you skip or mask URLs and tag events.