Documentation menuBackend frameworks: ASP.NET Core

Backend frameworks

Add Loogika to ASP.NET Core

Apps built with ASP.NET Core MVC or Razor Pages wrap every view in one layout file, _Layout.cshtml. You add the Loogika tag to the head of that layout. The site ID sits in appsettings.json, and the layout reads it through IConfiguration.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in the Loogika dashboard, under the Tracking Script tab. The 12-character site ID in the tag is the value you move into the app configuration:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

Where do you store the site ID?

Add a Loogika section with a SiteId value to appsettings.json:

{
  "Loogika": {
    "SiteId": "YOUR_SITE_ID"
  }
}

Some teams want the ID outside source control, and others need a separate ID for each environment. In both cases, give SiteId an empty value in appsettings.json and the real value in appsettings.Production.json. The environment variable Loogika__SiteId does the same job, where the double underscore separates configuration sections.

How do you add the tag to the layout?

Inject IConfiguration at the top of the layout file and print the tag inside the head element. An app that uses controllers with views has the layout in Views/Shared/_Layout.cshtml. A Razor Pages app has it in Pages/Shared/_Layout.cshtml.

@inject IConfiguration Configuration
@{
    var loogikaSiteId = Configuration["Loogika:SiteId"];
}
<!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>
    ...

    @if (!string.IsNullOrEmpty(loogikaSiteId))
    {
        <script src="https://app.loogika.com/api/script.js?siteId=@loogikaSiteId" defer></script>
    }
</head>

The @if block skips the tag when SiteId is empty. Development is then not tracked unless you set a value in that environment.

The @inject line needs the Microsoft.Extensions.Configuration namespace. If _ViewImports.cshtml does not import it already, put @using Microsoft.Extensions.Configuration on the line before @inject.

What if the app has more than one layout?

Every layout needs the tag. Paste it into all of them, or put it in a partial view that they share. A partial named _Loogika is rendered from every layout with <partial name="_Loogika" />, so the tag lives in one file.

How do you check that Loogika is installed?

Open the running site in another tab and visit a few pages. The Loogika dashboard shows the pageviews within a few seconds.

If nothing arrives, open the page source and look for script.js?siteId=. When the tag is there, the Network tab of your browser should show requests to /api/track with status 200. The script troubleshooting page covers ad blockers and other causes.

Why does ASP.NET Core not send data to Loogika?

CauseFix
Another settings file overrides the valueA file for one environment, such as appsettings.Development.json, takes priority over appsettings.json, and environment variables take priority over both. Check ASPNETCORE_ENVIRONMENT and confirm that the Loogika section exists for that environment
A Content Security Policy blocks the scriptWhen a Content-Security-Policy header comes from your middleware, allow the Loogika domain with the directives below, or load the script through a proxy on your own domain
An old page comes from response caching or output cachingThe server renders the tag, so a cached response keeps the configuration from the moment it was cached. Clear the cache after you change the site ID

A Content Security Policy must allow Loogika in two directives:

script-src 'self' https://app.loogika.com; connect-src 'self' https://app.loogika.com;