Error 400 - Bad Request - Invalid Value when using dynamic segment, Core Reporting API v3.0

2,808 views
Skip to first unread message

Junior

unread,
Jan 20, 2012, 7:14:04 AM1/20/12
to google-analytics...@googlegroups.com
Hi,
When we try to obtain data via the Core Reporting API v3.0 using the request below, we receive a "bad request error". The details below were obtained using OAuth 2.0 playground.
The same query works fine using the Data Feed Query Explorer, which is based on version 2.4 of the API.
The culprit seems to be the segment bit. If we remove the "segment=dynamic::ga:medium==CPC" part, the query works flawlessly, but the syntax seems to be correct. The problem still happens when I use the "==" in encoded form (i.e. %3D%3D).

Any help on this issue would be greatly appreciated.

Kind regards,
Junior
 

------------------------------------------

REQUEST:

GET /analytics/v3/data/ga?ids=ga:XXXXXX&dimensions=ga:keyword,ga:medium&metrics=ga:bounces,ga:visits,ga:adClicks,ga:adCost,ga:impressions,ga:goal2Completions,ga:goal3Completions,ga:goal4Completions,ga:goal5Completions,ga:goalCompletionsAll&segment=dynamic::ga:medium==CPC&max-results=10000&prettyPrint=true&start-date=2012-01-01&end-date=2012-01-01&start-index=1 HTTP/1.1
Host: www.googleapis.com
Authorization: OAuth ya29.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

------------------------------------------

RESPONSE:

HTTP/1.1 400 Bad Request
status: 400
content-length: 172
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
expires: Fri, 20 Jan 2012 12:02:08 GMT
x-google-cache-control: remote-fetch
server: GSE
via: HTTP/1.1 GWA
cache-control: private, max-age=0
date: Fri, 20 Jan 2012 12:02:08 GMT
x-frame-options: SAMEORIGIN
content-type: application/json; charset=UTF-8
-content-encoding: gzip
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}

Aaron Toledo

unread,
Jan 20, 2012, 7:10:29 PM1/20/12
to google-analytics...@googlegroups.com
I didn't realize you could build out a dynamic segment until I saw your post. Thanks for that!  I was able to build out this query with my project and it works:

Based on the code you provided, it looks like you have to escape "==" to "%3D%3D".

Junior

unread,
Jan 23, 2012, 9:26:48 AM1/23/12
to google-analytics...@googlegroups.com
Hi Aaron,

Thanks for your reply.

As I mentioned in my original e-mail, the problem persists when we encoded query string (namely %3D%3D). I paste the unencoded form for clarity and because that was the output from OAuth 2.0 playground.

It does not surprise me you were able to run build and run a similar query on your system. We are have been able to use segments so far and with several clients. This code only seems to fail for some accounts. I wonder if there is any account level restriction for dynamic segments, even thought it does not really make sense to be.


Aaron Toledo

unread,
Jan 23, 2012, 10:35:49 AM1/23/12
to google-analytics...@googlegroups.com
Ah, I didn't see your last sentence in the original post.

I tried again using the exact same parameters you used and I was able to reproduce the error. When I removed the AdWords-related metrics, the problem seemed to go away. Actually, I can't even request those metrics individually right now. Try your request again without requesting adclicks, adcost, and impressions and let's see if the problem goes away.

Aaron Toledo

unread,
Jan 23, 2012, 10:52:35 AM1/23/12
to google-analytics...@googlegroups.com
One more update - It looks like I'm only able to request AdWords metrics when I completely remove segments from the request. Meaning, "&segment" can't even be in the query.

Junior

unread,
Jan 23, 2012, 11:03:35 AM1/23/12
to google-analytics...@googlegroups.com
We found out the problem was that we were trying to include some metrics which, according to the documentation are "disallowed segment metrics", namely: ga:impressions, ga:adClicks and ga:adCost. 

When we remove those metrics, the query run as normal.

However, this is either an incorrect behaviour of the API during the input validation, or an error in the documentation, as it says:

"Disallowed Dimensions and Metrics in segments.

The following dimensions and metrics are not allowed in advanced segment expressions:"

In this instance, we are not using the "disallowed" metrics in the segment expression, we are merely including them in the results. We segment by ga:medium.

As for recap, the following query string does not work:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxxxx&dimensions=ga%3Akeyword&metrics=ga%3Avisits,ga%3Aimpressions&segment=dynamic%3A%3Aga%3Amedium%3D%3DCPC&prettyPrint=true&start-date=2012-01-01&end-date=2012-01-01&start-index=1

After stripping off the dynamic segment, it works fine:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxxxx&dimensions=ga%3Akeyword&metrics=ga%3Avisits,ga%3Aimpressions&prettyPrint=true&start-date=2012-01-01&end-date=2012-01-01&start-index=1

Or, if we keep the segment but remove the offending metric (ga:impressions) it also works fine:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxxxx&dimensions=ga%3Akeyword&metrics=ga%3Avisits&segment=dynamic%3A%3Aga%3Amedium%3D%3DCPC&prettyPrint=true&start-date=2012-01-01&end-date=2012-01-01&start-index=1


For the moment we will just assume "Disallowed Segment Metrics"  means "do not include those metrics if we are going to use segments, but any additional information about this issue would be greatly appreciated.

Kind regards,

Junior







Junior

unread,
Jan 23, 2012, 11:05:33 AM1/23/12
to google-analytics...@googlegroups.com
Hi Aaron, many thanks for your additional input on the issue. Yes, you are right, those metrics/segments seems to be incompatible.

Mike Sullivan

unread,
Feb 22, 2012, 9:26:55 AM2/22/12
to google-analytics...@googlegroups.com
Just a followup on this problem. It is not possible to use any segment definition, even the standard set, with the 'excluded' metrics. This is inconsistent with the web UI and therefore should be considered a bug in the API.

e.g. a simple query of impressions for the new visitors segment fails via the API but works fine from the web UI:
Reply all
Reply to author
Forward
0 new messages