Skip to content
Aleksandr Ivanov edited this page Nov 11, 2022 · 5 revisions

Most mixpanel-csharp methods that deal with messages has a properties parameter of type object. mixpanel-csharp do not force you to use some specific type so you can choose the format that better suits your needs.

Valid types for properties parameter

Dictionary

Just provide an object that implements IDictionary (or preferably IDictionary<string, object>):

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.TrackAsync("Level Complete", new Dictionary<string, object>
    {
        {"DistinctId", "12345"},
        {"Level Number", 5},
        {"Level Name", "First Dungeon"}
    });

You can also use constants from MixpanelProperty as keys.

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.PeopleSetAsync(new Dictionary<string, object>
    {
        {MixpanelProperty.DistinctId, "12345"},
        {MixpanelProperty.FirstName, "Darth"},
        {MixpanelProperty.LastName, "Vader"},
    });

NB! If you use dictionaries, then rules for property name formatting will be not applied.

Anonymous class

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.TrackAsync("Level Complete", new
    {
        DistinctId = "12345",
        LevelNumber = 5,
        LevelName = "First Dungeon"
    });

Normal class

class LevelCompletionInfo 
{
    public string DistinctId { get; set; }
    public int LevelNumber { get; set; }
    public string LevelName { get; set; }
}

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.TrackAsync("Level Complete", new LevelCompletionInfo
    {
        DistinctId = "12345",
        LevelNumber = 5,
        LevelName = "First Dungeon"
    });

One of the advantages of using normal classes is that you can add attributes to properties. mixpanel-csharp respects DataContract, DataMember and IgnoreDataMember attributes. There is also MixpanelName attribute. With use of these attributes you can do a lot of things:

[DataContract]
class LevelCompletionInfo 
{
    [MixpanelName(MixpanelProperty.DistinctId)]
    [DataMember(Name = "user_id")]
    public string UserId { get; set; }
    
    [DataMember(Name = "level_number")]
    public int LevelNumber { get; set; }
    
    [MixpanelName("Level Name")]
    [DataMember(Name = "level_name")]
    public string LevelName { get; set; }
    
    [IgnoreDataMember]
    public string IgnoredProperty { get; set; }
    
    public decimal AnotherIgnoredProperty { get; set }
}

How such class will be parsed?

Property Name Parse process
UserId Has two attributes but MixpanelName has greater priority than DataMember, so the value will be MixpanelProperty.DistinctId
LevelNumber DataMember will be used, so the value will be "level_number"
LevelName MixpanelName will be used, the value will be "Level Name"
IgnoredProperty Ignored because of IgnoreDataMember attribute
AnotherIgnoredProperty ignored because it's not a part of DataContract

Supported data types

Type Description
All value types except structs Check the list of C# value types
String Passed as is
DateTime First converted to UTC (if needed). Then converted to string using Mixpanel date format 'YYYY-MM-DDThh:mm:ss' or to unix time.
TimeSpan Converted to string using the default ToString() method
Guid Converted to string using the default ToString() method
IPAddress Converted to string using the default ToString() method
IEnumerable Converted to Mixpanel list, if items are of supported types listed above