Lavabird.Amplitude 1.1.0

dotnet add package Lavabird.Amplitude --version 1.1.0
                    
NuGet\Install-Package Lavabird.Amplitude -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lavabird.Amplitude" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lavabird.Amplitude" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Lavabird.Amplitude" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lavabird.Amplitude --version 1.1.0
                    
#r "nuget: Lavabird.Amplitude, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lavabird.Amplitude@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lavabird.Amplitude&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Lavabird.Amplitude&version=1.1.0
                    
Install as a Cake Tool

Amplitude

This is a .Net library to track events and user data via the Amplitude analytics platform.

Features

  • Batching of events to minimize network traffic.
  • Automatic retry in case of temporary network loss - as is typical on mobile.
  • Background persistence for unsent events in case of network loss on on exit or application crash.
  • Simple logging harness with no external dependencies. Easily connect with your existing logging setup.
  • AOT compatible (see below for details).

Usage

Initialization

Create a new instance of the AmplitudeService using the API key for the Amplitude project you are sending data to.

var amplitude = new AmplitudeService("<YOUR_API_KEY>");

The service includes a default configuration with behavior suitable for standard use cases. For more specific requirements the behavior can be customized using an AmplitudeServiceSettings instance.

var settings = new AmplitudeServiceSettings()
{
	DispatchBatchPeriodSeconds = 10,
	UseEuResidency = true,
};
var amplitude = new AmplitudeService("<YOUR_API_KEY>", settings: settings);
Identify

Events in Amplitude are tied to an identity in the form of a user or device identifier (or both). These are represented by an AmplitudeIdentity. If only a user identity is specified, then Amplitude will automatically generate a device identifier based on a hash of the user identifier.

A call to Identify will set the identity in use for the current session. This identity will apply to events sent after this call.

var identity = new AmplitudeIdentity("user_id", "device_id");
amplitude.Identify(identity);
amplitude.Event("Dummy Event"); // Will use the previous identity

Amplitude supports storing custom data about each identity. This can be sent with the Identify call to be made available in the Amplitude dashboard.

var identity = new AmplitudeIdentity("user_id", "device_id");
amplitude.Identify(identity, new()
{
    UserProperty = "Foo",
    AnotherProperty = 20,
});
Sending Events

Events in Amplitude consist of an event name, and a set of optional parameters to send with that event. These optional parameters are extracted as key-value pairs to display in the Amplitude dashboard.

Sending a simple event:

amplitude.Event("Dummy Event");

Sending custom event data via anonymous object:

amplitude.Event("Dummy Event", new {
	DummyProperty = "Foo",
	AnotherProperty = 20,
});

Alternatively custom data can be sent using a Dictionary instead:

var data = new Dictionary<string, object>()  
{  
    ["DummyProperty"] = "Foo",  
    ["AnotherProperty"] = 10,  
};
amplitude.Event("Dummy Event", data);

In most cases, the library will be used on a client and the identity can be set once with an Identify call. However, if being used server side you may want to tie each event to a specific user each time an event is created. All of the Event methods include an extra overload to also pass an identity for that specific event.

var identity = new AmplitudeIdentity("user_id", "device_id");
amplitude.Event(identity, "Dummy Event", data);
Persistence

A Stream (normally a FileStream) can be passed during initialization to persist unsent events on Shutdown() or Dispose().

var stream = File.Open("path/to/file", FileMode.OpenOrCreate);
var amplitude = new AmplitudeService("<YOUR_API_KEY>", persistenceStream: stream);

The persistence stream will also be written to periodically in the background in case the parent application crashes with unsent data. The write frequency can be controlled with an AmplitudeServiceSettings object during initialization.

During initialization, the stream will be checked for existing data from a previous session. Any unsent data will be added to the queue to be retried. The AmplitudeService will have generated a unique insert ID with each event, so if an event was previously sent but still saved (e.g. due to an application crash or a network error when confirming the event) the event will not be duplicated by Amplitude.

Persisted events older than Amplitude's max timeout of 7 days will not be replayed by default.

AOT

The library is AOT compatible, but this comes with additional constraints.

The Amplitude API requires objects to be serialized as JSON, and since reflection will not be available, you will need to provide a custom JsonSerializerContext and use concrete event types rather than anonymous objects. See the STJ reference docs for more information.

Using Dictionary<string, object> will also work for basic JSON compatible types, but is not recommended. Passing an annoymous type directly to Event will also work since the metadata will be preserved, but anonymous subtypes will not.

Installation

Simply add a reference to the Lavabird.Amplitude NuGet package to your project.

Install the package in the project:

Install-Package Lavabird.Amplitude

Compatibility

Supports .Net Framework 4.7, Net 6, or .Net 8+.

License

Licensed under the MIT license. Based on the original AmplitudeSharp by Mark Vulfson.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 149 6/15/2026
1.0.5 269 10/31/2024
1.0.4 202 10/27/2024 1.0.4 is deprecated because it is no longer maintained.