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

Programatically specify Google Analytics Reporting API v4 query results

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

示例:域.com/thisPage、域.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。

您应该为您的请求添加一个过滤器,只需添加您希望查看其数据的页面,并且只会返回这些页面的数据。

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 来测试您的过滤器,让它们正常工作可能会很棘手。