diff --git a/Mixpanel.NET.nuspec b/Mixpanel.NET.nuspec index b194ce8..7520164 100644 --- a/Mixpanel.NET.nuspec +++ b/Mixpanel.NET.nuspec @@ -2,7 +2,7 @@ Mixpanel.NET - 0.2.0 + 0.2.1 Mixpanel API integration for .NET Chris Nicola Chris Nicola diff --git a/src/Mixpanel.NET/Engage/MixpanelEngage.cs b/src/Mixpanel.NET/Engage/MixpanelEngage.cs index ca5641d..a1cf2a9 100644 --- a/src/Mixpanel.NET/Engage/MixpanelEngage.cs +++ b/src/Mixpanel.NET/Engage/MixpanelEngage.cs @@ -28,9 +28,9 @@ private bool Engage(string distinctId, IDictionary setProperties var dictionary = new Dictionary {{"$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); diff --git a/src/Mixpanel.NET/Events/MixpanelTracker.cs b/src/Mixpanel.NET/Events/MixpanelTracker.cs index a668bf3..ba2b503 100644 --- a/src/Mixpanel.NET/Events/MixpanelTracker.cs +++ b/src/Mixpanel.NET/Events/MixpanelTracker.cs @@ -27,26 +27,18 @@ public MixpanelTracker(string token, IMixpanelHttp http = null, TrackerOptions o public bool Track(string @event, IDictionary properties) { - var propertyBag = new Dictionary(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(); - var data = - new JavaScriptSerializer() - .Serialize(new Dictionary - { - { "event", @event }, - { "properties", propertyBag } - }); + var data = new JavaScriptSerializer().Serialize(new Dictionary + { + { "event", @event }, + { "properties", propertyBag } + }); var values = "data=" + data.Base64Encode(); diff --git a/src/Mixpanel.NET/MixpanelExtension.cs b/src/Mixpanel.NET/MixpanelExtension.cs index 3e91d9a..ad26e73 100644 --- a/src/Mixpanel.NET/MixpanelExtension.cs +++ b/src/Mixpanel.NET/MixpanelExtension.cs @@ -48,5 +48,25 @@ public static string ComputeHash(this string value) { var hexDigest = hash.Aggregate("", (x,y) => x + y.ToString("X").ToLower()); return hexDigest; } + + /// + /// 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 + /// + public static string FormatDate(this DateTime value) { + return value.ToString("s"); + } + + public static IDictionary FormatProperties(this IDictionary values) { + var output = new Dictionary(); + 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; + } } } \ No newline at end of file