Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Fixes issue #5 problems with DateTime format for Mixpanel #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mixpanel.NET.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Mixpanel.NET</id>
<version>0.2.0</version>
<version>0.2.1</version>
<title>Mixpanel API integration for .NET</title>
<authors>Chris Nicola</authors>
<owners>Chris Nicola</owners>
Expand Down
4 changes: 2 additions & 2 deletions src/Mixpanel.NET/Engage/MixpanelEngage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ private bool Engage(string distinctId, IDictionary<string, object> setProperties
var dictionary =
new Dictionary<string, object> {{"$token", token}, {"$distinct_id", distinctId}};

if (setProperties != null) dictionary.Add("$set", setProperties);
if (setProperties != null) dictionary.Add("$set", setProperties.FormatProperties());

if (incrementProperties != null) dictionary.Add("$add", incrementProperties);
if (incrementProperties != null) dictionary.Add("$add", incrementProperties.FormatProperties());

var data = new JavaScriptSerializer().Serialize(dictionary);

Expand Down
24 changes: 8 additions & 16 deletions src/Mixpanel.NET/Events/MixpanelTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,18 @@ public MixpanelTracker(string token, IMixpanelHttp http = null, TrackerOptions o

public bool Track(string @event, IDictionary<string, object> properties)
{
var propertyBag = new Dictionary<string, object>(properties);
var propertyBag = properties.FormatProperties();
// Standardize token and time values for Mixpanel
propertyBag["token"] = token;
if (_options.SetEventTime)
{
propertyBag["time"] =
propertyBag.Where(x => x.Key.ToLower() == "time")
.Select(x => x.Value)
.FirstOrDefault() ?? DateTime.UtcNow;

propertyBag.Remove("Time");
}
if (_options.SetEventTime && !properties.Keys.Any(x => x.ToLower() == "time"))
propertyBag["time"] = DateTime.UtcNow.FormatDate();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code would rename Time to time whereas the new code doesn't. I suspect that was done because mix panel is case sensitive on that property (but that's just a guess).


var data =
new JavaScriptSerializer()
.Serialize(new Dictionary<string, object>
{
{ "event", @event },
{ "properties", propertyBag }
});
var data = new JavaScriptSerializer().Serialize(new Dictionary<string, object>
{
{ "event", @event },
{ "properties", propertyBag }
});

var values = "data=" + data.Base64Encode();

Expand Down
20 changes: 20 additions & 0 deletions src/Mixpanel.NET/MixpanelExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,25 @@ public static string ComputeHash(this string value) {
var hexDigest = hash.Aggregate("", (x,y) => x + y.ToString("X").ToLower());
return hexDigest;
}

/// <summary>
/// Converts the time to "sortable" format which MixPanel understands
/// https://mixpanel.com/docs/properties-or-segments/property-data-types
/// http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
/// </summary>
public static string FormatDate(this DateTime value) {
return value.ToString("s");
}

public static IDictionary<string, object> FormatProperties(this IDictionary<string, object> values) {
var output = new Dictionary<string, object>();
foreach (var prop in values) {
if (prop.Value is DateTime)
output[prop.Key] = ((DateTime)prop.Value).FormatDate();
else
output[prop.Key] = prop.Value;
}
return output;
}
}
}