RudderStack's .NET SDK lets you track your customer event data from your .NET applications and send it to your specified destinations via RudderStack.
SDK setup requirements
- Sign up to RudderStack Cloud.
- Set up a .NET source in your dashboard. You should be able to see a write key for this source, as shown:
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
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 .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));
Sending events
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:
Field | Type | Description |
---|---|---|
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
traits | Object | An optional dictionary of the user's traits like name or email . |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
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:
Field | Type | Description |
---|---|---|
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
event Required | String | Name of the event. |
properties | Object | An optional dictionary of the properties associated with the event. |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
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:
Field | Type | Description |
---|---|---|
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
name Required | String | Name of the viewed page. |
category | String | Category of the viewed page. |
properties | Object | An optional dictionary of the properties associated with the viewed page, like url or referrer . |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
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:
Field | Type | Description |
---|---|---|
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
name Required | String | Name of the viewed screen. |
category | String | Category of the viewed screen. |
properties | Object | An optional dictionary of the properties associated with the viewed screen, like url or referrer . |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
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:
Field | Type | Description |
---|---|---|
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
groupId Required | String | Unique identifier of the group in your database. |
traits | Object | An optional dictionary of the group's traits like name or email . |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
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.
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:
Field | Type | Description |
---|---|---|
previousId Required | String | The previous unique identifier of the user. |
userId Required, if anonymousId is absent. | String | Unique identifier for a user in your database. |
options | Object | Object containing anonymousId , integrations , timestamp , and context . |
options
object and its fields.options
parameter
Field | Type | Description |
---|---|---|
anonymousId Required, if userId is absent. | String | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
timestamp | Timestamp in ISO 8601 format | The timestamp of the event's arrival. |
context | Object | An 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();
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));}
Contact us
For more information on the topics covered on this page, email us or start a conversation in our Slack community.