public void WithMetric_ShouldAddMetric()
        {
            // Arrange
            var reportRequestBuilder = new ReportRequestBuilder();
            var metric = new AnalyticsMetric
            {
                Id = "metric"
            };

            // Act
            var result = reportRequestBuilder
                .WithMetric(metric)
                .Build();

            // Assert
            Assert.That(result.MetricContainer.Metrics.Count, Is.EqualTo(1));
            Assert.That(
                result.MetricContainer.Metrics,
                Is.All.Property("Id")
                    .EqualTo(metric.Id));
            Assert.That(
                result.MetricContainer.Metrics,
                Is.All.Property("ColumnId")
                    .EqualTo(metric.Id));
        }
        public ReportRequestBuilder WithMetric(AnalyticsMetric metric)
        {
            var reportMetric = new ReportMetric
            {
                Id       = metric.Id,
                ColumnId = metric.Id
            };

            _metrics.Add(reportMetric);

            return(this);
        }
Example #3
0
        /// <summary>
        /// Converts the string representation of the filter options.
        /// </summary>
        /// <param name="str">A string containing the filter options to convert.</param>
        /// <returns>The converted filter options.</returns>
        public static AnalyticsFilterOptions Parse(string str)
        {
            AnalyticsFilterOptions options = new AnalyticsFilterOptions();

            string[] a = str.Split(';');

            for (int i = 0; i < a.Length; i++)
            {
                string[] b = a[i].Split(',');

                if (i > 0)
                {
                    options._filters.Add(AnalyticsFilterOperator.And);
                }

                for (int j = 0; j < b.Length; j++)
                {
                    if (j > 0)
                    {
                        options._filters.Add(AnalyticsFilterOperator.Or);
                    }

                    Match m = Regex.Match(b[j], "^(ga:[a-zA-Z]+)(.{1,2})(.+?)$");

                    if (!m.Success)
                    {
                        throw new Exception("Unable to parse filter '" + b[j] + "'");
                    }

                    if (AnalyticsMetric.TryGet(m.Groups[1].Value, out var metric) && AnalyticsMetricOperator.TryParse(m.Groups[2].Value, out var metricOperator))
                    {
                        options._filters.Add(new AnalyticsMetricFilter(metric, metricOperator, StringUtils.UrlDecode(m.Groups[3].Value)));
                    }
                    else if (AnalyticsDimension.TryGet(m.Groups[1].Value, out var dimension) && AnalyticsDimensionOperator.TryParse(m.Groups[2].Value, out var dimensionOperator))
                    {
                        options._filters.Add(new AnalyticsDimensionFilter(dimension, dimensionOperator, StringUtils.UrlDecode(m.Groups[3].Value)));
                    }
                    else
                    {
                        // Currently the parsing will only work if the specified dimension or
                        // metric name matches a defined constant, so if Google adds a new
                        // dimension or metric, the constants must be updated before the parsing
                        // will work. I'm not sure how often Google adds new dimensions or metric,
                        // so perhaps this isn't a problem
                        throw new Exception("Unable to parse filter '" + b[j] + "'");
                    }
                }
            }

            return(options);
        }
 /// <summary>
 /// Gets the total value of the metric and converts it to <var>T</var>.
 /// </summary>
 /// <typeparam name="T">The type to which the value should be converted.</typeparam>
 /// <param name="metric">The metric of the value.</param>
 public T GetTotalValue <T>(AnalyticsMetric metric)
 {
     return((T)Convert.ChangeType(_totalsForAllResults[metric.Name], typeof(T)));
 }
 public static bool TryGetValue(this AnalyticsDataRow row, AnalyticsMetric key, out string value)
 {
     value = row.GetString(key);
     return(String.IsNullOrWhiteSpace(value) == false);
 }
 public AnalyticsMetricSortField(AnalyticsMetric metric, AnalyticsDataSortOrder order)
 {
     Metric = metric ?? throw new ArgumentNullException(nameof(metric));
     Order  = order;
 }