Analytics-CSharp (C#) Migration Guide


If you’re using a different library, follow the steps below to migrate to the Analytics-CSharp library.

Start the Migration

  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;
    

Optional Changes

  1. 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);
    
  2. Review your anonymous ID settings.


    Before:

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

    The new SDK by default, generates an Anonymous ID for you if you never call analytics.Identify. If you’ve called Identify and want to go back to anonymous, try:


    After:

     analytics.Reset();
    
  3. Change your nested properties settings.


    Before:

     Analytics.Client.Identify("hj2kf92ds212", new Traits() {
         { "email", "tom@example.com" },
         { "name", "Tom Smykowski" },
         { "address", new Dict() {
             { "street", "123 Fake Street" },
             { "city", "Boston" }
         }}
      });
    


    After:

     // compatbile with the old way
     analytics.Identify("hj2kf92ds212", new JsonObject()
     {
         { "email", "tom@example.com" },
         { "name", "Tom Smykowski" },
         { "address", new JsonObject() {
             { "street", "123 Fake Street" },
             { "city", "Boston" }
         }}
     });
    

    The new SDK internally implements a flexible JSON builder (Serialization.NET), that allows you build a complex JSON payload:

     var jsonObject = new JsonObject
     {
         ["int"] = 1,
         ["float"] = 1f,
         ["long"] = 1L,
         ["double"] = 1.0,
         ["string"] = "1",
         ["bool"] = true,
         ["object"] = new JsonObject
         {
             ["another object"] = "obj"
         },
         ["array"] = new JsonArray
         {
             1, 1f, 1L, 1.0, "1", true, new JsonObject
             {
                 ["object in array"] = "obj"
             }
         }
     };
    
  4. Review your Flush settings.


    Before:

      Analytics.Client.Flush();
    


    After:

     analytics.Flush();
    

This page was last modified: 12 Sep 2023



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