javascript - Using "filters" parameter in Google API request always results in an error? -
i'm experimenting querying google analaytics core reporting api. i've hit bug can't figure out. i'm in days i've copied example javascript implemtation quickstart guide. ran this, works, lovely.
so started playing parameters , seemed fine until entered filter, so
this worked
// query core reporting api number sessions // past 7 days. gapi.client.analytics.data.ga.get({ 'ids': 'ga:' + profileid, 'start-date': '7daysago', 'end-date': 'today', 'dimensions':'ga:browser', 'metrics': 'ga:pageviews' })
doesn't work
gapi.client.analytics.data.ga.get({ 'ids': 'ga:' + profileid, 'start-date': '7daysago', 'end-date': 'today', 'filters':'ga:city%3d%3dirvine', 'dimensions':'ga:browser', 'metrics': 'ga:pageviews' })
results in error:
{ "error": { "errors": [{ "domain": "global", "reason": "invalidparameter", "message": "invalidvalue'ga: city%3d%3dirvine'forfiltersparameter." }], "code": 400, "message": "invalidvalue'ga: city%3d%3dirvine'forfiltersparameter." } }
i've read re-read the docs , can't see issue filter.
so missing here? why entering filter, i'm doing fail?
full code
i don't think you'll able run without clientid, etc. here goes (for completeness)
<!doctype html> <html> <head> <meta charset="utf-8"> <title>hello analytics - quickstart guide javascript</title> </head> <body> <button id="auth-button" hidden>authorize</button> <h1>hello analytics</h1> <textarea cols="80" rows="20" id="query-output"></textarea> <script> // replace client id developer console. var client_id = 'myclientid'; // set authorized scope. var scopes = ['https://www.googleapis.com/auth/analytics.readonly']; function authorize(event) { // handles authorization flow. // `immediate` should false when invoked button click. var useimmdiate = event ? false : true; var authdata = { client_id: client_id, scope: scopes, immediate: useimmdiate }; gapi.auth.authorize(authdata, function(response) { var authbutton = document.getelementbyid('auth-button'); if (response.error) { authbutton.hidden = false; } else { authbutton.hidden = true; queryaccounts(); } }); } function queryaccounts() { // load google analytics client library. gapi.client.load('analytics', 'v3').then(function() { // list of google analytics accounts user gapi.client.analytics.management.accounts.list().then(handleaccounts); }); } function handleaccounts(response) { // handles response accounts list method. if (response.result.items && response.result.items.length) { // first google analytics account. var firstaccountid = response.result.items[0].id; // query properties. queryproperties(firstaccountid); } else { console.log('no accounts found user.'); } } function queryproperties(accountid) { // list of properties account. gapi.client.analytics.management.webproperties.list( {'accountid': accountid}) .then(handleproperties) .then(null, function(err) { // log errors. console.log(err); }); } function handleproperties(response) { // handles response webproperties list method. if (response.result.items && response.result.items.length) { // first google analytics account var firstaccountid = response.result.items[0].accountid; // first property id var firstpropertyid = response.result.items[0].id; // query views (profiles). queryprofiles(firstaccountid, firstpropertyid); } else { console.log('no properties found user.'); } } function queryprofiles(accountid, propertyid) { // list of views (profiles) first property // of first account. gapi.client.analytics.management.profiles.list({ 'accountid': accountid, 'webpropertyid': propertyid }) .then(handleprofiles) .then(null, function(err) { // log errors. console.log(err); }); } function handleprofiles(response) { // handles response profiles list method. if (response.result.items && response.result.items.length) { // first view (profile) id. var firstprofileid = response.result.items[0].id; // query core reporting api. querycorereportingapi(firstprofileid); } else { console.log('no views (profiles) found user.'); } } function querycorereportingapi(profileid) { // query core reporting api number sessions // past 7 days. gapi.client.analytics.data.ga.get({ 'ids': 'ga:' + profileid, 'start-date': '7daysago', 'end-date': 'today', 'filters':'ga:city%3d%3dirvine', 'dimensions':'ga:browser', 'metrics': 'ga:pageviews' }) .then(function(response) { var formattedjson = json.stringify(response.result, null, 2); document.getelementbyid('query-output').value = formattedjson; }) .then(null, function(err) { // log errors. console.log(err); }); } // add event listener 'auth-button'. document.getelementbyid('auth-button').addeventlistener('click', authorize); </script> <script src="https://apis.google.com/js/client.js?onload=authorize"></script> </body> </html>
as stated in doc, filters encoding examples final http requests
https://www.googleapis.com/analytics/v3/data/ga ?ids=ga:12134 &dimensions=ga:browser &metrics=ga:pageviews &filters=ga:browser%3d~%5efirefox &start-date=2007-01-01 &end-date=2007-12-31
but api doing encoding.
so replacing %3d%3d
==
in filter ga:city%3d%3dirvine
job.
Comments
Post a Comment