![]() |
VOOZH | about |
dotnet add package PostHog.AspNetCore --version 2.6.3
NuGet\Install-Package PostHog.AspNetCore -Version 2.6.3
<PackageReference Include="PostHog.AspNetCore" Version="2.6.3" />
<PackageVersion Include="PostHog.AspNetCore" Version="2.6.3" />Directory.Packages.props
<PackageReference Include="PostHog.AspNetCore" />Project file
paket add PostHog.AspNetCore --version 2.6.3
#r "nuget: PostHog.AspNetCore, 2.6.3"
#:package PostHog.AspNetCore@2.6.3
#addin nuget:?package=PostHog.AspNetCore&version=2.6.3Install as a Cake Addin
#tool nuget:?package=PostHog.AspNetCore&version=2.6.3Install as a Cake Tool
This is a client SDK for the PostHog API written in C#. This package depends on the PostHog package and provides additional functionality for ASP.NET Core projects.
Use the dotnet CLI to add the package to your project:
$ dotnet add package PostHog.AspNetCore
Register your PostHog instance in Program.cs (or Startup.cs depending on how you roll):
var builder = WebApplication.CreateBuilder(args);
builder.AddPostHog();
Set your project token using user secrets:
$ dotnet user-secrets set PostHog:ProjectToken YOUR_PROJECT_TOKEN
In most cases, that's all you need to configure!
If you're using the EU hosted instance of PostHog or a self-hosted instance, you can configure the HostUrl setting in
appsettings.json:
{
...
"PostHog": {
"HostUrl": "https://eu.i.posthog.com"
}
}
There are some more settings you can configure if you want to tweak the behavior of the client, but the defaults should work in most cases.
The available options are:
| Option | Description | Default |
|---|---|---|
HostUrl |
The URL of the PostHog instance. | https://us.i.posthog.com |
MaxBatchSize |
The maximum number of events to send in a single batch. | 100 |
MaxQueueSize |
The maximum number of events to store in the queue at any time. | 1000 |
FlushAt |
The number of events to enqueue before sending events to PostHog. | 20 |
FlushInterval |
The interval in milliseconds between periodic flushes. | 30 seconds |
The client will attempt to send events to PostHog in the background. It sends it every FlushInterval or when
FlushAt events have been enqueued. However, if the network is down or if there's a spike in events, the queue
could grow without restriction. The MaxQueueSize setting is there to prevent the queue from growing too large.
When that number is reached, the client will start dropping older events. MaxBatchSize ensures that the /batch
request doesn't get too large.
If you want to evaluate feature flags locally, you'll need to provide a personal API key for the PostHog API.
For local development, you can set the PostHog:PersonalApiKey setting in your user secrets:
$ dotnet user-secrets set PostHog:PersonalApiKey YOUR_PERSONAL_API_KEY
For production, we recommend using a secrets manager or environment variables to set the PostHog:PersonalApiKey setting.
More detailed docs for using this library can be found at PostHog Docs for the .NET Client SDK.
Inject the IPostHogClient interface into your controller or page:
public class HomeController(IPostHogClient posthog) : Controller
{
public IActionResult SignUpComplete()
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
posthog.Capture(userId, "user signed up", new() { ["plan"] = "pro" });
return View();
}
}
public class IndexModel(IPostHogClient posthog) : PageModel
{
public void OnGet()
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
posthog.CapturePageView(userId, Request.Path.Value ?? "Unknown");
}
}
See the Identifying users for more information about identifying users.
Identifying a user typically happens on the front-end. For example, when an authenticated user logs in, you can call identify to associate the user with their previously anonymous actions.
When identify is called the first-time for a distinct id, PostHog will create a new user profile. If the user already exists, PostHog will update the user profile with the new data. So the typical usage of IdentifyAsync here will be to update the person properties that PostHog knows about your user.
await posthog.IdentifyAsync(
userId,
new()
{
["email"] = "haacked@posthog.com",
["name"] = "Phil Haack",
["plan"] = "pro"
});
Use the Alias method to associate one identity with another. This is useful when a user logs in and you want to associate their anonymous actions with their authenticated actions.
var sessionId = Request.Cookies["session_id"]; // Used for anonymous actions.
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; // Now we know who they are.
await posthog.AliasAsync(sessionId, userId);
Note that capturing events is designed to be fast and done in the background. You can configure how often batches are sent to the PostHog API using the FlushAt and FlushInterval settings.
posthog.Capture(userId, "user signed up", new() { ["plan"] = "pro" });
posthog.CapturePageView(userId, Request.Path.Value ?? "Unknown");
posthog.CaptureScreen(userId, "Main Screen");
Check if the awesome-new-feature feature flag is enabled for the user with the id userId.
var enabled = await posthog.IsFeatureEnabledAsync(userId, "awesome-new-feature");
Some feature flags may have associated payloads.
if (await posthog.GetFeatureFlagAsync("awesome-new-feature", "some-user-id") is { Payload: {} payload })
{
// Do something with the payload.
Console.WriteLine($"The payload is: {payload}");
}
Using information on the PostHog server.
var flags = await posthog.GetAllFeatureFlagsAsync("some-user-id");
Overriding the group properties for the current user.
var flags = await posthog.GetAllFeatureFlagsAsync(
"some-user-id",
options: new AllFeatureFlagsOptions
{
Groups =
[
new Group("project", "aaaa-bbbb-cccc")
{
["$group_key"] = "aaaa-bbbb-cccc",
["size"] = "large"
}
]
});
PostHog.AspNetCore supports .NET Feature Management. This allows you to use the <feature /> tag helper and
the FeatureGateAttribute in your ASP.NET Core applications to gate access to certain features using PostHog
feature flags.
To use feature flags with the .NET Feature Management library, you'll need to implement the
IPostHogFeatureFlagContextProvider interface. The quickest way to do that is to inherit from the
PostHogFeatureFlagContextProvider class and override the GetDistinctId and GetFeatureFlagOptionsAsync methods.
public class MyFeatureFlagContextProvider(IHttpContextAccessor httpContextAccessor)
: PostHogFeatureFlagContextProvider
{
protected override string? GetDistinctId() => httpContextAccessor.HttpContext?.User.Identity?.Name;
protected override ValueTask<FeatureFlagOptions> GetFeatureFlagOptionsAsync()
{
// In a real app, you might get this information from a database or other source for the current user.
return ValueTask.FromResult(
new FeatureFlagOptions
{
PersonProperties = new Dictionary<string, object?>
{
["email"] = "some-test@example.com"
},
OnlyEvaluateLocally = true
});
}
}
Then, register your implementation in Program.cs (or Startup.cs):
var builder = WebApplication.CreateBuilder(args);
builder.AddPostHog(options => {
options.UseFeatureManagement<MyFeatureFlagContextProvider>();
});
You can now use feature tag helpers in your Razor views:
<feature name="awesome-new-feature">
<p>This is the new feature!</p>
</feature>
<feature name="awesome-new-feature" negate="true">
<p>Sorry, no awesome new feature for you.</p>
</feature>
Multivariate feature flags are also supported:
<feature name="awesome-new-feature" value="variant-a">
<p>This is the new feature variant A!</p>
</feature>
<feature name="awesome-new-feature" value="variant-b">
<p>This is the new feature variant B!</p>
</feature>
You can also use the FeatureGateAttribute to gate access to controllers or actions:
[FeatureGate("awesome-new-feature")]
public class NewFeatureController : Controller
{
public IActionResult Index()
{
return View();
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.6.3 | 23,035 | 5/11/2026 |
| 2.6.2 | 20,691 | 5/6/2026 |
| 2.6.1 | 332 | 5/5/2026 |
| 2.6.0 | 2,664 | 5/1/2026 |
| 2.5.0 | 9,821 | 4/23/2026 |
| 2.4.3 | 6,570 | 4/21/2026 |
| 2.4.2 | 160 | 4/21/2026 |
| 2.4.1 | 30,087 | 3/19/2026 |
| 2.4.0 | 20,514 | 3/5/2026 |
| 2.3.2 | 7,210 | 2/27/2026 |
| 2.3.1 | 131 | 2/27/2026 |
| 2.3.0 | 2,083 | 2/26/2026 |
| 2.2.2 | 138,306 | 11/21/2025 |
| 2.2.1 | 52,186 | 10/22/2025 |
| 2.2.0 | 2,859 | 10/17/2025 |
| 2.1.0 | 361 | 10/14/2025 |
| 2.0.1 | 21,186 | 9/28/2025 |
| 2.0.0 | 30,520 | 8/26/2025 |
| 1.0.8 | 15,959 | 8/7/2025 |
| 1.0.7 | 29,270 | 7/21/2025 |