/// <summary> /// Turn an IMeasurementActivity into the key/value pairs necessary for building /// the URI to track with Measurement Protocol. /// </summary> /// <param name="activity">Activity to turn into key/value pairs.</param> /// <returns>Enumerable of key/value pairs representing the activity.</returns> internal IEnumerable<KeyValuePair<string, string>> GetActivityParameters(IMeasurementActivity activity) { if (activity is AppViewActivity) return GetParameters((AppViewActivity)activity); if (activity is ContentViewActivity) return GetParameters((ContentViewActivity)activity); if (activity is CampaignActivity) return GetParameters((CampaignActivity)activity); if (activity is ExceptionActivity) return GetParameters((ExceptionActivity)activity); if (activity is EventActivity) return GetParameters((EventActivity)activity); if (activity is TimedEventActivity) return GetParameters((TimedEventActivity)activity); if (activity is SocialActivity) return GetParameters((SocialActivity)activity); if (activity is TransactionActivity) { var transaction = (TransactionActivity) activity; lastTransaction = transaction; return GetParameters(transaction); } if (activity is TransactionItemActivity) return GetParameters((TransactionItemActivity)activity); Debug.Assert(false, "Unknown Activity type"); return Enumerable.Empty<KeyValuePair<string, string>>(); }
/// <summary> /// Add an item to a transaction. /// </summary> /// <param name="transaction">Transaction to add an item to.</param> /// <param name="code">Product code or SKU.</param> /// <param name="name">Product name.</param> /// <param name="price">Unit price.</param> /// <param name="quantity">Quantity.</param> /// <param name="variation">Variation or category.</param> public static void AddItem(this TransactionActivity transaction, string code, string name, decimal price, int quantity, string variation = null) { if (transaction == null) { throw new ArgumentNullException("transaction"); } transaction.AddItem(new TransactionItemActivity(code, name, price, quantity, variation)); }
/// <summary> /// Preserves the last transaction seen and carries it forward to subsequent /// transaction items. /// </summary> /// <param name="activity">Current activity being tracked.</param> private void CarryForwardLastTransaction(MeasurementActivity activity) { if (activity is TransactionActivity) lastTransaction = (TransactionActivity) activity; if (activity is TransactionItemActivity) ((TransactionItemActivity) activity).Transaction = lastTransaction; }
public void MeasurementTracker_Track_Carries_Forward_Last_Transaction() { var actual = new List<Uri>(); var tracker = new MeasurementTracker(MeasurementTestHelpers.Configuration, MeasurementTestHelpers.CreateSessionManager(), MeasurementTestHelpers.CreateEnvironment(), actual.Add); var transaction = new TransactionActivity { OrderId = "123", Currency = "GBP" }; tracker.Track(transaction); var transactionItem = new TransactionItemActivity("ABC", "Unit Test", 1.23m, 4); tracker.Track(transactionItem); Assert.AreEqual(transaction, transactionItem.Transaction); StringAssert.Contains(actual.Last().OriginalString, "ti=123"); }
internal static IEnumerable<KeyValuePair<string, string>> GetParameters(TransactionActivity transaction) { yield return KeyValuePair.Create("utmt", "tran"); yield return KeyValuePair.Create("utmtid", transaction.OrderId); yield return KeyValuePair.Create("utmtst", transaction.StoreName); yield return KeyValuePair.Create("utmtto", transaction.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture)); yield return KeyValuePair.Create("utmttx", transaction.TaxCost.ToString("0.00", CultureInfo.InvariantCulture)); yield return KeyValuePair.Create("utmtsp", transaction.ShippingCost.ToString("0.00", CultureInfo.InvariantCulture)); yield return KeyValuePair.Create("utmtci", transaction.BillingCity ?? ""); yield return KeyValuePair.Create("utmtco", transaction.BillingCountry ?? ""); yield return KeyValuePair.Create("utmtrg", transaction.BillingRegion ?? ""); }
public void MeasurementActivityTracker_GetParameter_For_TransactionItemActivity_Returns_Correct_Values() { var tracker = new MeasurementActivityTracker(); var transaction = new TransactionActivity { OrderId = "567", Currency = "GBP" }; tracker.GetActivityParameters(transaction); var activity = new TransactionItemActivity("code", "name", 1.23m, 4096, "variation"); var parameters = tracker.GetParameters(activity).ToDictionary(k => k.Key, v => v.Value); Assert.AreEqual(8, parameters.Keys.Count); Assert.AreEqual("item", parameters["t"]); Assert.AreEqual("567", parameters["ti"]); Assert.AreEqual("GBP", parameters["cu"]); Assert.AreEqual("code", parameters["ic"]); Assert.AreEqual("name", parameters["in"]); Assert.AreEqual("1.23", parameters["ip"]); Assert.AreEqual("4096", parameters["iq"]); Assert.AreEqual("variation", parameters["iv"]); }
public void MeasurementActivityTracker_GetParameter_For_TransactionActivity_Returns_Correct_Values() { var tracker = new MeasurementActivityTracker(); var activity = new TransactionActivity { OrderId = "12345", Currency = "USD", OrderTotal = 109.76m, ShippingCost = 11.27m, StoreName = "My Store", TaxCost = 8.18m }; var parameters = tracker.GetParameters(activity).ToDictionary(k => k.Key, v => v.Value); Assert.AreEqual(7, parameters.Keys.Count); Assert.AreEqual("transaction", parameters["t"]); Assert.AreEqual("12345", parameters["ti"]); Assert.AreEqual("USD", parameters["cu"]); Assert.AreEqual("109.76", parameters["tr"]); Assert.AreEqual("11.27", parameters["ts"]); Assert.AreEqual("My Store", parameters["ta"]); Assert.AreEqual("8.18", parameters["tt"]); }
/// <summary> /// Obtain the key/value pairs for a TransactionActivity. /// </summary> /// <param name="transaction">TransactionActivity to turn into key/value pairs.</param> /// <returns>Key/value pairs representing this TransactionActivity.</returns> internal static IEnumerable<KeyValuePair<string, string>> GetParameters(TransactionActivity transaction) { yield return KeyValuePair.Create("t", "transaction"); yield return KeyValuePair.Create("ti", transaction.OrderId); foreach (var parameter in GetCommonParameters(transaction)) yield return parameter; if (!String.IsNullOrWhiteSpace(transaction.StoreName)) yield return KeyValuePair.Create("ta", transaction.StoreName); if (transaction.OrderTotal != Decimal.Zero) yield return KeyValuePair.Create("tr", transaction.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture)); if (transaction.ShippingCost != Decimal.Zero) yield return KeyValuePair.Create("ts", transaction.ShippingCost.ToString("0.00", CultureInfo.InvariantCulture)); if (transaction.TaxCost != Decimal.Zero) yield return KeyValuePair.Create("tt", transaction.TaxCost.ToString("0.00", CultureInfo.InvariantCulture)); if (!String.IsNullOrWhiteSpace(transaction.Currency)) yield return KeyValuePair.Create("cu", transaction.Currency); }