In Google Analytics, we are able to create segments like below:
From the Google Analytics API explorer, we can pull the segment information be it by segment id or by its code:
I would assume we can just cut and paste that code into the google analytics core reporting v3 reference code like we do with all the other parameters:
When I run the code however, I get this error:
Arg, there was an API error : 400 : Invalid value 'users::condition::dateOfSession==2015-04-30;ga:sessionCount==1;condition::ga:campaign=@33100;sessions::condition::ga:userType=@Returning Visitor'
for segment parameter.
I am not sure what could be wrong. When I use the segment id instead in my code for the segment parameter(segment ='gaid::xxxxxxx'), i get this error:
Arg, there was an API error : 400 : Segment XXXXXXXXXXX is not supported in the API.
Not sure how I should structure the code.
I found the solution after tweaking with the segment code and reading more about segments in the segments dev guide.
I was having issues with the dateOfsession parameter where i should have used '<>' instead of '=' for the date. So this is what i should have done:
dateOfsession<>2015-04-30_2015-04-30
Somehow using '==' doesn't work.The dateOfsession is a new parameter in the GA API and hence have little examples and documentation about it.
Everything you send to the segment, actually anything you send to any of the parameter to the reporting API must be URL encoded.
becomes
That should work. the only thing I am not sure about is if the
;
needs to be encode or not. You may need to test that. I know the==
does. If it doesn't work let me know and I will add a few segments and do some testing myself.