2015-07-12 101 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在服務器上執行,密鑰文件永遠不能通過瀏覽器訪問。 –