2015-07-12 133 views
1

我想要的内容:网站应显示没有授权的分析数据。从Google AnalyticsAPI获取数据

我做了什么:我有一个在谷歌应用程序引擎和启用API的应用程序,并创建了一个JSON文件的服务帐户。

我试图做同样的:https://ga-dev-tools.appspot.com/embed-api/custom-components/但没有成功。

然后,我遇到了这个问题:http://code.google.com/p/analytics-issues/issues/detail?id=496和改变了我的代码,因为这

<!DOCTYPE html> 
<html> 
<head> 
    <title>Embed API Demo</title> 
</head> 
<body> 

<!-- Step 1: Create the containing elements. --> 

<section id="auth-button"></section> 
<section id="view-selector"></section> 
<section id="timeline"></section> 

<!-- Step 2: Load the library. --> 

<script> 
(function(w,d,s,g,js,fjs){ 
    g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}}; 
    js=d.createElement(s);fjs=d.getElementsByTagName(s)[0]; 
    js.src='https://apis.google.com/js/platform.js'; 
    fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')}; 
}(window,document,'script')); 
</script> 

<script> 
gapi.analytics.ready(function() { 

    // Step 3: Authorize the user. 


    gapi.analytics.auth.authorize({ 
    serverAuth: '<server auth key>' 
    }); 

    // Step 4: Create the view selector. 

    var viewSelector = new gapi.analytics.ViewSelector({ 
    container: 'view-selector' 
    }); 

    // Step 5: Create the timeline chart. 

    var timeline = new gapi.analytics.googleCharts.DataChart({ 
    reportType: 'ga', 
    query: { 
     'dimensions': 'ga:date', 
     'metrics': 'ga:sessions', 
     'start-date': '30daysAgo', 
     'end-date': 'yesterday', 
    }, 
    chart: { 
     type: 'LINE', 
     container: 'timeline' 
    } 
    }); 

    // YOU MUST CALL THIS MANUALLY HERE INSTEAD OF WAITING FOR CALLBACK 
    viewSelector.execute(); 

    // Step 6: Hook up the components to work together. 

    gapi.analytics.auth.on('success', function(response) { 
    viewSelector.execute(); 
    }); 

    viewSelector.on('change', function(ids) { 
    var newIds = { 
     query: { 
     ids: ids 
     } 
    } 
    timeline.set(newIds).execute(); 
    }); 
}); 
</script> 
</body> 
</html> 

我试着看到几个文件,以获得服务器授权密钥,但我没得到它。

任何人都可以帮助设置服务器身份验证密钥,并通过这将我的目的显示图表没有身份验证将解决?

感谢, 沙拉德·瑞里

+0

此问答可能有帮助:http://stackoverflow.com/questions/31057968/showing-google-analytics-data-in-real-time/31058732#31058732 –

回答

1

Embeded API Getting started

The Embed API handles almost all of the authorization process for you by providing a one-click sign-in component that uses the familiar OAuth 2.0 flow. In order to get this button working on your page you'll need a client ID.

嵌入式API的设计与Oauh2工作,并要求用户进行身份验证它不是设计与服务帐户的工作。我从来没有见过使用JavaScript的服务帐户的代码。这可能是由于我自己包括的很多人并不认为客户端JavaScript中的服务帐户是安全的,甚至可能会违反Goggles的新使用条款。

如果您想使用服务帐户,则需要切换到服务器端语言进行身份验证。嵌入式API只需使用Google图表,以便您也可以手动编码。

+0

感谢DalmTo,你可以分享任何有用的文档来实现PHP授权。 –

+0

(谷歌教程)https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php(我的教程)http://www.daimto.com/google_service_account_php/ – DaImTo

+1

不必要地nitpicky实际上有一种情况下,这与JavaScript的工作(在这里讨论:http://stackoverflow.com/questions/28070463/google-analytics-embed-api-how-to-retireve-access-token)。但是,在node.js中,JS在服务器上执行,密钥文件永远不能通过浏览器访问。 –

相关问题