Documentation tools
Add Loogika to Docusaurus
You add Loogika to Docusaurus with the scripts option in docusaurus.config.js. Docusaurus then writes the script tag into the head of every page. You do not need to swizzle a theme component, which is the Docusaurus term for replacing one with your own copy.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
In Docusaurus you do not paste the tag itself. You copy the address from the src attribute into the config file.
How do you add Loogika in docusaurus.config.js?
Add one entry to the scripts array. The config file sits at the root of your project and is named docusaurus.config.js or docusaurus.config.ts.
/** @type {import('@docusaurus/types').Config} */
const config = {
// ... title, url, baseUrl, presets, themeConfig
scripts: [
{
src: "https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID",
defer: true,
},
],
};
module.exports = config;
Run npm run build and deploy the result. A running npm run start session does not pick up the new tag, so restart the session before you test locally.
Can you use headTags instead of scripts?
Yes. A headTags entry gives the same result as the scripts entry. Use it when your project already keeps its head tags in that option.
headTags: [
{
tagName: "script",
attributes: {
src: "https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID",
defer: "true",
},
},
],
Does Loogika count page changes in Docusaurus?
Yes. A Docusaurus site is a single-page app that runs on React Router, so a click on a sidebar link changes the address without a full page load. Loogika records each of those route changes as a pageview when SPA Navigation is on in Site Settings. The setting is on for a new site.
How do you track a custom event in Docusaurus?
Docusaurus pages are React, so you can send events from a component and place that component in an MDX page. The button below calls window.loogika.event() when a reader clicks it.
import React from "react";
export default function TrackedButton({ children, eventName, eventData }) {
const handleClick = () => {
window.loogika?.event(eventName, eventData);
};
return (
<button onClick={handleClick} className="button button--primary">
{children}
</button>
);
}
Save the component as src/components/TrackedButton.jsx. Then import it in an MDX page and pass the event name and its properties:
import TrackedButton from "@site/src/components/TrackedButton";
<TrackedButton eventName="learn_more_clicked" eventData={{ page: "introduction" }}>
Learn more
</TrackedButton>
Links to other sites need no component. A click on an external link becomes an outbound event in Loogika while Outbound Links is on in Site Settings, under the Tracking tab.
What should you know about Docusaurus client modules?
A client module is a Docusaurus file for code that runs on the whole site. Two details matter if you add tracking code of your own in one.
- The
onRouteDidUpdatefunction runs after every page change, while the navbar and the footer stay mounted. Listeners that you attach to them inside that function accumulate. - Server-side rendering runs client modules too. Check that
windowexists before your code reads it.
How do you check that Loogika is installed?
Open your deployed docs site in another tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.
If nothing arrives, open the page source and look for script.js?siteId=. When the tag is missing, rebuild the site and deploy it again. The script troubleshooting page lists other causes, such as ad blockers.