using System.Collections.Generic; using System.Text; namespace JustTrack { /// /// Represents a user-generated event with associated data. /// public class AppEvent { /// /// Gets the name of the event. /// public string Name { get; private set; } /// /// Gets the collection of dimensions (key-value pairs) associated with this event. /// internal Dictionary Dimensions { get; private set; } /// /// Gets the numeric value associated with this event. /// internal double Value { get; private set; } /// /// Gets the unit of measurement for the event value, if specified. /// internal Unit? Unit { get; private set; } /// /// Gets the currency code for monetary values, if specified. /// internal string? Currency { get; private set; } /// /// Initializes a new instance of the class with the specified name. /// /// The name of the event. public AppEvent(string pName) { this.Name = pName; this.Dimensions = new Dictionary(); this.Value = 0.0; this.Unit = null; this.Currency = null; } /// /// Initializes a new instance of the class with the specified name, value, and unit. /// /// The name of the event. /// The numeric value of the event. /// The unit of measurement for the value. public AppEvent(string pName, double pValue, Unit pUnit) { this.Name = pName; this.Dimensions = new Dictionary(); this.Value = pValue; this.Unit = pUnit; this.Currency = null; } /// /// Initializes a new instance of the class with the specified name and monetary value. /// /// The name of the event. /// The monetary value associated with the event. public AppEvent(string pName, Money pMoney) { this.Name = pName; this.Dimensions = new Dictionary(); this.Value = pMoney.Value; this.Unit = null; this.Currency = pMoney.Currency; } /// /// Adds a dimension with the specified key and value to the event. /// /// The key of the dimension. /// The value of the dimension. /// The current AppEvent instance for method chaining. public AppEvent AddDimension(string pDimension, string pValue) { if (!string.IsNullOrEmpty(pDimension)) { if (string.IsNullOrEmpty(pValue)) { this.Dimensions.Remove(pDimension); } else { this.Dimensions.Add(pDimension, pValue); } } return this; } /// /// Adds a dimension with the specified enum key and value to the event. /// /// The enum key of the dimension. /// The value of the dimension. /// The current AppEvent instance for method chaining. public AppEvent AddDimension(Dimension pDimension, string pValue) { return this.AddDimension(DimensionConversions.DimensionToString(pDimension), pValue); } /// /// Removes a dimension with the specified key from the event. /// /// The key of the dimension to remove. /// The current AppEvent instance for method chaining. public AppEvent RemoveDimension(string pDimension) { if (!string.IsNullOrEmpty(pDimension)) { this.Dimensions.Remove(pDimension); } return this; } /// /// Removes a dimension with the specified enum key from the event. /// /// The enum key of the dimension to remove. /// The current AppEvent instance for method chaining. public AppEvent RemoveDimension(Dimension pDimension) { return this.RemoveDimension(DimensionConversions.DimensionToString(pDimension)); } /// /// Sets the value and unit of measurement for the event. /// /// The numeric value to set. /// The unit of measurement for the value. /// The current AppEvent instance for method chaining. public AppEvent SetValue(double pValue, Unit pUnit) { this.Value = pValue; this.Unit = pUnit; this.Currency = null; return this; } /// /// Sets the monetary value for the event. /// /// The monetary value to set. /// The current AppEvent instance for method chaining. public AppEvent SetValue(Money pMoney) { this.Value = pMoney.Value; this.Unit = null; this.Currency = pMoney.Currency; return this; } /// /// Sets the count value for the event. /// /// The count value to set. /// The current AppEvent instance for method chaining. public AppEvent SetCount(double pCount) { return this.SetValue(pCount, JustTrack.Unit.Count); } /// /// Sets the duration in seconds for the event. /// /// The duration in seconds to set. /// The current AppEvent instance for method chaining. public AppEvent SetSeconds(double pSeconds) { return this.SetValue(pSeconds, JustTrack.Unit.Seconds); } /// /// Sets the duration in milliseconds for the event. /// /// The duration in milliseconds to set. /// The current AppEvent instance for method chaining. public AppEvent SetMilliseconds(double pMilliseconds) { return this.SetValue(pMilliseconds, JustTrack.Unit.Milliseconds); } #if UNITY_IOS internal string EncodeDimensions() { StringBuilder sb = new StringBuilder(); sb.Append('{'); bool first = true; foreach (var item in Dimensions) { if (first) { first = false; } else { sb.Append(','); } sb.Append($"\"{item.Key}\":\""); foreach (var c in item.Value) { if (c < ' ' || c == '"' || c == '\\' || c > '~') { sb.Append("\\u").Append(((int) c).ToString("X4")); } else { sb.Append(c); } } sb.Append('"'); } sb.Append('}'); return sb.ToString(); } #endif } }