Angular
Add OnyxMetric analytics to your Angular app
An Angular CLI project serves every route from one HTML shell, src/index.html. Put the tag there and the script loads once for the whole app.
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 Angular
Open src/index.html and place the tag right before </head>:
<head>
<meta charset="utf-8">
<title>MyAngularApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>
</head>Router navigation is picked up automatically and recorded as pageviews.
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.
Track custom events
Declare onyx on window once so TypeScript accepts the call, then use it from any component or service. Check that it exists first, because an ad blocker may stop the script from loading.
declare global {
interface Window {
onyx?: {
event: (eventName: string, eventData?: Record<string, any>) => void;
pageview: () => void;
};
}
}
@Component({
selector: "app-feature",
template: `<button (click)="onFeatureClick()">Use feature</button>`,
})
export class FeatureComponent {
onFeatureClick(): void {
window.onyx?.event("feature_used", { featureName: "Export" });
}
}Troubleshooting
- Pageviews missing after navigation: uncommon. Try the default first; if it still fails, subscribe to
Router.events, filter forNavigationEnd, and callwindow.onyx.pageview().
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.