Documentation menuBackend frameworks: Blazor

Backend frameworks

Add Loogika to Blazor

Your Blazor hosting model decides which file takes the Loogika tag. In a Blazor Web App the head element sits in Components/App.razor, and a standalone Blazor WebAssembly app keeps it in wwwroot/index.html. A Blazor Server app on .NET 7 or earlier uses Pages/_Host.cshtml.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. Blazor navigates inside the browser, so the tag you add once remains loaded.

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

The value YOUR_SITE_ID stands for your own site ID, a 12-character string.

How do you add Loogika to a Blazor Web App?

The server renders App.razor, so the site ID can come from appsettings.json. Start with a Loogika section in that file:

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

Next, Components/App.razor injects IConfiguration and prints the tag inside the head element:

@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" />
    ...
    <HeadOutlet />

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

You can set another value per environment in appsettings.Production.json or with the environment variable Loogika__SiteId.

How do you add Loogika to a standalone Blazor WebAssembly app?

In a standalone WebAssembly app, wwwroot/index.html is a static file. Paste the tag directly inside its head element:

<head>
    ...

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

A fixed site ID in index.html is fine, because the ID is visible in the page source of every site. The browser can read wwwroot/appsettings.json as well, so moving the ID there gives no advantage.

How do you add Loogika to a Blazor Server app on .NET 7 or earlier?

Add the same Loogika section to appsettings.json, then put the tag in Pages/_Host.cshtml. In a .NET 6 project the head element lives in Pages/_Layout.cshtml, so the tag goes there.

@page "/"
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
    var loogikaSiteId = Configuration["Loogika:SiteId"];
}
<!DOCTYPE html>
<html lang="en">
<head>
    ...
    <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />

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

Does Loogika track navigation inside a Blazor app?

Yes, as long as SPA Navigation is on in Site Settings, which is the default. The Blazor router and NavigationManager.NavigateTo change the address through the browser history instead of reloading the page. Enhanced navigation in a Blazor Web App does the same. The Loogika script watches those history changes and records each one as a pageview.

Enhanced navigation updates the page without running the scripts in the head element again. The Loogika script therefore loads once and keeps running for the whole visit.

A call to NavigateTo(url, forceLoad: true) and a link with data-enhance-nav="false" both cause a full page load. Loogika records a full page load as a new pageview.

How do you check that Loogika is installed?

Open the published app in a new tab and move between a few pages. The pageviews show up in the Loogika dashboard within a few seconds.

If the dashboard stays empty, open the page source and look for script.js?siteId=. The script troubleshooting page explains how to test the script from the browser console.

Why does Blazor not send data to Loogika?

CauseFix
A Blazor Web App renders no tagApp.razor gets its configuration on the server. Look at the appsettings file that belongs to the active ASPNETCORE_ENVIRONMENT, and remember that environment variables override the files
A Blazor WebAssembly PWA serves a cached pageThe service worker caches index.html. Visitors get the tag after you republish the app and the service worker updates
A Content Security Policy blocks the scriptAllow the Loogika domain with the directives below, or load the script through a proxy on your own domain

A Content Security Policy must allow Loogika in two directives:

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