Use the server SDK where the thing happened on your server — a payment confirmed, a subscription
renewed, an invite accepted. In browser code, use @togohq/sdk-browser.
.NET 8 or later.
Azure Functions (isolated worker)
dotnet add package TogoHq.Sdk.Functions.Worker
using TogoHq.Sdk.Functions.Worker;
var builder = FunctionsApplication.CreateBuilder(args);
builder.UseTogo(); // sends each invocation's events before it returns
builder.Services.AddTogo(o => o.WriteKey = Environment.GetEnvironmentVariable("TOGO_WRITE_KEY")!);
builder.Build().Run();
Then take ITogoClient in your function’s constructor and call Track:
public sealed class PaymentWebhook(ITogoClient togo)
{
[Function("payment-webhook")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData request)
{
// ...confirm the payment first
togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
return request.CreateResponse(HttpStatusCode.OK);
}
}
Do not skip UseTogo(). The Functions host may freeze the process the moment a function
returns, and a frozen process sends nothing, so events waiting for the next batch can be lost. The
middleware sends them before the invocation completes, also when the function throws. It covers
every function in the app, including ones you add later.
Anywhere else
dotnet add package TogoHq.Sdk
using TogoHq.Sdk;
// One client per process, shared by every request.
await using var togo = new TogoClient(new TogoClientOptions { WriteKey = writeKey });
togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
Disposing the client sends what is queued. In a short-lived process, await togo.FlushAsync()
before it ends.
In a serverless handler with no UseTogo() — AWS Lambda, for example — flush in a finally, so the
events leave before the handler returns, also when it throws:
public class Function
{
// One client per process: Lambda reuses it across warm invocations.
private static readonly TogoClient togo = new(new TogoClientOptions
{
WriteKey = Environment.GetEnvironmentVariable("TOGO_WRITE_KEY"),
});
public async Task<APIGatewayProxyResponse> Handler(APIGatewayProxyRequest request, ILambdaContext context)
{
try
{
// ...confirm the payment first
togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
return new APIGatewayProxyResponse { StatusCode = 200 };
}
finally
{
await togo.FlushAsync();
}
}
}
Name the person on every call
A server serves many people, so nothing is remembered between calls. Use the same userId the
browser passes to identify(), or each user becomes two people. Before sign-in, forward the
browser’s anonymous id with TogoPerson.Anonymous(id). More:
Send events for the right person.
Check it works
Confirm the events arrived: Check that events arrive.
Use a test key (togo_pkt_…) while you build. Track returns the event’s id; after
await togo.FlushAsync(), ask your coding agent to check those ids with
verify_tracking_by_event_ids. If events
do not arrive, see Server events are missing.
A local run will not show a missing UseTogo() or FlushAsync(): your machine never freezes the
process, so the SDK’s timer sends the batch anyway. Where AWS_LAMBDA_FUNCTION_NAME or
FUNCTIONS_WORKER_RUNTIME is set — Azure Functions Core Tools sets the second locally — the SDK
logs one [togo] events were sent twice by the 10-second timer … warning when the timer sends
twice with no FlushAsync() between, naming the fix. After you deploy a function, trigger it and
watch Events → Live View → Production in the dashboard.
Every method and option: TogoHq.Sdk reference.