ASP.NET Core
Add OnyxMetric analytics to your ASP.NET Core app
ASP.NET Core MVC and Razor Pages apps wrap every view in _Layout.cshtml, so the tag belongs in that file's <head>. Keep the site ID in appsettings.json and read it with IConfiguration.
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 ASP.NET Core
Store the site ID
{
"OnyxMetric": {
"SiteId": "YOUR_SITE_ID"
}
}To vary it per environment, leave it empty here and override it in appsettings.Production.json or with the environment variable OnyxMetric__SiteId.
Edit the layout
Open Views/Shared/_Layout.cshtml (MVC) or Pages/Shared/_Layout.cshtml (Razor Pages), inject the configuration, and add the tag in <head>:
@inject IConfiguration Configuration
@{
var onyxSiteId = Configuration["OnyxMetric:SiteId"];
}
<head>
<meta charset="utf-8" />
<title>@ViewData["Title"] - MyApp</title>
@if (!string.IsNullOrEmpty(onyxSiteId))
{
<script src="https://app.onyxmetric.com/api/script.js?siteId=@onyxSiteId" async></script>
}
</head>If Microsoft.Extensions.Configuration is not imported in _ViewImports.cshtml, add @using Microsoft.Extensions.Configuration above the @inject line.
The @if check leaves the tag out when no ID is set, so development stays untracked. With several layouts, add the tag to each or move it to a partial view.
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
- Tag missing:
appsettings.Development.jsonoverridesappsettings.json. CheckASPNETCORE_ENVIRONMENTand thatOnyxMetric:SiteIdis set in the file that is active. - Content Security Policy: if you set one, allow
app.onyxmetric.cominscript-srcandconnect-src, or proxy the script through your own domain. - Response or output caching: the tag is rendered on the server, so a cached page keeps the configuration from when it was cached. Clear the cache after changing the ID.
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.