-
Notifications
You must be signed in to change notification settings - Fork 15
Home
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 first option because token
is a required property for all messages. Second constructor should be used when you are using only SendAsync
method, because in this case token
is already set in the messages. For now we will not provide config
and superProperties
parameters. You can read about them in the corresponding wiki pages.
IMixpanelClient mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
There are rich configuration options in mixpanel-csharp
but the most obvious one is to configure the logging of the errors. Here is an example for NLog.
private static readonly Logger MixpanelLogger = LogManager.GetLogger("Mixpanel");
MixpanelConfig.Global.ErrorLogFn =
(message, exception) => MixpanelLogger.Error(exception, message);
Read more at Configuration wiki page.
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",
LevelNumber = 5,
Duration = TimeSpan.FromMinutes(1)
});
This will send the following JSON to Mixpanel:
{
"event": "Level Complete",
"properties": {
"token": "e3bc4100330c35722740fb8c6f5abddc",
"distinct_id": "12345",
"LevelNumber": 5,
"$duration": 60
}
}
As you can see mixpanel-csharp
made all dirty work for you. DistinctId
is renamed to distinct_id
, Duration
value is renamed to $duration
and converted to seconds.
Sending messages like that might be not the best option, but it really depends on the architecture of your system. mixpanel-csharp
gives you an alternative option. You can first generate the messages and then send them from another thread or maybe even from another machine.
For that we will use Get*Message and SendAsync 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 the queue and actually send the messages. So your client code might look like that:
var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
// Get message but not send it yet (token will be in the message)
MixpanelMessage msg = mc.GetTrackMessage("Level Complete", new
{
DistinctId = "12345",
LevelNumber = 5,
Duration = TimeSpan.FromMinutes(1)
});
// 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 'SendAsync'
// 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(new [] { msg1, msg2, msg3 });
There are three messages but only two requests will be performed.
One will be sent to https://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 https://api.mixpanel.com/engage/
:
[
{
"$token": "e3bc4100330c35722740fb8c6f5abddc",
"$distinct_id": "12345",
"$set": {
"Level": 3
}
}
]