Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Google Analytics Reporting API V4
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
kamilo818
Hej
Pobieram na stronie dane z google analytics za pomocą API
Kod:
  1.  
  2. // Replace with your client ID from the developer console.
  3. var CLIENT_ID = '******';
  4. // Replace with your view ID.
  5. var VIEW_ID = '******';
  6. // Set the discovery URL.
  7. var DISCOVERY = 'https://analyticsreporting.googleapis.com/$discovery/rest';
  8. // Set authorized scope.
  9. var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];
  10.  
  11. function authorize(event) {
  12. // Handles the authorization flow.
  13. // `immediate` should be false when invoked from the button click.
  14. var useImmdiate = event ? false : true;
  15. var authData = {
  16. client_id: CLIENT_ID,
  17. scope: SCOPES,
  18. immediate: useImmdiate
  19. };
  20. gapi.auth.authorize(authData, function(response) {
  21. var authButton = document.getElementById('auth-button');
  22. if (response.error) {
  23. authButton.hidden = false;
  24. }
  25. else {
  26. authButton.hidden = true;
  27. queryReports();
  28. }
  29. });
  30. }
  31.  
  32.  
  33.  
  34.  
  35. function queryReports() {
  36. // Load the API from the client discovery URL.
  37. var today = new Date();
  38. var dd = today.getDate();
  39. var mm = today.getMonth()+1; //January is 0!
  40. var yyyy = today.getFullYear();
  41. newUsers = 0;
  42. if(dd<10) {
  43. dd='0'+dd
  44. }
  45. if(mm<10) {
  46. mm='0'+mm
  47. }
  48. var date = yyyy+'-'+mm+'-'+dd;
  49.  
  50. gapi.client.load(DISCOVERY
  51. ).then(function() {
  52.  
  53. // Call the Analytics Reporting API V4 batchGet method.
  54. gapi.client.analyticsreporting.reports.batchGet( {
  55. "reportRequests":[
  56. {
  57. "viewId":VIEW_ID,
  58. "dateRanges":[
  59. {
  60. "startDate":"2005-01-01",
  61. "endDate":date
  62. }],
  63. "metrics":[
  64. {
  65. "expression":"ga:users"
  66. }],
  67. "dimensions": [
  68. {
  69. "name":"ga:day"
  70. }]
  71. }]
  72. }).then(function(response) {
  73. //var formattedJson = JSON.stringify(response.result, null, 2);
  74. //document.getElementById('query-output').value = formattedJson;
  75. var allUsers = response.result.reports[0].data.totals[0].values[0];
  76. $('.widget-users .result').html(numberFormat(allUsers,','));
  77. return allUsers;
  78. }).then(null, function(err) {
  79. // Log any errors.
  80. console.log(err);
  81. });
  82.  
  83. // Call the Analytics Reporting API V4 batchGet method.
  84. gapi.client.analyticsreporting.reports.batchGet( {
  85. "reportRequests":[
  86. {
  87. "viewId":VIEW_ID,
  88. "dateRanges":[
  89. {
  90. "startDate":"2005-01-01",
  91. "endDate":date
  92. }],
  93. "metrics":[
  94. {
  95. "expression":"ga:newUsers"
  96. }],
  97. "dimensions": [
  98. {
  99. "name":"ga:day"
  100. }]
  101. }]
  102. }).then(function(response) {
  103. //var formattedJson = JSON.stringify(response.result, null, 2);
  104. //document.getElementById('query-output').value = formattedJson;
  105. var newUsers = response.result.reports[0].data.totals[0].values[0];
  106. $('.widget-new-users .result').html(numberFormat(newUsers,','));
  107. return newUsers;
  108. }).then(null, function(err) {
  109. // Log any errors.
  110. console.log(err);
  111. });
  112.  
  113. // Call the Analytics Reporting API V4 batchGet method.
  114. gapi.client.analyticsreporting.reports.batchGet( {
  115. "reportRequests":[
  116. {
  117. "viewId":VIEW_ID,
  118. "dateRanges":[
  119. {
  120. "startDate":date,
  121. "endDate":date
  122. }],
  123. "metrics":[
  124. {
  125. "expression":"ga:newUsers"
  126. }],
  127. "dimensions": [
  128. {
  129. "name":"ga:day"
  130. }]
  131. }]
  132. }).then(function(response) {
  133. //var formattedJson = JSON.stringify(response.result, null, 2);
  134. //document.getElementById('query-output').value = formattedJson;
  135. var todayUsers = response.result.reports[0].data.totals[0].values[0];
  136. $('.widget-today-users .result').html(numberFormat(todayUsers,','));
  137. return todayUsers;
  138. }).then(null, function(err) {
  139. // Log any errors.
  140. console.log(err);
  141. });
  142.  
  143.  
  144. });
  145. }
  146. // Add an event listener to the 'auth-button'.
  147. document.getElementById('auth-button').addEventListener('click', authorize);
  148.  
  149.  
  150. alert(newUsers+' '+allUsers+' '+todayUsers);
  151.  


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
LowiczakPL
Próbowałeś zadeklarować globalne zmienne poza funkcją a w funkcjach przypisać do tych zmiennych wartości ?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.