Pobieram na stronie dane z google analytics za pomocą API
Kod:
// Replace with your client ID from the developer console. var CLIENT_ID = '******'; // Replace with your view ID. var VIEW_ID = '******'; // Set the discovery URL. var DISCOVERY = 'https://analyticsreporting.googleapis.com/$discovery/rest'; // Set authorized scope. var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']; function authorize(event) { // Handles the authorization flow. // `immediate` should be false when invoked from the 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; queryReports(); } }); } function queryReports() { // Load the API from the client discovery URL. var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); newUsers = 0; if(dd<10) { dd='0'+dd } if(mm<10) { mm='0'+mm } gapi.client.load(DISCOVERY ).then(function() { // Call the Analytics Reporting API V4 batchGet method. gapi.client.analyticsreporting.reports.batchGet( { "reportRequests":[ { "viewId":VIEW_ID, "dateRanges":[ { "startDate":"2005-01-01", }], "metrics":[ { "expression":"ga:users" }], "dimensions": [ { "name":"ga:day" }] }] }).then(function(response) { //var formattedJson = JSON.stringify(response.result, null, 2); //document.getElementById('query-output').value = formattedJson; var allUsers = response.result.reports[0].data.totals[0].values[0]; $('.widget-users .result').html(numberFormat(allUsers,',')); return allUsers; }).then(null, function(err) { // Log any errors. console.log(err); }); // Call the Analytics Reporting API V4 batchGet method. gapi.client.analyticsreporting.reports.batchGet( { "reportRequests":[ { "viewId":VIEW_ID, "dateRanges":[ { "startDate":"2005-01-01", }], "metrics":[ { "expression":"ga:newUsers" }], "dimensions": [ { "name":"ga:day" }] }] }).then(function(response) { //var formattedJson = JSON.stringify(response.result, null, 2); //document.getElementById('query-output').value = formattedJson; var newUsers = response.result.reports[0].data.totals[0].values[0]; $('.widget-new-users .result').html(numberFormat(newUsers,',')); return newUsers; }).then(null, function(err) { // Log any errors. console.log(err); }); // Call the Analytics Reporting API V4 batchGet method. gapi.client.analyticsreporting.reports.batchGet( { "reportRequests":[ { "viewId":VIEW_ID, "dateRanges":[ { }], "metrics":[ { "expression":"ga:newUsers" }], "dimensions": [ { "name":"ga:day" }] }] }).then(function(response) { //var formattedJson = JSON.stringify(response.result, null, 2); //document.getElementById('query-output').value = formattedJson; var todayUsers = response.result.reports[0].data.totals[0].values[0]; $('.widget-today-users .result').html(numberFormat(todayUsers,',')); return todayUsers; }).then(null, function(err) { // Log any errors. console.log(err); }); }); } // Add an event listener to the 'auth-button'. document.getElementById('auth-button').addEventListener('click', authorize); alert(newUsers+' '+allUsers+' '+todayUsers);
Chciałbym w poźniejszym etapie użyć wartości w newUsers, allUsers i todayUsers np do załadawania ich do wykresów. Tylko nie mam dostępu do tych wartości, pojawia się komunikat że nie są zdefiniowane. Jak je wcześniej definiuje to i tak nie przyjmują właściwych wartości pobrany z google analysic.
Ten skrypt jest wzięty z dokumentacji i działa poprawnie. Nie wiem jak przerobić aby przypisać poprawinie te wartości i użyć w dalszej części kodu strony.
Pozdrawiam