Documentation menuJavaScript frameworks: SolidJS

JavaScript frameworks

Add Loogika to SolidJS

You add Loogika to a SolidJS app by pasting one script tag into the head of the file that holds your HTML document. For a client-side Solid app made with Vite that file is index.html, and for a SolidStart app it is src/entry-server.tsx.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in the Loogika dashboard, under the Tracking Script tab. Your site ID is a 12-character string and replaces YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

How do you add Loogika to a Solid app built with Vite?

Paste the tag into the head element of index.html, which sits in the project root. The command npm create solid@latest generates this file for the vanilla templates.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Solid App</title>
    <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
  </head>
  <body>
    <div id="root"></div>
    <script src="/src/index.tsx" type="module"></script>
  </body>
</html>

How do you add Loogika to SolidStart?

SolidStart works without an index.html, so the tag goes into src/entry-server.tsx. In that file the StartServer component receives the HTML document through its document prop. The router is all that src/app.tsx renders, and it lands inside that document.

Place the tag in the head element, next to {assets}:

// @refresh reload
import { createHandler, StartServer } from "@solidjs/start/server";

export default createHandler(() => (
  <StartServer
    document={({ assets, children, scripts }) => (
      <html lang="en">
        <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <link rel="icon" href="/favicon.ico" />
          <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
          {assets}
        </head>
        <body>
          <div id="app">{children}</div>
          {scripts}
        </body>
      </html>
    )}
  />
));

Restart the SolidStart dev server after you change src/entry-server.tsx.

Does Loogika track route changes in SolidJS?

Yes. Loogika records a pageview for each route change made by @solidjs/router, and the file routes of SolidStart are included. You write no code for it.

Route changes are counted while SPA Navigation is on in Site Settings, which is the case for a new site. The Loogika script waits 500 milliseconds after a route change before it records the pageview.

How do you track custom events in SolidJS?

Call window.loogika.event() from any component, behind a check that the object exists. The check stops an error when the script is blocked or still loading. In SolidStart the same check also covers rendering on the server, where window does not exist.

export default function SignupButton() {
  const handleClick = () => {
    if (typeof window !== "undefined" && window.loogika) {
      window.loogika.event("signup_click", { location: "hero" });
    }
  };

  return <button onClick={handleClick}>Sign up</button>;
}

For a plain click you can drop the handler and add data-loogika-event="signup_click" to the button. The event tracking page describes properties and limits.

How do you check that Loogika is installed?

Load the deployed Solid app in a fresh browser tab and visit a few routes. Loogika's dashboard shows those pageviews within a few seconds.

If nothing shows up, look for script.js?siteId= in the page source. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track. Ad blockers can also stop the script, and the proxy guide shows a way around that.