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

Refer to the SDK's GitHub codebase for the implementation-specific details.
Github Badge

SDK setup requirements

  1. Sign up to RudderStack Cloud.
  2. Set up a .NET source in your dashboard. You should be able to see a write key for this source, as shown:
.NET source write key

You will also need a data plane URL. Refer to the Dashboard Overview guide for more information on the data plane URL and where to find it.

Installing the .NET SDK

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

Install-Package RudderAnalytics -Version 2.0.0
The SDK uses the Newton.JSON library for JSON processing.

Initializing the SDK

To initialize the SDK, run the following code snippet:

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

Gzipping requests

The Gzip feature is enabled by default in the .NET SDK from version `2.0.0`.

The .NET SDK automatically gzips requests. However, you can disable this by setting the gzip parameter of RudderConfig to false while initializing the SDK, as shown:

using RudderStack;
RudderAnalytics.Initialize(
WRITE_KEY,
new RudderConfig(dataPlaneUrl: DATA_PLANE_URL, gzip: false)
);
Gzip requires rudder-server v1.4 or higher. Otherwise, your events might fail.

Sending events

RudderStack does not store or persist the user state in any of the server-side SDKs.

Unlike the client-side SDKs that deal with only a single user at a given time, the server-side SDKs deal with multiple users simultaneously. Therefore, you must specify either the userId or anonymousId every time while making any API calls supported by the .NET SDK.

Identify

The identify call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.

A sample identify call made using the .NET SDK is shown below:

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

The identify method parameters are as described below:

FieldTypeDescription
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
traitsObjectAn optional dictionary of the user's traits like name or email.
optionsObjectObject 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(
"1hKOmRA4GRlm",
"CTA Clicked",
new Dictionary<string, object> { {"plan", "premium"}, }
);

The track method parameters are as described below:

FieldTypeDescription
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
event
Required
StringName of the event.
propertiesObjectAn optional dictionary of the properties associated with the event.
optionsObjectObject 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 lets you record the page views on your application along with the other relevant information about the page.

A sample page call is as shown:

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

The page method parameters are as described below:

FieldTypeDescription
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
name
Required
StringName of the viewed page.
categoryStringCategory of the viewed page.
propertiesObjectAn optional dictionary of the properties associated with the viewed page, like url or referrer.
optionsObjectObject 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 lets you record the screen views on your mobile app along with other relevant information about the screen.

A sample screen call is as shown:

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

The screen method parameters are as described below:

FieldTypeDescription
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
name
Required
StringName of the viewed screen.
categoryStringCategory of the viewed screen.
propertiesObjectAn optional dictionary of the properties associated with the viewed screen, like url or referrer.
optionsObjectObject 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 link an identified user with a group, such as a company, organization, or an account. It also lets you record any custom traits or properties associated with that group.

A sample group call made using the .NET SDK is shown below:

RudderAnalytics.Client.Group(
"1hKOmRA4GRlm",
"12",
new Dictionary<string, object> { {"role", "Owner"}, }
);

The group method parameters are as follows:

FieldTypeDescription
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
groupId
Required
StringUnique identifier of the group in your database.
traitsObjectAn optional dictionary of the group's traits like nameor email.
optionsObjectObject 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 lets you merge different identities of a known user. It is an advanced method that lets you change the tracked user's ID explicitly. You can use alias for managing the user's identity in some of the downstream destinations.

RudderStack supports sending alias events only to select downstream destinations. Refer to the destination-specific documentation for more details.

A sample alias call is as shown:

RudderAnalytics.Client.Alias("1hKOmRA4GRlm", "12345");

The alias method parameters are as mentioned below:

FieldTypeDescription
previousId
Required
StringThe previous unique identifier of the user.
userId
Required, if anonymousId is absent.
StringUnique identifier for a user in your database.
optionsObjectObject containing anonymousId, integrations, timestamp, and context.
Refer to the options parameter section below for more information on the options object and its fields.

options parameter

FieldTypeDescription
anonymousId
Required, if userId is absent.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
timestampTimestamp in ISO 8601 formatThe timestamp of the event's arrival.
contextObjectAn optional dictionary of information that provides context about the event. 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.

RudderAnalytics.Client.Flush();
You cannot call the flush() method again until all the messages are flushed from the queue.

Logging

The .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 more information on the topics covered on this page, email us or start a conversation in our Slack community.

On this page