简体   繁体   English

Google Analytics API-Analytics属性授权

[英]Google Analytics API - Authorization on Analytics Properties

I'm using Google Analytics API. 我正在使用Google Analytics(分析)API。 I need to know if exists a function that returns me a boolean value (or something like that) telling me if the logged google account has the rights to show the charts, but I didn't find anything similar in the API. 我需要知道是否存在一个返回布尔值(或类似值)的函数,该函数告诉我登录的Google帐户是否有权显示图表,但我在API中找不到任何类似的东西。

So I tried to do something different, I tried to handle the error when I draw the charts with the function execute(), using the callback as explained here: https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide#build-a-core-reporting-api-query but my code doesn't call that callback. 因此,我尝试做一些不同的事情,当我使用函数execute()绘制图表时,尝试使用该回调处理错误,如下所示: https : //developers.google.com/analytics/devguides/reporting/core / v3 / coreDevguide#build-a-core-reporting-api-query,但我的代码未调用该回调。

Here is my code: 这是我的代码:

var dataChart = new gapi.analytics.googleCharts.DataChart({
    query: {
      ids: ga_property_id,
      metrics: 'ga:sessions, ga:users',
      dimensions: 'ga:date',
    },
    chart: {
      container: 'chart-container',
      type: 'LINE',
      options: {
        width: '100%',
        fontSize: 14
      }
    }
});

function makeApiCall() {
    dataChart.set({query: dateRange});
    dataChart.execute(handleCoreReportingResults);
}

function handleCoreReportingResults(results) {
    if (!results.error) 
    {
        console.log("success");
    } 
    else 
    {
        console.log("fail");
    }
}

makeApiCall();

I tried also to put the execute() into a try/catch but it works without catch, I guess because the exception comes from the gapi. 我也尝试将execute()放入try / catch中,但是它没有捕获就可以工作,我想是因为异常来自gapi。

Finding a way to know if the logged user has rights on the property will be better, but it will be really good also fixing this code and make it works! 找到一种方法来确定登录用户是否对该属性具有权限会更好,但同时修复此代码并使之正常工作也将是一件好事!

Thanks in advance! 提前致谢!

You could call the accounts Summaries API and find the complete list of accounts to which the user has access. 您可以调用帐户Summaries API并找到用户有权访问的帐户的完整列表。

summary = gapi.client.analytics.management.accountSummaries.list();

And then loop the results to verify that the property or view are among the list. 然后循环结果以验证属性或视图是否在列表中。

But you could also include a view selector component and in that way you would only ever be using view Ids to which the user has access. 但是您还可以包括一个视图选择器组件,这样您将只能使用用户有权访问的视图ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM