CoolKitchen

Google analytics MQTT

Apr 17th, 2018
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. <?php
  2.  // 20180414 Robin MQTT
  3.  
  4.  require("phpMQTT.php");
  5. $server = "192.168.178.67";     // change if necessary
  6. $port = 1883;                     // change if necessary
  7. $username = "";                   // set your username
  8. $password = "";                   // set your password
  9. $client_id = "GoogleAnalyticsPublisher"; // make sure this is unique for connecting to sever - you could use uniqid()
  10.  
  11.  
  12.  
  13.  
  14. // Diesen Bereich anpassen!!!
  15. $service_account_email = 'g.com'; //Dienstkonto-Email-Adresse
  16. $key_file_location = __DIR__ . '/GoogleAnalytics-xxxxxxxx.p12'; //p12-Konto-File
  17. $profile = 199999993; //ID der Datenansicht
  18.  
  19. require_once __DIR__ . '/src/Google/autoload.php';
  20.  
  21. class MKleine_Google_Analytics_API
  22. {
  23.     protected $serviceAccountEmail = null;
  24.     protected $keyFile = null;
  25.     protected $profileId = null;
  26.  
  27.     protected $analyticsRef = null;
  28.  
  29.     function __construct($serviceAccountEmail, $keyFile, $profileId)
  30.     {
  31.         $this->serviceAccountEmail = $serviceAccountEmail;
  32.         $this->keyFile = $keyFile;
  33.         $this->profileId = $profileId;
  34.     }
  35.  
  36.     /**
  37.      * @return Google_Service_Analytics
  38.      */
  39.     protected function getService()
  40.     {
  41.         if (is_null($this->analyticsRef)) {
  42.             // Create and configure a new client object.
  43.             $client = new Google_Client();
  44.             $client->setApplicationName("GoogleAnalytics");
  45.             $this->analyticsRef = new Google_Service_Analytics($client);
  46.  
  47.             // Read the generated client_secrets.p12 key.
  48.             $key = file_get_contents($this->keyFile);
  49.             $cred = new Google_Auth_AssertionCredentials(
  50.                 $this->serviceAccountEmail,
  51.                 array(Google_Service_Analytics::ANALYTICS_READONLY),
  52.                 $key
  53.             );
  54.  
  55.             $client->setAssertionCredentials($cred);
  56.             if ($client->getAuth()->isAccessTokenExpired()) {
  57.                 $client->getAuth()->refreshTokenWithAssertion($cred);
  58.             }
  59.         }
  60.  
  61.         return $this->analyticsRef;
  62.     }
  63.  
  64.     /**
  65.      * @param $startDate
  66.      * @param $endDate
  67.      * @param $metrics
  68.      * @return bool|int
  69.      */
  70.     public function getResults($startDate, $endDate, $metrics)
  71.     {
  72.         // Calls the Core Reporting API and queries for the number of sessions
  73.         // for the last seven days.
  74.         $data = $this->getService()->data_ga->get(
  75.             'ga:' . $this->profileId,
  76.             $startDate,
  77.             $endDate,
  78.             $metrics
  79.         );
  80.  
  81.         return $this->parseResults($data);
  82.     }
  83.  
  84.     /**
  85.      * @param $metrics
  86.      * @return bool|int
  87.      */
  88.     public function getRealtimeResults($metrics)
  89.     {
  90.         $data = $this->getService()->data_realtime->get(
  91.             'ga:' . $this->profileId,
  92.             $metrics
  93.         );
  94.  
  95.         return $this->parseResults($data);
  96.     }
  97.  
  98.     /**
  99.      * @param $results
  100.      * @return bool|int
  101.      */
  102.     protected function parseResults($results)
  103.     {
  104.         // Parses the response from the Core Reporting API and prints
  105.         // the profile name and total sessions.
  106.         if (count($results->getRows()) > 0) {
  107.  
  108.             // Get the entry for the first entry in the first row.
  109.             $rows = $results->getRows();
  110.             $sessions = $rows[0][0];
  111.  
  112.             return (int)$sessions > 0 ? (int)$sessions : false;
  113.         } else {
  114.             return false;
  115.         }
  116.     }
  117.  
  118. }
  119.  
  120. $obj = new MKleine_Google_Analytics_API(
  121.     $service_account_email,
  122.     $key_file_location,
  123.     $profile
  124. );
  125.  
  126.  
  127. /* echo $obj->getResults('7daysAgo', 'today', 'ga:pageviews').PHP_EOL; */
  128. /* echo $obj->getRealtimeResults('rt:activeUsers'); */
  129.  
  130. /* echo $obj->getResults('7daysAgo', 'today', 'ga:pageviews'); */
  131.  
  132. $mqtt = new phpMQTT($server, $port, $client_id);
  133.  
  134. if ($mqtt->connect(true, NULL, $username, $password)) {
  135.     $mqtt->publish("/fhem/Web/ActiveUsers", $obj->getRealtimeResults('rt:activeUsers'), 0);
  136.     $mqtt->close();
  137. } else {
  138.     echo "Time out!\n";
  139. }
  140.  
  141.  
  142.  
  143. if ($mqtt->connect(true, NULL, $username, $password)) {
  144.         $mqtt->publish("/fhem/Web/PageviewsMonth", $obj->getResults(date('Y-m-01'), 'today', 'ga:pageviews'), 0);
  145.         $mqtt->close();
  146. } else {
  147.     echo "Time out!\n";
  148. }
  149.  
  150.  
  151. if ($mqtt->connect(true, NULL, $username, $password)) {
  152.         $mqtt->publish("/fhem/Web/PageviewsToday", $obj->getResults(date('Y-m-d'), 'today', 'ga:pageviews'), 0);
  153.         $mqtt->close();
  154. } else {
  155.     echo "Time out!\n";
  156. }
  157.  
  158.  
  159.  
  160. if ($mqtt->connect(true, NULL, $username, $password)) {
  161.         $mqtt->publish("/fhem/Web/UserMonth", $obj->getResults(date('Y-m-01'), 'today', 'ga:users'), 0);
  162.         $mqtt->close();
  163. } else {
  164.     echo "Time out!\n";
  165. }
  166.  
  167. if ($mqtt->connect(true, NULL, $username, $password)) {
  168.         $mqtt->publish("/fhem/Web/UserToday", $obj->getResults(date('Y-m-d'), 'today', 'ga:users'), 0);
  169.         $mqtt->close();
  170. } else {
  171.     echo "Time out!\n";
  172. }
Add Comment
Please, Sign In to add comment