The hassle-free way to add Segment analytics to your .Net app (Unity/Xamarin/.Net). Analytics helps you measure your users, product, and business. It unlocks insights into your app's funnel, core business metrics, and whether you have product-market fit.
Analytics-CSharp is supported across the following platforms:
- net/.net core/.net framework
- mono
- universal windows platform
- xamarin
- ios
- mac
- android
- unity
- ios
- android
- pc, mac, linux
NOTE: This project is currently only available in Beta phase and is covered by Segment's First Access & Beta Preview Terms. We encourage you to try out this new library. Please provide feedback via Github issues/PRs, and feel free to submit pull requests.
NOTE: Migrating from Analytics.NET and Analytics.Xamarin? See our migration guide here
BREAKING CHANGES ALERT: if you are upgrading from 1.+ to 2.+, check out the breaking changes list here.
To get started with the Analytics-CSharp library:
-
Create a Source in Segment.
- Go to Connections > Sources > Add Source.
- Search for Xamarin or Unity or .NET and click Add source.
-
Add the Analytics dependency to your project.
dotnet add package Segment.Analytics.CSharp --version <LATEST_VERSION>
Analytics-CSharp is distributed via NuGet. Check other installation options here.
-
Initialize and configure the client.
// NOTE: to make Analytics stateless/in-memory, // add `InMemoryStorageProvider` to the configuration var configuration = new Configuration("<YOUR WRITE KEY>", flushAt: 1, flushInterval: 10); var analytics = new Analytics(configuration);
These are the options you can apply to configure the client:
Option Name | Description |
---|---|
writeKey required |
This is your Segment write key. |
apiHost |
Default set to api.segment.io/v1 . This sets a default API Host to which Segment sends events. |
autoAddSegmentDestination |
Default set to true . This automatically adds the Segment Destination plugin. You can set this to false if you want to manually add the Segment Destination plugin. |
defaultSettings |
Default set to {} . The settings object used as fallback in case of network failure. |
flushAt |
Default set to 20 . The count of events at which Segment flushes events. |
flushInterval |
Default set to 30 (seconds). The interval in seconds at which Segment flushes events. |
exceptionHandler |
Set a an exception handler to handle errors happened in async methods within the analytics scope |
storageProvider |
Set how you want your data to be stored. DefaultStorageProvider is used by default which stores data to local storage. InMemoryStorageProvider is also provided in the library. You can also write your own storage solution by implementing IStorageProvider and IStorage |
Once you've installed the mobile or server Analytics-CSharp library, you can start collecting data through Segment's tracking methods:
info "" For any of the different methods described, you can replace the properties and traits in the code samples with variables that represent the data collected.
The Identify method lets you tie a user to their actions and record traits about them. This includes a unique user ID and any optional traits you know about them like their email, name, address. The traits option can include any information you want to tie to the user. When using any of the reserved traits, be sure the information reflects the name of the trait. For example, email
should always be a string of the user's email address.
analytics.Identify("user-123", new JsonObject {
["username"] = "MisterWhiskers",
["email"] = "hello@test.com",
["plan"] = "premium"
});
The Track method lets you record the actions your users perform. Every action triggers an event, which also has associated properties that the track method records.
analytics.Track("View Product", new JsonObject {
["productId"] = 123,
["productName"] = "Striped trousers"
});
The Screen method lets you record whenever a user sees a screen in your mobile app, along with optional extra information about the page being viewed.
You'll want to record a screen event whenever the user opens a screen in your app. This could be a view, fragment, dialog or activity depending on your app.
Not all integrations support screen, so when it's not supported explicitly, the screen method tracks as an event with the same parameters.
analytics.Screen("ScreenName", new JsonObject {
["productSlug"] = "example-product-123"
});
The Page method lets you record whenever a user sees a page in your web app, along with optional extra information about the page being viewed.
You'll want to record a page event whenever the user opens a page in your app. This could be a webpage, view, fragment, dialog or activity depending on your app.
Not all integrations support page, so when it's not supported explicitly, the page method tracks as an event with the same parameters.
analytics.Page("PageName", new JsonObject {
["productSlug"] = "example-product-123"
});
The Group method lets you associate an individual user with a group— whether it's a company, organization, account, project, or team. This includes a unique group identifier and any additional group traits you may have, like company name, industry, number of employees. You can include any information you want to associate with the group in the traits option. When using any of the reserved group traits, be sure the information reflects the name of the trait. For example, email should always be a string of the user's email address.
analytics.Group("user-123", new JsonObject {
["username"] = "MisterWhiskers",
["email"] = "hello@test.com",
["plan"] = "premium"
});
Segment's plugin architecture enables you to modify and augment how the analytics client works. From modifying event payloads to changing analytics functionality, plugins help to speed up the process of getting things done.
Plugins are run through a timeline, which executes in order of insertion based on their entry types. Segment has these 5 entry types:
Type | Details |
---|---|
Before |
Executes before event processing begins. |
Enrichment |
Executes as the first level of event processing. |
Destination |
Executes as events begin to pass off to destinations. |
After |
Executes after all event processing completes. You can use this to perform cleanup operations. |
Utility |
Executes only with manual calls such as Logging. |
There are 3 basic types of plugins that you can use as a foundation for modifying functionality. They are: Plugin
, EventPlugin
, and DestinationPlugin
.
Plugin
acts on any event payload going through the timeline.
For example, if you want to add something to the context object of any event payload as an enrichment:
class SomePlugin : Plugin
{
public override PluginType Type => PluginType.Enrichment;
public override RawEvent Execute(RawEvent incomingEvent)
{
incomingEvent.Context["foo"] = "bar";
return incomingEvent;
}
}
EventPlugin
is a plugin interface that acts on specific event types. You can choose the event types by only overriding the event functions you want.
For example, if you only want to act on track
& identify
events:
class SomePlugin : EventPlugin
{
public override PluginType Type => PluginType.Enrichment;
public override IdentifyEvent Identify(IdentifyEvent identifyEvent)
{
// code to modify identify event
return identifyEvent;
}
public override TrackEvent Track(TrackEvent trackEvent)
{
// code to modify track event
return trackEvent;
}
}
The DestinationPlugin
interface is commonly used for device-mode destinations. This plugin contains an internal timeline that follows the same process as the analytics timeline, enabling you to modify and augment how events reach a particular destination.
For example, if you want to implement a device-mode destination plugin for Amplitude, you can use this:
class AmplitudePlugin : DestinationPlugin
{
public override string Key =>
"Amplitude"; // This is the name of the destination plugin, it is used to retrieve settings internally
private Amplitude amplitudeSDK: // This is an instance of the partner SDK
public AmplitudePlugin()
{
amplitudeSDK = Amplitude.instance;
amplitudeSDK.initialize(applicationContext, "API_KEY");
}
/*
* Implementing this function allows this plugin to hook into any track events
* coming into the analytics timeline
*/
public override TrackEvent Track(TrackEvent trackEvent)
{
amplitudeSDK.logEvent(trackEvent.Event);
return trackEvent;
}
}
configure(Analytics)
: Use this function to setup your plugin. This implicitly calls once the plugin registers.update(Settings)
: Use this function to react to any settings updates. This implicitly calls when settings update. You can force a settings update by callinganalytics.checkSettings()
.DestinationPlugin
timeline: The destination plugin contains an internal timeline that follows the same process as the analytics timeline, enabling you to modify/augment how events reach the particular destination. For example if you only wanted to add a context key when sending an event toAmplitude
:
class AmplitudeEnrichment : Plugin
{
public override PluginType Type => PluginType.Enrichment;
public override RawEvent Execute(RawEvent incomingEvent)
{
incomingEvent.Context["foo"] = "bar";
return incomingEvent;
}
}
var amplitudePlugin = new AmplitudePlugin(); // add amplitudePlugin to the analytics client
analytics.Add(amplitudePlugin);
amplitudePlugin.Add(new AmplitudeEnrichment()); // add enrichment plugin to amplitude timeline
Adding plugins enable you to modify your analytics implementation to best fit your needs. You can add a plugin using this:
var yourPlugin = new SomePlugin()
analytics.Add(yourPlugin)
Though you can add plugins anywhere in your code, it's best to implement your plugin when you configure the client.
Here's an example of adding a plugin to the context object of any event payload as an enrichment:
class SomePlugin : Plugin
{
public override PluginType Type => PluginType.Enrichment;
public override RawEvent Execute(RawEvent incomingEvent)
{
incomingEvent.Context["foo"] = "bar";
return incomingEvent;
}
}
var yourPlugin = new SomePlugin()
analytics.Add(yourPlugin)
See how different platforms and languages use Analytics-CSharp in different example projects.
The Analytics-CSharp utility methods help you work with plugins from the analytics timeline. They include:
There's also the Flush method to help you manage the current queue of events.
The Add method lets you add a plugin to the analytics timeline.
class SomePlugin : Plugin
{
public override PluginType Type => PluginType.Enrichment;
public override RawEvent Execute(RawEvent incomingEvent)
{
incomingEvent.Context["foo"] = "bar";
return incomingEvent;
}
}
var somePlugin = new SomePlugin();
analytics.Add(somePlugin);
The Find method lets you find a registered plugin from the analytics timeline.
var plugin = analytics.Find<SomePlugin>();
The Remove methods lets you remove a registered plugin from the analytics timeline.
analytics.remove(somePlugin);
The Flush method lets you force flush the current queue of events regardless of what the flushAt
and flushInterval
is set to.
analytics.Flush();
The reset
method clears the SDK’s internal stores for the current user and group. This is useful for apps where users log in and out with different identities on the same device over time.
analytics.Reset()
For samples usage of the SDK in specific platforms, checkout the following:
Platform | Sample |
---|---|
Asp.Net | Setup with dependency injection |
Asp.Net MVC | Setup with Dependency injection |
Console | Basic setups |
Unity | Singleton Analytics |
Lifecycle plugin | |
Custom HTTPClient | |
Xamarin | Basic setups |
This library targets .NET Standard 2.0
. Checkout here for compatible platforms.
View the Analytics-CSharp changelog on GitHub.
See the contributing guide to learn how to contribute to the repository and the development workflow.
Interested in integrating your service with us? Check out our Partners page for more details.
Before contributing, please also see our code of conduct.
MIT License
Copyright (c) 2021 Segment
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.