以编程方式指定Google Analytics Reporting API v4查询结果

时间:2017-05-16 01:13:26

标签: c# .net google-api google-analytics-api google-api-dotnet-client

如何查询特定页面?我只需要从特定页面获取数据。

示例:domain.com/thisPage,domain.com/anotherPage等。

ReportRequest reportRequestLandingPath = new ReportRequest
{
    ViewId = GoogleAnalyticsIds,
    DateRanges = new List<DateRange>() { dateRange },
    Dimensions = new List<Dimension>() { landingPagePath },
    Metrics = new List<Metric>() { avgSessionDuration, sessions, pageviewsPerSession }
};

我试图获得看起来something like this 的结果,以便我可以将它们添加到我正在构建的Google图表中。

我的问题是,我为我的域名中的每个页面取回了所有78个结果。我只需要从5-6个特定页面获取avgSessionDuration,sessions,pageviewsPerSession。

1 个答案:

答案 0 :(得分:1)

您应该在请求中添加一个过滤器,只需添加您想要查看数据的页面,并且只返回这些页面的数据。

var metrics = new List<Metric>();
metrics.Add(new Metric { Expression = "ga:avgSessionDuration", Alias = "Avg. Session Duration" });
metrics.Add(new Metric { Expression = "ga:sessions", Alias = "Sessions" });
metrics.Add(new Metric { Expression = "ga:pageviewsPerSession", Alias = "Pageviews Per Session" });

var dimensions = new List<Dimension>();
dimensions.Add(new Dimension { Name = "ga:landingPagePath" });

var filterClause = new DimensionFilterClause();

var filter = new DimensionFilter
         {
         DimensionName = "ga:landingPagePath",
         Operator__ = "PARTIAL",
         Expressions = new List<string> { "accessing-google-calendar-with-php-oauth2" }
        };
filterClause.Filters = new List<DimensionFilter> { filter };
ReportRequest reportRequest = new ReportRequest
            {
                ViewId = "78110423",
                DateRanges = new List<DateRange>() { new DateRange() { StartDate = "3daysago", EndDate = "today" } },
                Dimensions = dimensions,
                Metrics = metrics,
                DimensionFilterClauses = new List<DimensionFilterClause> { filterClause }
            };

List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);

// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };

我建议使用Query explorer来测试您的过滤器,让它们起作用可能很棘手。