Documentation menuDesktop, mobile and extensions: Capacitor

Desktop, mobile and extensions

Add Loogika to Capacitor

You add Loogika to a Capacitor app by pasting the script tag into the head of the web app's index.html and enabling the CapacitorHttp plugin. A Capacitor app is an ordinary web app inside a native WebView, so npx cap sync carries the tag to both iOS and Android. The plugin is what makes tracking work on iOS.

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 site ID in place of YOUR_SITE_ID:

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

Loogika has no npm package. The script tag is the install method on Capacitor, and it loads from app.loogika.com each time the app starts.

How do you add Loogika to a Capacitor app?

The install has three steps: add the tag, enable CapacitorHttp and sync the native projects.

  1. Open the index.html of your web app and paste the tag into its head. After your framework's build, that file must end up in the webDir folder named in capacitor.config.
  2. Enable CapacitorHttp in capacitor.config.ts, as shown below.
  3. Build the web app and copy it into the native projects with npx cap sync.
import type { CapacitorConfig } from "@capacitor/cli";

const config: CapacitorConfig = {
  appId: "com.example.app",
  appName: "My App",
  webDir: "dist",
  plugins: {
    CapacitorHttp: {
      enabled: true,
    },
  },
};

export default config;
npm run build
npx cap sync

An app built with Ionic Angular, React or Vue keeps index.html where that framework puts it. The guides for Angular, React and Vue show the place for the tag.

Why does Capacitor need CapacitorHttp for Loogika?

The iOS WebView runs your app on the origin capacitor://localhost, and the WebView blocks requests from that origin to Loogika. The Loogika tracking endpoints answer cross-origin checks (CORS) only for origins that start with http:// or https://.

CapacitorHttp routes window.fetch through native HTTP code, which is not bound by those browser checks. The Loogika script sends its data with fetch, so its requests go out once the plugin is on.

Android serves the app from https://localhost, because server.androidScheme defaults to https. That is a regular origin, so tracking works on Android with or without CapacitorHttp.

Does Loogika track screens inside the app?

Yes. Loogika records a pageview when your router changes the address through the History API or through a hash route. The setting for that is SPA Navigation in Site Settings, which is on for a new site.

How do you check that Loogika is installed?

Run a production build on a device or simulator and open a few screens. The pageviews show up in the Loogika dashboard within a few seconds.

Test iOS with a production build, not with live reload. When server.url points at your dev server, the origin in the WebView is http://, and requests succeed even without CapacitorHttp.

What are common problems with Loogika in Capacitor?

SymptomCause and fix
Android and the browser send data, iOS sends nothingEither CapacitorHttp is switched off, or nobody synced the native projects after the config change. Run npx cap sync, then build again in Xcode
The device still shows the old versionThe sync step copies the web bundle as it was last built. Rebuild the web app first, then sync again
Every pageview shows localhost as hostnameThat is normal. On both platforms the default value of server.hostname is localhost, so tell apps apart by site and not by hostname