Exemple #1
0
        /// <summary>
        /// Track a new event
        /// </summary>
        /// <returns>The response from mixpanel</returns>
        /// <param name="profile">Profile.</param>
        /// <param name="eventName">Event name.</param>
        /// <param name="properties">Properties</param>
        public Task <MixpanelResponse> Track(Profile profile, string eventName,
                                             Dictionary <string, object> properties)
        {
            if (Client.Token == null)
            {
                throw new Exception("Cannot track events without initializing Client first");
            }

            if (eventName == null)
            {
                throw new ArgumentNullException(nameof(eventName));
            }

            if (properties == null)
            {
                properties = new Dictionary <string, object> ();
            }

            properties.Add("token", Client.Token);

            if (profile != null)
            {
                properties.Add("distinct_id", profile.DistinctId);
            }

            if (_globalProperties != null)
            {
                foreach (var prop in _globalProperties)
                {
                    properties.Add(prop.Key, prop.Value);
                }
            }

            if (_timedEvents.ContainsKey(eventName))
            {
                var duration = DateTime.UtcNow - _timedEvents [eventName];
                properties.Add("duration", duration.TotalSeconds);
                _timedEvents.Remove(eventName);
            }

            var dataDictionary = new Dictionary <string, object>
            {
                { "event", eventName },
                { "properties", properties ?? new Dictionary <string, object> () }
            };

            var request = RequestHelpers.GetRequestMessageFromDictionaryAndEndpoint(Constants.TrackUri, dataDictionary);

            return(RequestHelpers.MakeRequest(request));
        }
Exemple #2
0
        /// <summary>
        /// Engage a new user with a distinct id.
        /// This will not add any further information to a profile.
        /// </summary>
        /// <returns>The response from the mixpanel server</returns>
        private Task <MixpanelResponse> Engage(string operation, object properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            var dataDictionary = new Dictionary <string, object>
            {
                { "$token", Client.Token },
                { "$distinct_id", _distinctId },
                { operation, properties }
            };

            var request =
                RequestHelpers.GetRequestMessageFromDictionaryAndEndpoint(Constants.EngageUri,
                                                                          dataDictionary);

            return(RequestHelpers.MakeRequest(request));
        }