Filter by regex querying the API

3,830 views
Skip to first unread message

julesb...@hotmail.com

unread,
Jun 20, 2013, 5:19:28 AM6/20/13
to google-analytics...@googlegroups.com
Hi Guys,

I face a problem concernig the use of a regex in my API Query. Here are the Javascript codes :

//Trafic Direct
function requeteTrafficDirect (accountID){
    accountID = 'xxxxxxx';
    var apiQuery = gapi.client.analytics.data.ga.get({
    'ids': 'ga:' + accountID,
    'start-date': '2013-01-01',
    'end-date': '2013-06-05',
    'metrics': 'ga:visits,  ga:transactions,  ga:transactionRevenue ,  ga:visitBounceRate,  ga:visitors,  ga:newVisits',
    'dimensions': 'ga:year, ga:week',
    'sort': 'ga:year, ga:week',
    'filters': 'ga:source==(direct)',
    'max-results': 10000
   });
   apiQuery.execute(testImpressionRequete);
}

//E-mail
function requeteEmail (accountID){
    accountID = 'xxxxxx';
    regexp = '(messagerie-)|(mail(.*)\\.(.*)\\..{2\,4})|E-mail'
    regexp = encodeURIComponent(regexp);
    alert (regexp);
  var apiQuery = gapi.client.analytics.data.ga.get({
    // Attention c'est l'id du profil et pas du compte qu'on a chopé au dessus
    'ids': 'ga:' + accountID,
    'start-date': '2013-01-01',
    'end-date': '2013-06-05',
    'metrics': 'ga:visits,  ga:transactions,  ga:transactionRevenue ,  ga:visitBounceRate,  ga:visitors,  ga:newVisits',
    'dimensions': 'ga:year, ga:week',
    'sort': 'ga:year, ga:week',
   
    'filters': 'ga:source=~' + regexp,
    'max-results': 500
   });
   apiQuery.execute(testImpressionRequete);
}


The first query (Traffic direct) works well.
The second one (e-mail) is doesn't work at all... Indeed, all metrics results are "0". 
The thing is that if i do the following regex : regexp = '(google|yahoo)'; The results are still "0".

If I test those regex to the Query explorer and results are good. There is something i didn't get. Can somebody help me please ?

The ultimate filter i'd like to do is the following : " include " " Source/medium " " regex " " (messagerie-)|(mail(.*)\.(.*)\..{2,4})|E-mail "
This what i've done in a customisable report.


Thanks for your time guys and have a nice day !

PS: hope my english is good enougth :)


Jean-Baptiste Gabellieri

unread,
Jun 21, 2013, 8:30:06 AM6/21/13
to google-analytics...@googlegroups.com
Hi,

Does it work in the Query Explorer tool ? http://ga-dev-tools.appspot.com/explorer/

The code looks fine, even if I'm not a js expert. :-)

JB

julesb...@hotmail.com

unread,
Jun 24, 2013, 8:18:42 AM6/24/13
to google-analytics...@googlegroups.com
Hello JB,

Yes it works,

Julien

Aaron Toledo

unread,
Jun 24, 2013, 9:56:56 AM6/24/13
to google-analytics...@googlegroups.com
I'm not familiar with the JS library, but its my understanding that all the libraries automatically encode the request URL. Have you tried without encoding the filter? Just pass it in as a string.

julesb...@hotmail.com

unread,
Jun 26, 2013, 5:30:29 AM6/26/13
to google-analytics...@googlegroups.com
Hi all,

I've found the solution. With my first request code i had an error message :

The filter was : ga:source=~(messagerie-)|(mail(.*)\\.(.*)\\..{2\,4})|E-mail
The Answer was : " There was an error querying core reporting API : Invalid value 'ga:source=~(messagerie-)|(mail(.*)\.(.*)\..{2,4})|E-mail' for filters. "

Look into the core reporting tutorial, you ll understand that this is the function next the query that was failing.

Its said that you have to backslash caracters like "\"or "," but in the error message there is no backslash before the coma.

That is why i've just try : ga:source=~(messagerie-)|(mail(.*)\\.(.*)\\..{2\\,4})|E-mail
... I've double backslashed the coma and it works.

Good to know... :)

Thx for your help guys

Julien
Reply all
Reply to author
Forward
0 new messages