Google Analytics service accounts filters & dynamic segments

Member Article

Filters & Segments with Google Analytics Service Accounts

I am in the process of developing parts of this website (bdaily.co.uk) to depend on live Google Analytics data and I have just stumbled upon a solution to what appears to be a fairly common problem.

Service accounts:

Only a service account can allow an anonymous user to query Google Analytics data, making them an extremely useful bit of kit. We intend to use them to deliver information to custom, front end dashboards so that our journalists, and eventually our community publishers, can check the performance of their content live. We will also be using a service account to create bespoke reports and live data feeds.

The Problem:

Both the Google Analytics Core Reporting API and service accounts are restricted in ways which seem to make it impossible to query using complex arguments. Making data segmentation extremely difficult. This is a problem for us because we use a series of advanced segments with convoluted filters to segment our user data by UK county.

  • The analytics API limits arguments to 128 characters. So complex regular expressions (regex) are out of the question.
  • The same applies to dynamic segments.
  • You can not log into analytics with a service account so advanced segments cannot be applied to them.

If you have spent any time segmenting with the Core Reporting API you will be intimately familiar with this error message:

’An error occurred when querying the google analytics service (Regular expression too long)’

The Solution:

Mentioned very discretely here in the Core Reporting API documentation is a section which details the use of AND (;) and OR (,) operators to define multiple arguments for a single query. So what once was an API breakingly long filter:

ga:city=~^Alnwick$|^Ashington$|^Berwick-upon-Tweed$|^Billingham$|^Bishop Auckland$|^Consett$|^Cramlington$|^Darlington$|^Durham$|^Ferryhill$|^Gateshead$| ^Hartlepool$|^Hexham$|^Ingram$|^Morpeth$|^Newburn$|^Newcastle upon Tyne$|^Newton Aycliffe$| ^North Shields$|^North Tyneside District$|^Penshaw$|^Peterlee$|^Rothbury$|^Rowlands Gill$|^Sherburn Village$|^South Bank$|^South Tyneside District$|^Spennymoor$|^Stockton-on-Tees$|^Sunderland$|^Washington$|^Yarm$

Ew… Is now a tidy set of logic-separated arguments that can be painlessly processed by the API:

ga:city=~^Alnwick$|^Ashington$|^Berwick-upon-Tweed$|^Billingham$,

ga:city=~^Bishop Auckland$|^Consett$|^Cramlington$||^Cramlington$,

etc.

Any questions, please leave them in the comments below. Additionally, once I have finished cleaning up our UK county advanced segments I intended to make them available publicly. If you’d like me to notify you when they are available let me know.

Edit:

It is worth remembering that even after passing multiple arguments no request can surpass the limit of 2048 characters, which will give you this error:

’An error occured when querying the google analytics service (Invalid json)’

The only solution I have found to this issue (short of making a feature request to google for advanced segments in service accounts) is to split the single request into two requests, essentially cutting your argument down the midddle, and then combining the data from the two responses.

This was posted in Bdaily's Members' News section by Si Tapson .

Our Partners

Top Ten Most Read