Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pobieranie danych z API google analytics
chomikiki
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 1
Dołączył: 16.09.2016
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Cześć,
Niestety przeszukując zasoby internetu nie mogę znaleźć tutoriala jak pobrać dane na swoją stronę. Jak już znalazłam jakieś kody to nie działają, albo ja ich nie potrafię użyć (to drugie bardziej prawdopodobne). Doszłam do przycisku logowania, ale tez wyskakuje błąd i nie bardzo wiem co dalej. Zna ktoś może jakieś tutoriale, albo powie co po kolei powinnam zrobić? Jestem wzrokowcem i najszybciej kumam o co chodzi jak widzę przykład. Niestety jedyne przykłady jakie znalazlam dotyczą umieszczenia wtyczki do pobierania danych przez google a nie pobierania danych Z google (IMG:style_emoticons/default/sad.gif) Bardzo proszę o pomoc

Ten post edytował chomikiki 21.09.2016, 11:08:37
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
chomikiki
post
Post #2





Grupa: Zarejestrowani
Postów: 14
Pomógł: 1
Dołączył: 16.09.2016
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


mam cos takiego

  1. require_once 'google-api-php-client/src/Google/autoload.php'; // jest w głównym folderze
  2.  
  3.  
  4. $analytics = initializeAnalytics();
  5. $profile = getFirstProfileId($analytics);
  6. $results = getResults($analytics, $profile);
  7. printResults($results);
  8.  
  9. function initializeAnalytics()
  10. {
  11. // Creates and returns the Analytics Reporting service object.
  12.  
  13. // Use the developers console and download your service account
  14. // credentials in JSON format. Place them in this directory or
  15. // change the key file location if necessary.
  16. $KEY_FILE_LOCATION = __DIR__ . '/[...]/client_secret.json'; // w tym samym miejscu co ten plik
  17.  
  18. // Create and configure a new client object.
  19. $client = new Google_Client();
  20. $client->setApplicationName("Hello Analytics Reporting");
  21. $client->setAuthConfig($KEY_FILE_LOCATION);
  22. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  23. $analytics = new Google_Service_Analytics($client);
  24.  
  25. return $analytics;
  26. }
  27.  
  28. function getFirstProfileId($analytics) {
  29. // Get the user's first view (profile) ID.
  30.  
  31. // Get the list of accounts for the authorized user.
  32. $accounts = $analytics->management_accounts->listManagementAccounts();
  33.  
  34. if (count($accounts->getItems()) > 0) {
  35. $items = $accounts->getItems();
  36. $firstAccountId = $items[0]->getId();
  37.  
  38. // Get the list of properties for the authorized user.
  39. $properties = $analytics->management_webproperties
  40. ->listManagementWebproperties($firstAccountId);
  41.  
  42. if (count($properties->getItems()) > 0) {
  43. $items = $properties->getItems();
  44. $firstPropertyId = $items[0]->getId();
  45.  
  46. // Get the list of views (profiles) for the authorized user.
  47. $profiles = $analytics->management_profiles
  48. ->listManagementProfiles($firstAccountId, $firstPropertyId);
  49.  
  50. if (count($profiles->getItems()) > 0) {
  51. $items = $profiles->getItems();
  52.  
  53. // Return the first view (profile) ID.
  54. return $items[0]->getId();
  55.  
  56. } else {
  57. throw new Exception('No views (profiles) found for this user.');
  58. }
  59. } else {
  60. throw new Exception('No properties found for this user.');
  61. }
  62. } else {
  63. throw new Exception('No accounts found for this user.');
  64. }
  65. }
  66.  
  67. function getResults($analytics, $profileId) {
  68. // Calls the Core Reporting API and queries for the number of sessions
  69. // for the last seven days.
  70. return $analytics->data_ga->get(
  71. 'ga:' . $profileId,
  72. '7daysAgo',
  73. 'today',
  74. 'ga:sessions');
  75. }
  76.  
  77. function printResults($results) {
  78. // Parses the response from the Core Reporting API and prints
  79. // the profile name and total sessions.
  80. if (count($results->getRows()) > 0) {
  81.  
  82. // Get the profile name.
  83. $profileName = $results->getProfileInfo()->getProfileName();
  84.  
  85. // Get the entry for the first entry in the first row.
  86. $rows = $results->getRows();
  87. $sessions = $rows[0][0];
  88.  
  89. // Print the results.
  90. print "First view (profile) found: $profileName\n";
  91. print "Total sessions: $sessions\n";
  92. } else {
  93. print "No results found.\n";
  94. }
  95. }


i nic... dostaje pustą stronę. Nie ma błędów ale też nie ma wyniku. Co robię nie tak?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 10.10.2025 - 16:27