OnyxMetric
Integration Guides

Capacitor

Add OnyxMetric analytics to your Capacitor app

Capacitor wraps a web app in a native WebView, so the tag goes in the <head> of that web app's index.html and ships to iOS and Android with npx cap sync. Apps built with Ionic, React, Vue, Angular, or plain HTML all work the same way.

Copy your tracking snippet

In OnyxMetric, open your site's Site Settings and copy the tracking script. It looks like this:

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

YOUR_SITE_ID is the numeric ID of your site.

Add the snippet to Capacitor

  1. Paste the snippet into the <head> of your web app's index.html, the file in the webDir folder from capacitor.config after your framework's build:
index.html
<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>
  1. Enable CapacitorHttp, which routes window.fetch through native HTTP. The iOS WebView runs on capacitor://localhost, and the tracking endpoints answer CORS preflights for http:// and https:// origins only, so without native HTTP the requests fail on iOS.
capacitor.config.ts
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;
  1. Build the web app, then copy it into the native projects:
npm run build
npx cap sync

Android serves the app from https://localhost, a regular origin, so tracking works there with or without CapacitorHttp. In-app route changes are tracked automatically.

Check that it works

Load your live site and click through a few pages. Pageviews show up in the OnyxMetric dashboard within seconds.

If nothing arrives:

  • View the page source and search for script.js?siteId= to confirm the tag is present.
  • In the browser's Network tab, check that script.js returns 200 and that requests go to /api/track.
  • Turn off ad blockers, or proxy the script through your own domain.
  • Read script troubleshooting for other causes.

Troubleshooting

  • Works on Android and in the browser, silent on iOS: CapacitorHttp is not enabled, or the native projects were not re-synced after the config change. Run npx cap sync and rebuild in Xcode.
  • Changes do not show on the device: npx cap sync copies the built web bundle. Edit the source, rebuild the web app, then sync again.
  • Live reload during development: with server.url pointing at your dev server, the WebView loads an http:// origin, so requests succeed even without CapacitorHttp. Do not treat a working dev build as proof that production works.
  • Every pageview shows localhost as hostname: expected, since server.hostname defaults to localhost. Filter by site, not hostname.

Next steps

On this page