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
- Paste the snippet into the
<head>of your web app'sindex.html, the file in thewebDirfolder fromcapacitor.configafter your framework's build:
<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>- Enable
CapacitorHttp, which routeswindow.fetchthrough native HTTP. The iOS WebView runs oncapacitor://localhost, and the tracking endpoints answer CORS preflights forhttp://andhttps://origins only, so without native HTTP the requests fail on iOS.
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;- Build the web app, then copy it into the native projects:
npm run build
npx cap syncAndroid 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.jsreturns200and 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:
CapacitorHttpis not enabled, or the native projects were not re-synced after the config change. Runnpx cap syncand rebuild in Xcode. - Changes do not show on the device:
npx cap synccopies the built web bundle. Edit the source, rebuild the web app, then sync again. - Live reload during development: with
server.urlpointing at your dev server, the WebView loads anhttp://origin, so requests succeed even withoutCapacitorHttp. Do not treat a working dev build as proof that production works. - Every pageview shows
localhostas hostname: expected, sinceserver.hostnamedefaults tolocalhost. Filter by site, not hostname.
Next steps
- Track custom events such as signups, purchases, and clicks.
- Identify users to tie sessions to accounts.
- Proxy the script through your own domain to get past ad blockers.
- Script attributes let you skip or mask URLs and tag events.