Skip to content
Aleksandr Ivanov edited this page Mar 8, 2019 · 5 revisions

mixpanel-csharp works well out of the box but it also provides a set of configuration options. Configuration is done using MixpanelConfig class.

Ways to configure

You can set configuration options in two ways:

Global config

Using static MixpanelConfig.Global instance. Because it's static you can call it whenever you want in your code, but usually it will be done in some startup method.

// Example: set Json.NET to be default serializer globally
MixpanelConfig.Global.SerializeJsonFn = JsonConvert.SerializeObject;

Local instance config

You can pass an instance of MixpanelConfig to MixpanelClient constructor.

// Example: set Json.NET to be a serializer only for specific instance
var config = new MixpanelConfig();
config.SerializeJsonFn = JsonConvert.SerializeObject;
var client = new MixpanelClient(token, config);

Choosing config source

Every time mixpanel-csharp needs to get some object/method which can be configured, it chooses it based on priority.

Global config Local config Action
SET SET Local config will be used because it has greater priority than global config
SET NOT SET Global config will be used because local config is not set
NOT SET SET Local config will be used because it has greater priority than default implementation
NOT SET NOT SET Default implementation will be used because neither global or local configs are set
NB! If you pass a local config to MixpanelClient it will NOT override all global config properties, but only those that are set in local config.

Configuration options

JSON Serializer

mixpanel-csharp uses built-in JSON serializer by default.

// Example: set Json.NET to be default serializer globally
MixpanelConfig.Global.SerializeJsonFn = JsonConvert.SerializeObject;

An internal structure for holding message data in mixpanel-csharp is IDictionary<string, object> that contains nested dictionaries. So if you provide your custom serializer please be sure that it handles nested dictionaries well (for example DataContractSerializer can't do that properly).

HTTP POST Method

All HTTP requests in mixpanel-csharp are done only with POST method. You can provide your own implementation, if by some reason deafult HTTP POST method is not good for you.

MixpanelConfig.Global.HttpPostFn = (url, stringContent) =>
{
    // TODO: Your HTTP POST method
    return true;
};

MixpanelConfig.Global.AsyncHttpPostFn = async (url, stringContent) =>
{
    // TODO: Your async HTTP POST method
    return true;
};

Error Logging

All public methods in mixpanel-csharp catch all exceptions and if you do not configure error logging you never know if something is wrong.

// Example: provide custom error log method
public void LogMixpanelErrors(string message, Exception exception)
{
  // Error logging code...
}

MixpanelConfig.Global.ErrorLogFn = LogMixpanelErrors;

Custom Property Name Formatting

You can control how property names are formatted.

// Example: set property name format to be 'TitleCase'
MixpanelConfig.Global.MixpanelPropertyNameFormat = MixpanelPropertyNameFormat.TitleCase;

There are following formatting options:

Format Description
None No formatting is applied. This is a default option.
SentenceCase Property name will be parsed in sentence with only first word capitalized.
"VeryLongProperty" -> "Very long property"
TitleCase Property name will be parsed in sentence with all words capitalized.
"VeryLongProperty" -> "Very Long Property"
LowerCase Property name will be parsed in sentence with no words capitalized.
"VeryLongProperty" -> "very long property"

NB! If class property has DataMember attribute with Name property set or MixpanelName attribute, then property name formatting is ignored. Property name formatting is also not applied to dictionaries.

IP Address Handling

By default ip query string parameter is not added to URL when making requests to Mixpanel API. If you need to add that then set it to one of the possible values from MixpanelIpAddressHandling enumeration.

// Example: Always add ´ip=1´ to query string
MixpanelConfig.Global.IpAddressHandling = MixpanelIpAddressHandling.UseRequestIp;