Query multiple ID's on Google Analytics Javascript API

耗尽温柔 提交于 2020-01-04 04:57:22

问题


I'm using Google Analytic's API for javascript.

I'm performing a query for the user's count of a data range, and passing the view ID to the query:

gapi.client.analytics.data.ga.get({
    'ids': 'ga:XXXXXXXX',
    'start-date': '2014-10-01',
    'end-date': '2014-10-15',
    'metrics': 'ga:users'
}).execute(function (results) {
    if (results.rows && results.rows.length)
        console.log(results.rows[0][0]);
});

However, since I'm dealing with the quotas of usage, and I needed to query for multiple views.

Is there a way to request the report for more than one id in the same query?

Something like:

 gapi.client.analytics.data.ga.get({
    'ids': 'ga:XXXXXXXX, ga:XXXXXXXXX, ga:XXXXXXXXXX', //Which obviously doesn't work

回答1:


Answer: No there is no way to request more then one profile id

Google Analtyics Reporting API reference

ids=ga:12345 The unique table ID of the form ga:XXXX, where XXXX is the Analytics view (profile) ID for which the query will retrieve the data.

Even though it is called IDS does not mean that you can send more then one. You can't you can only send one at a time. It is a unique value you are going to have to run your request several times. If you think about it this makes sense as the data you would be getting back would be mixed up between the different profile id's you wouldn't be able to tell which profile the data came from as profile is not a dimension.




回答2:


This answer may be a little late, but I would like to help those that may have a similar use case. With Google Analytics Reporting API V4 I found a way to do this while also pulling information for 3 separate date ranges on a table of potentially hundreds of rows.

Under the field metrics you can add an alias. Here is where I passed along the View ID and the Date range information I needed to parse the JSON response.

The following is based off of Google's Hello Analytics example - there is probably a much better way to do this but this meets my needs at the current time.

I list below the way the table structured along with the functioning javascript. Table is setup that way so I can also use list.js to sort.

var VIEW_ID = ''
 function runReport() {
     //tbody has id gReport and I want to loop through each row
     $("#gReport tr").each(function() {
      //VIEW_ID is pulled from the table data
         VIEW_ID = $(this).find('.view').html();
         // Run the function with the set time frames along with a td I need to manipulate
         queryReports('30daysAgo', '.thirty')
         queryReports('7daysAgo', '.seven')
         queryReports('yesterday', '.one')
     });
 };

function queryReports(startDate, tdClass) {
     return gapi.client.request ({
         path: '/v4/reports:batchGet',
         root: 'https://analyticsreporting.googleapis.com/',
         method: 'POST',
         body: {
             reportRequests: [
                 {
                     viewId: VIEW_ID,
                     dateRanges: [
                         {
                             startDate: startDate,
                             endDate: 'today'
                         }
                                     ],
                     metrics: [
                         {
                             expression: 'ga:sessions',
                             alias: VIEW_ID + ":" + tdClass
                         }
                     ]
                 }
             ]
         }
     }).then(displayResults, console.error.bind(console));
 }
 function displayResults(response) {
 // find the alias string in JSON
     i = response.result.reports[0].columnHeader.metricHeader.metricHeaderEntries[0].name
     // manipulate string so I have variables to update on the table
     c = i.split(":").pop();
     u = i.split(":").shift();
     // return value given to me by google
     countValue = response.result.reports[0].data.totals[0].values[0]
     // find specific td of the row that contains the profile id and the time class I want to manage
     $("tr:contains('" + u + "')").find(c).html(countValue);
}
<!--- table row setup - I also have a tbody with the ID gReport -->
<tr>
   <td class="site"><a href="https://"></a></td>
   <td class="proj"><a href=""></a></td>
   <td class="ga"></td>
   <td class="view"></td>
   <td class="thirty"></td>
   <td class="seven"></td>
   <td class="one"></td>
   <td class="notes"></td>
</tr>



回答3:


Yes, accouding to GA Core Reporting API documentation you're allowed to query only one View ID at a time (in the same query).

However, you might decide to opt for some programming language scripts and iterate through multiple View IDs when you run the query. That is what I've done using the R language and a package named RGoogleAnalytics (which allows connecting to GA API). Running a for loop allowed me to retrieve data from multiple View IDs and store it in the same dataset. Here is a post explaining how to do this in R: http://www.analyticsforfun.com/2015/05/query-multiple-google-analytics-view.html



来源:https://stackoverflow.com/questions/26389336/query-multiple-ids-on-google-analytics-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!