Mixpanel for Windows Phone 8

A few weeks back I released a Windows Phone 8 version of Mixpanel’s API so WP8 developers could easily add analytics to their apps. The library is available as a NuGet package and can be found here: https://www.nuget.org/packages/Mixpanel/

The API relies on Mixpanel’s HTTP API which is documented here: https://mixpanel.com/help/reference/http

Here’s a set of examples illustrating how to use the library from a Windows Phone 8 app.

MixpanelClient

The main class is MixpanelClient, it’s that little fellow that allows you to track events and profile updates. Furthermore, if ever sending an element failed, the client will automatically save it locally to automatically send them as a batch later on.

Here’s how to retrieve an instance of it:

_client = await MixpanelClient.GetCurrentClient();

This method is awaitable as on the first call, the client checks if there’s any unsent element and sends them if so.

Tracking events

Once you have an instance of MixpanelClient, here’s how to track events:

Example 1

TrackingEvent evt = new TrackingEvent("Signed Up");
evt.Properties = new TrackingEventProperties(Token);
evt.Properties.DistinctId = "13793";
evt.Properties.All["Referred by"] = "Friend";
await _client.Track(evt);

Example 2

TrackingEvent levelComplete = new TrackingEvent("Level Complete");
levelComplete.Properties = new TrackingEventProperties(Token);
levelComplete.Properties.DistinctId = "13793";
levelComplete.Properties.Time = MixpanelClient.ToEpochTime(DateTime.Now);
levelComplete.Properties.IP = "203.0.113.9";
levelComplete.Properties.Tag = "Tim Trefren";
levelComplete.Properties.All["Level Number"] = 9;
await _client.Track(levelComplete);

Profile updates

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Set);
pu.IP = "123.123.123.123";
pu.OperationValues["Address"] = "1313 Mockingbird Lane";
pu.OperationValues["Birthday"] = "1948-01-01";
await _client.Track(pu);

Set

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Set);
pu.IP = "123.123.123.123";
pu.OperationValues["Address"] = "1313 Mockingbird Lane";
pu.OperationValues["Birthday"] = "1948-01-01";
await _client.Track(pu);

Set Once

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.SetOnce);
pu.OperationValues["First login date"] = MixpanelClient.ConvertToMixpanelDate(DateTime.Now);
await _client.Track(pu);

Add

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Add);
pu.OperationValues["Coins gathered"] = 12;
await _client.Track(pu);

Append

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Append);
pu.OperationValues["Power Ups"] = "Bubble Lead";
await _client.Track(pu);

Union

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Union);
pu.OperationValues["Items purchased"] = new List() { "socks", "shirts" };
await _client.Track(pu);

Unset

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Unset);
pu.UnsetValueList = new List() { "Days overdue" };
await _client.Track(pu);

Delete

ProfileUpdate pu = new ProfileUpdate(Token, "13793", ProfileUpdateOperation.Delete);
await _client.Track(pu);

 

Hope this helps,

Carl