使用Google Analytics Data API按国家/地区过滤

时间:2021-10-31 14:08:38

I'm trying to use the Google Analytics Data API to get data filtered by country. I am using the .NET library at http://google-gdata.googlecode.com/svn/trunk/ which works beautifully as long as I don't try to filter by country.

我正在尝试使用Google AnalyticsData API来按国家/地区过滤数据。我在http://google-gdata.googlecode.com/svn/trunk/上使用.NET库,只要我不尝试按国家/地区进行过滤,它就能很好地运行。

This works fine:

这很好用:

const string dataFeedUrl = "https://www.google.com/analytics/feeds/data"; 

var query = new DataQuery(dataFeedUrl);
query.Ids = this.ProfileID;
query.GAStartDate = this.FromDate;
query.GAEndDate = this.ToDate;
query.Metrics = "ga:newVisits,ga:visitors";

DataFeed dataFeed = this.AnalyticsService.Query(query);

But if I add this before the Query call:

但是如果我在Query调用之前添加它:

query.Filters = string.Format("ga:country=={0}", this.Country);
query.Dimensions = "ga:country";

... I get this exception:

......我得到这个例外:

System.Exception {Google.GData.Client.GDataRequestException}
"Execution of request failed: https://www.google.com/analytics/feeds/data?dimensions=ga:country&end-date=2009-06-17&filters=ga:country==Denmark&ids=ga:xxx&metrics=ga:newVisits,ga:visitors&start-date=2009-05-18"

I've tried manually escaping == to %3D%3D but that didn't help.

我已经尝试手动转义==到%3D%3D,但这没有帮助。

Any ideas?

1 个解决方案

#1


I finally found the ResponseString and saw this message:

我终于找到了ResponseString并看到了这条消息:

"Illegal combination of dimensions and metrics"

Apparently, I have been trying to get data that is not available. This does make sense when looking at the Query Validation Chart in the documentation.

显然,我一直在尝试获取不可用的数据。在查看文档中的查询验证图表时,这确实有意义。

#1


I finally found the ResponseString and saw this message:

我终于找到了ResponseString并看到了这条消息:

"Illegal combination of dimensions and metrics"

Apparently, I have been trying to get data that is not available. This does make sense when looking at the Query Validation Chart in the documentation.

显然,我一直在尝试获取不可用的数据。在查看文档中的查询验证图表时,这确实有意义。