The RudderStack .NET SDK lets you track your customer event data from your .NET applications and send it to your specified destinations via RudderStack.

Check the GitHub codebase to get a more hands-on understanding of the SDK.

Github Badge

SDK setup requirements

To set up the RudderStack .NET SDK, the following prerequisites must be met:

  • You will need to set up a RudderStack account.
  • Once signed up, set up a .NET source in the dashboard. For more information, see Adding a source. You should then see a Write Key for this source, as shown below:
.NET source write key
  • You will also need a data plane URL. Follow this section for more information on the data plane URL and where to get it.

Installing the .NET SDK

You can use NuGet to install the .NET SDK into your project.

Install-Package RudderAnalytics -Version <version>

Our library depends on Newton.JSON library for JSON processing.

Initializing the RudderStack client

To initialize the RudderStack client, run the following code snippet:

using RudderStack;
RudderAnalytics.Initialize(
WRITE_KEY,
new RudderConfig(dataPlaneUrl: DATA_PLANE_URL)
);

Sending events from RudderStack

Once the RudderStack client is initialized, you can then use it to send relevant customer events.

A sample track call is as shown:

RudderAnalytics.Client.Track(
"userId",
"CTA Clicked",
new Dictionary<string, object> { {"plan", "premium"}, }
);

Identify

The identify call lets you associate a user to their actions as well as captures the relevant traits or properties related to that user.

A sample identify call is shown below:

RudderAnalytics.Client.Identify(
"userId",
new Dictionary<string, object> { {"subscription", "inactive"}, }
);

The identify method parameters are as described below:

FieldTypePresenceDescription
userIdStringRequired, unless anonymousId is setUnique identifier for a particular user in your database.
traitsObjectOptionalDictionary of the traits associated with the user, such as nameor email .
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

Track

The track call lets you record the user actions along with their associated properties. Each user action is called an event.

A sample track call is shown below:

RudderAnalytics.Client.Track(
"userId",
"CTA Clicked",
new Dictionary<string, object> { {"plan", "premium"}, }
);

The track method parameters are:

NameTypePresenceDescription
userIdStringRequired, unless anonymousId is setThe developer identification for your user.
eventNameStringRequiredName of the event being performed by the user.
propertiesObjectOptionalDictionary of the properties associated with a particular event.
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

Page

The page call allows you to record the page views on your website along with the other relevant information about the viewed page.

A sample page call is as shown:

RudderAnalytics.Client.Page(
"userId",
"Sign Up",
new Dictionary<string, object> { {"url", "https://wwww.example.com/sign-up"}, }
);

The page method parameters are as described below:

FieldTypePresenceDescription
userIdStringRequired, unless anonymousId is setUnique identifier for a particular user in your database.
nameStringRequiredName of the page being viewed.
categoryStringOptionalThe category of the page.
propertiesObjectOptionalDictionary of the properties associated with the page being viewed, such as url and referrer.
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

Screen

The screen call is the mobile equivalent of the page call. It allows you to record the screen views on your mobile app along with the other relevant information about the app screen.

A sample screen call is as shown:

RudderAnalytics.Client.Screen(
"userId",
"Dashboard",
new Dictionary<string, object> { {"name", "Paid Dashboard"}, }
);

The screen method parameters are as described below:

FieldTypePresenceDescription
userIdStringRequired, unless anonymousId is setUnique identifier for a particular user in your database.
nameStringRequiredName of the screen being viewed.
categoryStringOptionalThe category of the screen.
propertiesObjectOptionalDictionary of the properties associated with the page being viewed, such as url and referrer.
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

Group

The group call lets you associate an identified user to a group - either a company, project, or a team, and record any custom traits or properties associated with that group.

A sample group call is as shown:

RudderAnalytics.Client.Group(
"userId",
"accountId",
new Dictionary<string, object> { {"role", "Owner"}, }
);

The group method parameters are as follows:

FieldTypePresenceDescription
userIdStringRequired, unless anonymousId is setUnique identifier for a particular user in your database.
groupIdStringRequiredUnique identifier of the group, as present in your database.
traitsObjectOptionalDictionary of the properties or traits associated with the group, such as email or name.
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

Alias

The alias call allows you to associate one identity with another.

alias is an advanced method that lets you change the tracked user's ID explicitly. This method is useful when managing identities for some of the downstream destinations.

For a detailed explanation of the alias call, refer to the RudderStack API Specification guide.

A sample alias call is as shown:

RudderAnalytics.Client.Alias("anonUserId", "userId");

The alias method parameters are as mentioned below:

FieldTypePresenceDescription
previousIdStringRequiredThe previous unique identifier of the user.
userIdStringRequired, unless anonymousId is setUnique identifier for a particular user in your database.
optionsObjectOptionalObject containing anonymousId, integrations, timestamp, and context.

Refer to The options parameter section below for more information on the options object and its fields.

The options parameter

The options object contains the following fields:

FieldTypePresenceDescription
anonymousIdStringOptionalSets the user ID for cases where there is no unique identifier for the user. Either userId or anonymousId is required.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.
timestampDateOptionalThe timestamp of the message's arrival.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.

Flushing events

To make sure no events are left in the queue, you can flush the events explicitly by using the SDK's flush() method, as shown:

RudderAnalytics.Client.Flush();

Calling flush() again is blocked until all the messages are flushed from the queue.

Logging

The RudderStack .NET SDK supports detailed logging. You can enable this feature as shown:

using RudderStack;
Logger.Handlers += LoggingHandler;
static void LoggingHandler(Logger.Level level, string message, IDictionary<string, object> args)
{
if (args != null)
{
foreach (string key in args.Keys)
{
message += String.Format(" {0}: {1},", "" + key, "" + args[key]);
}
}
Console.WriteLine(String.Format("[RudderAnalytics] [{0}] {1}", level, message));
}

The logger must be on a minimum version of .NET Core 2.1.

Contact us

For queries on any of the sections covered in this guide, you can contact us or start a conversation in our Slack community.

If you come across any issues while using the .NET SDK, you can open a new issue on our GitHub Issues page.

Contents