问题
Good afternoon! I've been searching StackOverflow and the web for about 24 hours now, and I haven't found an answer yet. I hope I'm missing something simple that a more experienced API user can point out for me.
I'm querying the Google Analytics API using their .NET library. This request works:
https://www.google.com/analytics/feeds/data?start-index=1&max-results=500&dimensions=ga:date&end-date=2011-01-06&ids=ga:________&metrics=ga:visitors,ga:visits&start-date=2011-01-06
...but I'd like to filter the results. I've tried a single very simple filter which does not require URL encoding:
filters=ga:city!@Minsk
All I get is the maddeningly vague "400 bad request" error. I get the same result using the equals operator:
DataQuery query = new DataQuery(URL);
...
query.Filters = "ga:city==Minsk"
Ditto if I encode the equals signs as %3D. This gets translated into "ga:city%253D%253DMinsk", according to the error message. I've tried single-quoting "Minsk", and double-quoting it; no luck.
I'm stumped. Any ideas? Thanks!
回答1:
Found it. It turns out that filters have to be "compatible" with the dimensions and metrics actually expressed in a query. Dropping ga:visitors allowed me to apply most of the filters I needed, though the combination of ga:pagePath and ga:campaign is not allowed with ga:visits... for some reason. Here's the detail, but be warned: you may end up cross-eyed.
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceValidCombos.html
Oh, and using the .NET library you do not need to encode equals signs or spaces in your filters. I structured my filters like this:
private static readonly string Filter = string.Join
(
";", new string[]
{
"ga:city!=Simi Valley", // URL encoding is handled for you
"ga:pagePath!@/splash",
"ga:pagePath!@static_test",
"ga:networkLocation!@spring",
"ga:networkLocation!@equinix asia pacific pte ltd"
// ...
}
);
Hope this helps someone!
来源:https://stackoverflow.com/questions/4618446/google-analytics-api-filtering-via-net-so-close