ASP.NET Core
Add Rybbit analytics to your ASP.NET Core app
ASP.NET Core MVC and Razor Pages apps share one layout file, _Layout.cshtml, that wraps every view, so the snippet goes into its <head>. Keep the site ID in appsettings.json and inject IConfiguration into the layout so nothing is hard-coded.
Get your tracking snippet
In your Rybbit dashboard, open Site Settings → Tracking Script and copy your snippet. It looks like this:
<script src="https://app.rybbit.io/api/script.js?siteId=YOUR_SITE_ID" defer></script>YOUR_SITE_ID is the numeric ID of your site. If you self-host Rybbit, app.rybbit.io is the domain of your own instance.
Add the snippet to ASP.NET Core
Configure the site ID
Add a Rybbit section to appsettings.json:
{
"Rybbit": {
"InstanceUrl": "https://app.rybbit.io",
"SiteId": "YOUR_SITE_ID"
}
}To keep the ID out of source control or vary it per environment, leave SiteId empty in appsettings.json and set it in appsettings.Production.json, or as the environment variable Rybbit__SiteId (double underscore separates configuration sections).
Add the tag to your layout
Open the layout file:
- Controllers with views:
Views/Shared/_Layout.cshtml - Razor Pages:
Pages/Shared/_Layout.cshtml
Inject IConfiguration at the top and add the tag inside <head>:
@inject IConfiguration Configuration
@{
var rybbitSiteId = Configuration["Rybbit:SiteId"];
var rybbitInstanceUrl = Configuration["Rybbit:InstanceUrl"] ?? "https://app.rybbit.io";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - MyApp</title>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
@if (!string.IsNullOrEmpty(rybbitSiteId))
{
<script src="@rybbitInstanceUrl/api/script.js?siteId=@rybbitSiteId" defer></script>
}
</head>If Microsoft.Extensions.Configuration is not already imported in _ViewImports.cshtml, add @using Microsoft.Extensions.Configuration above the @inject line.
The @if guard skips the tag when SiteId is empty, so development is not tracked unless you set the value there. If your app has more than one layout, add the tag to each, or move it into a partial view and render it with <partial name="_Rybbit" /> from every layout.
Verify installation
Open your live site in a new tab and click through a few pages. Within a few seconds the pageviews appear in the Rybbit dashboard.
If nothing shows up:
- View the page source and search for
script.js?siteId=to confirm the snippet is on the page. - Open the browser Network tab and check that
script.jsreturns200and thatPOSTrequests go to/api/track. - Disable ad blockers, or set up a proxy so the script loads from your own domain.
- See the script troubleshooting guide for other common causes.
Troubleshooting
- Tag missing: environment-specific files such as
appsettings.Development.jsonoverrideappsettings.json, so check which environment you are running (ASPNETCORE_ENVIRONMENT) and that theRybbitsection exists there or in the base file. Environment variables override both. - Content Security Policy: if middleware adds a
Content-Security-Policyheader, add your Rybbit host toscript-srcandconnect-src, or proxy the script through your own domain. - Response caching or output caching: the tag is rendered server-side, so a cached response keeps whatever configuration was active when it was cached. Clear the cache after changing the site ID.
- Self-hosted instance: set
Rybbit:InstanceUrlto your own domain without a trailing slash; the layout appends the script path to it.
Next steps
- Track custom events such as signups, purchases and button clicks.
- Identify users to connect sessions to accounts.
- Proxy the script through your own domain to bypass ad blockers.
- Script attributes let you skip or mask URLs and tag events.