OnyxMetric
Integration Guides

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

appsettings.json
{
  "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>:

Views/Shared/_Layout.cshtml
@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.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

  • Tag missing: appsettings.Development.json overrides appsettings.json. Check ASPNETCORE_ENVIRONMENT and that OnyxMetric:SiteId is set in the file that is active.
  • Content Security Policy: if you set one, allow app.onyxmetric.com in script-src and connect-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

On this page