/// <summary>
        /// Adds product information to be sent with a given hit. The action provided affects how the products passed in through this method get processed.
        /// </summary>
        /// <param name="product">The product you wish to add.</param>
        /// <returns>The builder object that you can use to chain calls.</returns>
        public HitBuilder AddProduct(Ecommerce.Product product)
        {
            var index = ProductCount + 1;

            var data = new Dictionary <string, string>();

            if (product.Id != null)
            {
                data.Add($"pr{index}id", product.Id);
            }
            if (product.Name != null)
            {
                data.Add($"pr{index}nm", product.Name);
            }
            if (product.Brand != null)
            {
                data.Add($"pr{index}br", product.Brand);
            }
            if (product.Category != null)
            {
                data.Add($"pr{index}ca", product.Category);
            }
            if (product.Variant != null)
            {
                data.Add($"pr{index}va", product.Variant);
            }
            if (product.Price.HasValue)
            {
                data.Add($"pr{index}pr", product.Price.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (product.Quantity.HasValue)
            {
                data.Add($"pr{index}at", product.Quantity.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (product.CouponCode != null)
            {
                data.Add($"pr{index}cc", product.CouponCode);
            }
            if (product.Position.HasValue)
            {
                data.Add($"pr{index}ps", product.Position.Value.ToString(CultureInfo.InvariantCulture));
            }
            foreach (var item in product.CustomDimensions)
            {
                data.Add($"pr{index}cd{item.Key}", item.Value);
            }
            foreach (var item in product.CustomMetrics)
            {
                data.Add($"pr{index}cm{item.Key}", item.Value.ToString(CultureInfo.InvariantCulture));
            }
            return(new HitBuilder(lineage, data)
            {
                ProductCount = index
            });
        }
Exemple #2
0
        /// <summary>
        /// Adds product information as impression to be sent with a given hit, optionally associated with named impression list.
        /// </summary>
        /// <param name="product">The product you wish to add as an impression.</param>
        /// <param name="impressionList">The associated named impression list</param>
        /// <returns>The builder object that you can use to chain calls.</returns>
        public HitBuilder AddImpression(Ecommerce.Product product, string impressionList)
        {
            var idxImpression = 1;
            var idxProduct    = 1;

            if (String.IsNullOrEmpty(impressionList))
            {
                impressionList = String.Empty;
            }
            var impressions = new Dictionary <string, int>(this.impressions);

            if (!impressions.ContainsKey(impressionList))
            {
                impressions.Add(impressionList, 1);
                idxImpression = impressions.Count;
            }
            else
            {
                idxImpression = impressions.Keys.ToList().IndexOf(impressionList) + 1;
                idxProduct    = impressions[impressionList]++;
            }

            var data = new Dictionary <string, string>();

            if (!String.IsNullOrEmpty(impressionList))
            {
                data.Add($"il{idxImpression}nm", impressionList);
            }
            if (product.Id != null)
            {
                data.Add($"il{idxImpression}pi{idxProduct}id", product.Id);
            }
            if (product.Name != null)
            {
                data.Add($"il{idxImpression}pi{idxProduct}nm", product.Name);
            }
            if (product.Brand != null)
            {
                data.Add($"il{idxImpression}pi{idxProduct}br", product.Brand);
            }
            if (product.Category != null)
            {
                data.Add($"il{idxImpression}pi{idxProduct}ca", product.Category);
            }
            if (product.Variant != null)
            {
                data.Add($"il{idxImpression}pi{idxProduct}va", product.Variant);
            }
            if (product.Price.HasValue)
            {
                data.Add($"il{idxImpression}pi{idxProduct}pr", product.Price.Value.ToString(CultureInfo.InvariantCulture));
            }
            if (product.Position.HasValue)
            {
                data.Add($"il{idxImpression}pi{idxProduct}ps", product.Position.Value.ToString(CultureInfo.InvariantCulture));
            }
            foreach (var item in product.CustomDimensions)
            {
                data.Add($"il{idxImpression}pi{idxProduct}cd{item.Key}", item.Value);
            }
            foreach (var item in product.CustomMetrics)
            {
                data.Add($"il{idxImpression}pi{idxProduct}cm{item.Key}", item.Value.ToString(CultureInfo.InvariantCulture));
            }
            return(new HitBuilder(lineage, data, impressions));
        }