Analytics for CSharp (C#) Migration Guide



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

This library is currently in beta and is governed by Segment’s First Access and Beta terms.

  1. Create a source in Segment. If you want to reuse your current source, skip to step 2.
    1. Go to Connections > Sources > Add Source.
    2. Search for Xamarin, Unity or .NET and click Add source. Note: There is no CSharp source. To use Analytics-CSharp, use either Xamarin, Unity, or .NET as your source.
  2. 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>
    
  3. Modify your tracking methods.
    • Identify


      Before:

        Analytics.Client.Identify("019mr8mf4r", new Traits() {
            { "name", "#{ user.name }" },
            { "email", "#{ user.email }" },
            { "friends", 29 }
        });
      


      After:

        // compatible with the old way
        analytics.Identify("019mr8mf4r", new JsonObject()
        {
            { "name", "#{ user.name }" },
            { "email", "#{ user.email }" },
            { "friends", 29 }
        });
      
    • Track
      Before:
            Analytics.Client.Track("019mr8mf4r", "Item Purchased", new Properties() {
                { "revenue", 39.95 },
                { "shipping", "2-day" }
            });
      


      After:

        // compatible with the old way
        analytics.Track("Item Purchased", new JsonObject()
        {
            { "revenue", 39.95 },
            { "shipping", "2-day" }
        });
      

      Note: The Analytics-CSharp SDK remembers the identity info from the last identify call, so you don’t have to pass an identity every time. If you still want to identify on every track call, you can achieve it with Segment’s plugin system.

    • Page
      Before:
        Analytics.Client.Page("019mr8mf4r", "Login", new Properties() {
            { "path", "/login" },
            { "title", "Initech Login" }
        });
      


      After:

        // compatible with the old way
        analytics.Page("Login", new JsonObject()
        {
            { "path", "/login" },
            { "title", "Initech Login" }
        });
      
    • Screen
      Before:
        Analytics.Client.Screen("019mr8mf4r", "Register", new Properties() {
            { "type", "facebook" }
        });
      


      After:

        // compatible with the old way
        analytics.Screen("Register", new JsonObject()
        {
            { "type", "facebook" }
        });
      
    • Group
      Before:
        Analytics.Client.Group("userId", "groupId", new Traits() {
            { "name", "Initech, Inc." },
            { "website", "http://www.example.com" }
        });
      


      After:

        // compatible with the old way
        analytics.Group("groupId", new JsonObject()
        {
            { "name", "Initech, Inc." },
            { "website", "http://www.example.com" }
        });
      
    • Alias
      Before:
        Analytics.Client.Alias("previousId", "userId")
      


      After:

        analytics.Alias("newId");
      
  4. 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",
         userSynchronizeDispatcher: true);
     var analytics = new Analytics(configuration);
    
  5. 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();
    
  6. 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"
             }
         }
     };
    
  7. 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