Analytics-CSharp (C#) Migration Guide


If you’re currently using Analytics.NET or Analytics.Xamarin to send data to Segment, please follow the steps below to migrate to the Analytics-CSharp library.

Analytics-C# does not include v1 as part of the url address. If you are using the Segment EU endpoint, you will have to pass it manually. For instance, events.eu1.segmentapis.com/v1

You can update to Analytics-CSharp in 3 steps:

  1. Bundle Analytics-CSharp into your app and remove your previous SDK.
  2. Change the namespaces.
  3. (Optional) Use Reset to stay anonymous.

Migration steps

  1. Add the Analytics-CSharp dependency to your project.


    Before:

     dotnet add package Analytics --version <VERSION>
    


    Before (for Xamarin users only):

     git clone https://github.com/segmentio/Analytics.Xamarin.git
    


    After:

     dotnet add package Segment.Analytics.CSharp --version <VERSION>
    
  2. Replace namespaces.


    Before:

         using Segment;
         using Segment.Flush;
         using Segment.Model;
         using Segment.Request;
    


    After:

         using Segment.Analytics;
         using Segment.Analytics.Compat;
    
  3. (Optional) Update calls that resets the anonymous ID.

    The old SDK requires you to provide the anonymous ID. The new SDK generates an Anonymous ID for you if you never call analytics.Identify. If you call Identify and want to go back to anonymous, the new SDK provides a Reset function to achieve that.


    Before:

       Analytics.Client.Page(null, "Login", new Properties(), new Options()
       .SetAnonymousId("some-id"));
    


    After:

       analytics.Reset();
    

Optional changes for unit tests

Change your development settings if you would like to make analytics run synchronously for testing purposes.


Before:

  Analytics.Initialize("YOUR_WRITE_KEY", new Config().SetAsync(false));


After:

  var configuration = new Configuration("YOUR WRITE KEY",
    useSynchronizeDispatcher: true);
  var analytics = new Analytics(configuration);

This page was last modified: 01 Feb 2024



Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account