使用C#库在Google AnalyticsAPI中的维度或指标上应用过滤器

时间:2016-12-28 04:13:37

标签: c# asp.net filter google-analytics google-analytics-api

我已经能够在Google Analytics上对我的应用进行身份验证,并在独特的网页浏览量周围显示一些数据。现在我想根据流量来源获取页面浏览量,但我不太确定如何在维度上获取过滤器。我已经用c#编写了我的代码,并且无法理解我所见过的其他语言的例子。我使用了api Query Explorer并获得了正确的表达式和结果。我的困难是如何将其转换为我的代码。我的代码在

之下
    var filepath = Server.MapPath("~/jsonfile");  // path to the json file for the Service account
        GoogleCredential credentials;
        using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            string[] scopes = { AnalyticsReportingService.Scope.AnalyticsReadonly };
            var googleCredential = GoogleCredential.FromStream(stream);
            credentials = googleCredential.CreateScoped(scopes);
        }

        var reportingService = new AnalyticsReportingService(
        new BaseClientService.Initializer
        {
        HttpClientInitializer = credentials
        });


        var dateRange = new DateRange
        {
            StartDate = "2016-10-28",
            EndDate = "2016-12-20"
        };

        var sessions = new Metric
        {
            Expression = "ga:uniquePageviews", 
            Alias = "Sessions"
        };


        var social = new Dimension { Name = "ga:socialNetwork" };



        var reportRequest = new ReportRequest
        {
            DateRanges = new List<DateRange> { dateRange },
            Dimensions = new List<Dimension> { social },
            Metrics = new List<Metric> { sessions },
            ViewId = "myviewid" 
        };

        var getReportsRequest = new GetReportsRequest
        {
            ReportRequests = new List<ReportRequest> { reportRequest }
        };

        var batchRequest = reportingService.Reports.BatchGet(getReportsRequest);
        var response = batchRequest.Execute();
        foreach (var x in response.Reports.FirstOrDefault().Data.Rows)
        {
            Response.Write(string.Join(", ", x.Dimensions) + "   " + string.Join(", ", x.Metrics.First().Values));
        }

1 个答案:

答案 0 :(得分:1)

您应该创建DimensionFilterClauses并将其传递给ReportRequest,如下所示:

//Create the Dimension Filter
var dimensionFilter = new DimensionFilter();
dimensionFilter.DimensionName = "ga:socialNetwork";
dimensionFilter.Expressions = new List<string> { "someValue" };
var dimensionFilterClause = new DimensionFilterClause();
dimensionFilterClause.Filters = new List<DimensionFilter> { dimensionFilter };

然后修改您的ReportRequest

var reportRequest = new ReportRequest
{
    DateRanges = new List<DateRange> { dateRange },
    Dimensions = new List<Dimension> { social },
    Metrics = new List<Metric> { sessions },
    ViewId = "myviewid",
    DimensionFilterClauses = new List<DimensionFilterClause> { dimensionFilterClause }
};

<强> P.S: 此外,如果您需要过滤Metric而不是Dimension,则需要按如下方式创建MetricFilterClauses,然后将其传递到MetricFilterClauses中的ReportRequest

//Create the Metric Filter
var metricFilter = new MetricFilter();
metricFilter.MetricName = "someMetric";
metricFilter.ComparisonValue = "someValue";
var metricFilterClause = new MetricFilterClause();
metricFilterClause.Filters = new List<MetricFilter> { metricFilter };