Analytics
2.4K members online now
2.4K members online now
Ask questions about filter set-up and issues with using filters in Google Analytics reports
 
Guide Me
star_border
Reply

RegExp works in Analytics Web but not API

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

I'm trying to use the Google Spreadsheets API tool to run some analysis on what people search for on the site. I have a regular expression query which works when i test it in the web interface of Analytics but it's not working when i use the same filter in the API.

 

I've found a few example regular expressions that can identify UK Postcodes, such as:

 

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][AZa-z]{2})$

(src)

 

^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$

(src)

 

 These work in the web interface but the API says the following filter is invalid

 

ga:searchKeyword=~^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$

 

 The Query Explorer returns:

 

Ack! There was an error (400)
Invalid value 'ga:searchKeyword=~^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$' for filters parameter.

 

I've never known there to be a difference between a filter that the web interface accepts but the API doesn't. It's a far more complicated regular expression than I would usually use, can anybody help me understand why it doesn't work?

 

Thanks,

Ben

1 Expert replyverified_user

Re: RegExp works in Analytics Web but not API

Rising Star
# 2
Rising Star
Is it not simply the case that it is too long? I tried it in the explorer after hacking bits of it out and it worked without an error.
John Wedderburn, Advertiser Community, Rising Star
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: RegExp works in Analytics Web but not API

Rising Star
# 3
Rising Star
128 characters is the limit, anything over returns the error message you are experiencing:
https://developers.google.com/analytics/devguides/reporting/core/v3/reference?hl=en
John Wedderburn, Advertiser Community, Rising Star
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: RegExp works in Analytics Web but not API

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭
Thanks for your Answer John. The shorter filter was only 119 characters but perhaps it character counts the curly brackets as if '[ABD-HJLN-UW-Z]{2}' was written as '[ABD-HJLN-UW-Z][ABD-HJLN-UW-Z]'

I removed the curly brackets part and the filter is accepted (it doesn't match any results, but it certainly suggests that was causing a problem).