Skip to content
Aleksandr Ivanov edited this page Aug 10, 2015 · 10 revisions

##Getting started ####Create an instance of MixpanelClient There are two constructors in MixpanelClient class:

MixpanelClient(string token, MixpanelConfig config = null, object superProperties = null)
MixpanelClient(MixpanelConfig config = null, object superProperties = null)

Mostly you will use a first option because token is a required property for all messages. It can be avoided if you use only Send method, because in this case token is already contained in message. For now we will not provide config and superProperties parameters. You can read about them in corresponding wiki pages.

IMixpanelClient mc = MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");

Notice that mc is of type IMixpanelClient. It's often better to use interface which allows to have loose coupling and test code more easily. ####Configuration This step is optional. Default JSON serializer in mixpanel-csharp is JavaScriptSerializer. It was chosen to keep mixpanel-csharp dependency free and it's highly recommended to substitute it with something more appropriate. In this example we will use Json.NET:

MixpanelConfig.Global.SerializeJsonFn = JsonConvert.SerializeObject;

You might also want to configure error logging because otherwise all errors will disappear in void. We will use NLog.

private static readonly Logger MixpanelLogger = LogManager.GetLogger("Mixpanel");

MixpanelConfig.Global.ErrorLogFn = 
    (message, exception) => MixpanelLogger.Error(exception, message);

That's enough for initial configuration. Read more at Configuration wiki page. ####Send messages to Mixpanel For each message type there is a set of methods. The most simple option is to send a message immediately:

await mc.TrackAsync("Level Complete", new 
{ 
    DistinctId = "12345",
    Time = new DateTime(2013, 9, 26, 22, 33, 44, DateTimeKind.Utc),
    LevelNumber = 5
});

This will send the following JSON to Mixpanel:

{
    "event": "Level Complete",
    "properties": {
      "token": "e3bc4100330c35722740fb8c6f5abddc",
      "distinct_id": "12345",
      "time": 1380234824,
      "LevelNumber": 5
    }
}

As you can see mixpanel-csharp made all dirty work for you. DistinctId is renamed to distinct_id, Time value is converted to UNIX time.

Sending messages like that might be not the best option. It really depends on architecture of your system, but generally it's good to send messages in separate process (or from separate server) so it will not affect the performance of important things.

For that we will use Get*Message and Send methods. In the next example we will generate the message and add it to some queue and then some other process will read messages from queue and actually send the messages. So your client code might look like that:

// Get message but not send it yet
MixpanelMessage msg = mc.GetTrackMessage("Level Complete", new 
{ 
    DistinctId = "12345",
    Time = new DateTime(2013, 9, 26, 22, 33, 44, DateTimeKind.Utc),
    LevelNumber = 5
});

// Pseudo code of adding message to queue
QueueManager.Publish(msg);

And then in some other process:

// No need to provide token because we use only 'Send' 
// method and messages contain token already
IMixpanelClient mc = MixpanelClient();

// Pseudo code for getting message from queue
QueueManager.Received += (msg) => 
{
    SendResult result = await mc.SendAsync(msg);
};

Send method can also be used to send batch messages. So when you call this code:

MixpanelMessage msg1 = mc.GetTrackMessage("Bonus Gained", new 
{ 
    DistinctId = "12345",
    BonusName = "Booster"
});

MixpanelMessage msg2 = mc.GetTrackMessage("Level Up", new 
{ 
    DistinctId = "12345",
    Level = 3
});

MixpanelMessage msg3 = mc.GetPeopleSetMessage(new 
{ 
    DistinctId = "12345",
    Level = 3
});

SendResult result = await mc.SendAsync(msg1, msg2, msg3);

There are three messages but only two requests will be performed.

One will be sent to http://api.mixpanel.com/track/:

[
  {
    "event": "Bonus Gained",
    "properties": {
      "token": "e3bc4100330c35722740fb8c6f5abddc",
      "distinct_id": "12345",
      "BonusName": "Booster"
    }
  },
  {
    "event": "Level Up",
    "properties": {
      "token": "e3bc4100330c35722740fb8c6f5abddc",
      "distinct_id": "12345",
      "Level": 3
    }
  }
]

And another to http://api.mixpanel.com/engage/:

[
  {
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$set": {
      "Level": 3
    }
  }
]
Clone this wiki locally