How to use multiple segments with and condition in it for Google Analytics API while using in Python

Viewed 446

While code in python3 for Google Analytics API I am stuck at a point where I have to use Multiple Segment in the condition. But as per the GA's API Doc the way I use the code will only return me the data in the "OR" condition but I want the result to be in "AND" condition.

Result I want:

sessions::condition::ga:pagePath=@/xxxxx;ga:eventAction==xxxxx

but the current result:

sessions::condition::ga:pagePath=@/xxxxx,ga:eventAction==xxxxx

The code for the segment will be something like as below which is giving me the result in "OR" condition is

"segments":[{"dynamicSegment":{"name":"Page","sessionSegment":{"segmentFilters":[{"simpleSegment":{"orFiltersForSegment":[{"segmentFilterClauses":[{"dimensionFilter":{"dimensionName":"ga:pagePath","expressions":["/xxxxx"],"operator":"PARTIAL"}}]}]}}]}}}, {"dynamicSegment":{"name":"UpGrade","sessionSegment":{"segmentFilters":[{"simpleSegment":{"orFiltersForSegment":[{"segmentFilterClauses":[{"dimensionFilter":{"dimensionName":"ga:eventAction","expressions":["xxxxx"],"operator":"EXACT"}}]}]}}]}}}]

Can any one help in the above code so I can have the output in "AND" condition.

Thanks in Advance.

1 Answers

Try to create the segment in the Google Analytics Interface (with the rules you prefer), so you can use in the API the ID of that segment instead of creating it at runtime in your code.

How to do it:

Create the segment in Google Analytics, apply it and see the URL.

Example:

https://www.google.com/analytics/web/#report/visitors-overview/a11111111w22222222p33333333/%3F_.advseg%3Duser1234567890/

You are looking for _.advseg or _.useg parameter. In this case the segment ID is user1234567890 (characters %3D mean encoded equal sign "=").

So, in API you can use directly:

segment=gaid::user1234567890

In this way you define the AND and the OR in the interface and you can see immediately if that segment is what you need to use it in the API.

Related