Skip to content

Commit 9ac2261

Browse files
committed
docs: centralize SDK examples in official docs
1 parent 33cb476 commit 9ac2261

4 files changed

Lines changed: 26 additions & 869 deletions

File tree

README.md

Lines changed: 10 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,231 +1,19 @@
1-
# PostHog DotNet Client SDK ![Build status](https://github.com/PostHog/posthog-dotnet/actions/workflows/main.yaml/badge.svg?branch=main)
1+
# PostHog .NET
22

3-
This repository contains a set of packages for interacting with the PostHog API in .NET applications.
4-
This README is for those who wish to contribute to these packages.
3+
Please see the main [PostHog docs](https://posthog.com/docs).
54

6-
For documentation on the specific packages, see the README files in the respective package directories.
5+
SDK usage examples and code snippets live in the official documentation so they stay up to date.
76

8-
## Packages
7+
## Documentation
98

10-
| Package | Version | Description
11-
|---------|---------| -----------
12-
| [PostHog.AspNetCore](src/PostHog.AspNetCore/README.md) | [![NuGet version (PostHog.AspNetCore)](https://img.shields.io/nuget/v/PostHog.AspNetCore.svg?style=flat-square)](https://www.nuget.org/packages/PostHog.AspNetCore/) | For use in ASP.NET Core projects.
13-
| [PostHog](src/PostHog/README.md) | [![NuGet version (PostHog)](https://img.shields.io/nuget/v/PostHog.svg?style=flat-square)](https://www.nuget.org/packages/PostHog/) | The core library. Over time, this will support client environments such as Unit, Xamarin, etc.
14-
| [PostHog.AI](src/PostHog.AI/README.md) | [![NuGet version (PostHog.AI)](https://img.shields.io/nuget/v/PostHog.AI.svg?style=flat-square)](https://www.nuget.org/packages/PostHog.AI/) | AI Observability for OpenAI and other LLM providers.
15-
16-
## Platform
17-
18-
The core [PostHog](./src/PostHog/README.md) package targets `netstandard2.1` and `net8.0` for broad compatibility. The [PostHog.AspNetCore](src/PostHog.AspNetCore/README.md) package targets `net8.0`. The [PostHog.AI](src/PostHog.AI/README.md) package targets `netstandard2.1` and `net8.0` for broad compatibility.
9+
- [.NET library docs](https://posthog.com/docs/libraries/dotnet)
10+
- [.NET error tracking docs](https://posthog.com/docs/error-tracking/installation/dotnet)
11+
- [AI observability docs](https://posthog.com/docs/ai-observability)
1912

2013
## Contributing
2114

22-
See [CONTRIBUTING.md](CONTRIBUTING.md) for build, sample, and test instructions.
23-
24-
## Docs
25-
26-
More detailed docs for using this library can be found at [PostHog Docs for the .NET Client SDK](https://posthog.com/docs/libraries/dotnet).
27-
28-
## Installation
29-
30-
For ASP.NET Core projects, install the `PostHog.AspNetCore` package:
31-
32-
```bash
33-
$ dotnet add package PostHog.AspNetCore
34-
```
35-
36-
And register the PostHog services in `Program.cs` (or `Startup.cs`) file by calling the `AddPostHog` extension
37-
method on `IHostApplicationBuilder` like so:
38-
39-
```csharp
40-
using PostHog;
41-
var builder = WebApplication.CreateBuilder(args);
42-
builder.AddPostHog();
43-
```
44-
45-
For other .NET projects, install the `PostHog` package:
46-
47-
```bash
48-
$ dotnet add package PostHog
49-
```
50-
51-
And if your project supports dependency injection, register the PostHog services in `Program.cs` (or `Startup.cs`)
52-
file by calling the `AddPostHog` extension method on `IServiceCollection`. Here's an example for a console app:
53-
54-
```csharp
55-
using PostHog;
56-
var services = new ServiceCollection();
57-
services.AddPostHog();
58-
var serviceProvider = services.BuildServiceProvider();
59-
var posthog = serviceProvider.GetRequiredService<IPostHogClient>();
60-
```
61-
62-
For a console app (or apps not using dependency injection), you can also use the `PostHogClient` directly, just make
63-
sure it's a singleton:
64-
65-
```csharp
66-
using System;
67-
using PostHog;
68-
69-
var posthog = new PostHogClient(Environment.GetEnvironmentVariable("PostHog__PersonalApiKey"));
70-
```
71-
72-
The `AddPostHog` methods accept an optional `Action<PostHogOptions>` parameter that you can use to configure the
73-
client. For examples, check out the [HogTied.Web sample project](../samples/HogTied.Web/Program.cs) and the unit tests.
74-
75-
## Usage
76-
77-
Inject the `IPostHogClient` interface into your controller or page:
78-
79-
```csharp
80-
posthog.Capture(userId, "user signed up", new() { ["plan"] = "pro" });
81-
```
82-
83-
```csharp
84-
client.CapturePageView(userId, Request.Path.Value ?? "Unknown");
85-
```
86-
87-
### Identity
88-
89-
#### Identify a user
90-
91-
See the [Identifying users](https://posthog.com/docs/product-analytics/identify) for more information about identifying users.
92-
93-
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.
94-
95-
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.
96-
97-
```csharp
98-
await posthog.IdentifyAsync(
99-
userId,
100-
new()
101-
{
102-
["email"] = "haacked@posthog.com",
103-
["name"] = "Phil Haack",
104-
["plan"] = "pro"
105-
});
106-
```
107-
108-
#### Alias a user
109-
110-
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.
111-
112-
```csharp
113-
await posthog.AliasAsync(sessionId, userId);
114-
```
115-
116-
### Analytics
117-
118-
#### Capture an Event
119-
120-
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.
121-
122-
```csharp
123-
posthog.Capture(userId, "user signed up", new() { ["plan"] = "pro" });
124-
```
125-
126-
#### Capture a Page View
127-
128-
```csharp
129-
posthog.CapturePageView(userId, Request.Path.Value ?? "Unknown");
130-
```
131-
132-
#### Capture a Screen View
133-
134-
```csharp
135-
posthog.CaptureScreen(userId, "Main Screen");
136-
```
137-
138-
### Feature Flags
139-
140-
#### Check if feature flag is enabled
141-
142-
Check if the `awesome-new-feature` feature flag is enabled for the user with the id `userId`.
143-
144-
```csharp
145-
var enabled = await posthog.IsFeatureEnabledAsync(userId, "awesome-new-feature");
146-
```
147-
148-
You can override properties of the user stored on PostHog servers for the purposes of feature flag evaluation.
149-
For example, suppose you offer a temporary pro-plan for the duration of the user's session. You might do this:
150-
151-
```csharp
152-
if (await posthog.IsFeatureEnabledAsync(
153-
"pro-feature",
154-
"some-user-id",
155-
personProperties: new() { ["plan"] = "pro" }))
156-
{
157-
// Access to pro feature
158-
}
159-
```
160-
161-
If you have group analytics enabled, you can also override group properties.
162-
163-
```csharp
164-
if (await posthog.IsFeatureEnabledAsync(
165-
"large-project-feature",
166-
"some-user-id",
167-
new FeatureFlagOptions
168-
{
169-
Groups = [new Group(groupType: "project", groupKey: "project-group-key") { ["size"] = "large" }]
170-
}))
171-
{
172-
// Access large project feature
173-
}
174-
```
175-
176-
> [!NOTE]
177-
> Specifying `PersonProperties` and `GroupProperties` is necessary when using local evaluation of feature flags.
178-
179-
#### Evaluation contexts
180-
181-
You can configure SDK-level feature flag evaluation contexts to only evaluate flags intended for this application,
182-
platform, or product area.
183-
184-
```csharp
185-
services.AddPostHog(options =>
186-
{
187-
options.ProjectToken = "YOUR_PROJECT_TOKEN";
188-
options.EvaluationContexts = ["main-app", "api", "backend"];
189-
});
190-
```
191-
192-
Flags without evaluation contexts continue to evaluate for all SDKs. Flags with contexts evaluate only when at least
193-
one configured context matches.
194-
195-
### Get a single Feature Flag
196-
197-
Some feature flags may have associated payloads.
198-
199-
```csharp
200-
if (await posthog.GetFeatureFlagAsync("awesome-new-feature", "some-user-id") is { Payload: {} payload })
201-
{
202-
// Do something with the payload.
203-
Console.WriteLine($"The payload is: {payload}");
204-
}
205-
```
206-
207-
### Get All Feature Flags
208-
209-
Using information on the PostHog server.
210-
211-
```csharp
212-
var flags = await posthog.GetAllFeatureFlagsAsync("some-user-id");
213-
```
15+
See [CONTRIBUTING.md](CONTRIBUTING.md) for local setup and test instructions.
21416

215-
Overriding the group properties for the current user.
17+
## Releasing
21618

217-
```csharp
218-
var flags = await posthog.GetAllFeatureFlagsAsync(
219-
"some-user-id",
220-
options: new AllFeatureFlagsOptions
221-
{
222-
Groups =
223-
[
224-
new Group("project", "aaaa-bbbb-cccc")
225-
{
226-
["$group_key"] = "aaaa-bbbb-cccc",
227-
["size"] = "large"
228-
}
229-
]
230-
});
231-
```
19+
See [RELEASING.md](RELEASING.md).

0 commit comments

Comments
 (0)