Umami.Net
0.1.4
See the version list below for details.
dotnet add package Umami.Net --version 0.1.4
NuGet\Install-Package Umami.Net -Version 0.1.4
<PackageReference Include="Umami.Net" Version="0.1.4" />
<PackageVersion Include="Umami.Net" Version="0.1.4" />
<PackageReference Include="Umami.Net" />
paket add Umami.Net --version 0.1.4
#r "nuget: Umami.Net, 0.1.4"
#addin nuget:?package=Umami.Net&version=0.1.4
#tool nuget:?package=Umami.Net&version=0.1.4
Umami.Net
This is a .NET Core client for the Umami tracking API. It's based on the Umami Node client, which can be found here.
You can see how to set up Umami as a docker container here. You can read more detail about it's creation on my blog here.
To use this client you need the following appsettings.json configuration:
{
"Analytics":{
"UmamiPath" : "https://umamilocal.mostlylucid.net",
"WebsiteId" : "32c2aa31-b1ac-44c0-b8f3-ff1f50403bee"
},
}
Where UmamiPath
is the path to your Umami instance and WebsiteId
is the id of the website you want to track.
To use the client you need to add the following to your Program.cs
:
using Umami.Net;
services.SetupUmamiClient(builder.Configuration);
This will add the Umami client to the services collection.
You can then use the client in two ways:
Track
- Inject the
UmamiClient
into your class and call theTrack
method:
// Inject UmamiClient umamiClient
await umamiClient.Track("Search", new UmamiEventData(){{"query", encodedQuery}});
- Use the
UmamiBackgroundSender
to track events in the background (this uses anIHostedService
to send events in the background):
// Inject UmamiBackgroundSender umamiBackgroundSender
await umamiBackgroundSender.Track("Search", new UmamiEventData(){{"query", encodedQuery}});
The client will send the event to the Umami API and it will be stored.
The UmamiEventData
is a dictionary of key value pairs that will be sent to the Umami API as the event data.
There are additionally more low level methods that can be used to send events to the Umami API.
Track PageView
There's also a convenience method to track a page view. This will send an event to the Umami API with the url set (which counts as a pageview).
await umamiBackgroundSender.TrackPageView("api/search/" + encodedQuery, "searchEvent", eventData: new UmamiEventData(){{"query", encodedQuery}});
await umamiClient.TrackPageView("api/search/" + encodedQuery, "searchEvent", eventData: new UmamiEventData(){{"query", encodedQuery}});
Here we're setting the url to "api/search/" + encodedQuery and the event type to "searchEvent". We're also passing in a dictionary of key value pairs as the event data.
Raw 'Send' method
On both the UmamiClient
and UmamiBackgroundSender
you can call the following method.
Send(UmamiPayload? payload = null, UmamiEventData? eventData = null,
string eventType = "event")
If you don't pass in a UmamiPayload
object, the client will create one for you using the WebsiteId
from the appsettings.json.
public UmamiPayload GetPayload(string? url = null, UmamiEventData? data = null)
{
var httpContext = httpContextAccessor.HttpContext;
var request = httpContext?.Request;
var payload = new UmamiPayload
{
Website = settings.WebsiteId,
Data = data,
Url = url ?? httpContext?.Request?.Path.Value,
IpAddress = httpContext?.Connection?.RemoteIpAddress?.ToString(),
UserAgent = request?.Headers["User-Agent"].FirstOrDefault(),
Referrer = request?.Headers["Referer"].FirstOrDefault(),
Hostname = request?.Host.Host,
};
return payload;
}
You can see that this populates the UmamiPayload
object with the WebsiteId
from the appsettings.json, the Url
, IpAddress
, UserAgent
, Referrer
and Hostname
from the HttpContext
.
NOTE: eventType can only be "event" or "identify" as per the Umami API.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Http.Polly (>= 8.0.8)
- Polly (>= 8.4.1)
- Polly.Extensions.Http (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.