Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // 20180414 Robin MQTT
- require("phpMQTT.php");
- $server = "192.168.178.67"; // change if necessary
- $port = 1883; // change if necessary
- $username = ""; // set your username
- $password = ""; // set your password
- $client_id = "GoogleAnalyticsPublisher"; // make sure this is unique for connecting to sever - you could use uniqid()
- // Diesen Bereich anpassen!!!
- $service_account_email = 'g.com'; //Dienstkonto-Email-Adresse
- $key_file_location = __DIR__ . '/GoogleAnalytics-xxxxxxxx.p12'; //p12-Konto-File
- $profile = 199999993; //ID der Datenansicht
- require_once __DIR__ . '/src/Google/autoload.php';
- class MKleine_Google_Analytics_API
- {
- protected $serviceAccountEmail = null;
- protected $keyFile = null;
- protected $profileId = null;
- protected $analyticsRef = null;
- function __construct($serviceAccountEmail, $keyFile, $profileId)
- {
- $this->serviceAccountEmail = $serviceAccountEmail;
- $this->keyFile = $keyFile;
- $this->profileId = $profileId;
- }
- /**
- * @return Google_Service_Analytics
- */
- protected function getService()
- {
- if (is_null($this->analyticsRef)) {
- // Create and configure a new client object.
- $client = new Google_Client();
- $client->setApplicationName("GoogleAnalytics");
- $this->analyticsRef = new Google_Service_Analytics($client);
- // Read the generated client_secrets.p12 key.
- $key = file_get_contents($this->keyFile);
- $cred = new Google_Auth_AssertionCredentials(
- $this->serviceAccountEmail,
- array(Google_Service_Analytics::ANALYTICS_READONLY),
- $key
- );
- $client->setAssertionCredentials($cred);
- if ($client->getAuth()->isAccessTokenExpired()) {
- $client->getAuth()->refreshTokenWithAssertion($cred);
- }
- }
- return $this->analyticsRef;
- }
- /**
- * @param $startDate
- * @param $endDate
- * @param $metrics
- * @return bool|int
- */
- public function getResults($startDate, $endDate, $metrics)
- {
- // Calls the Core Reporting API and queries for the number of sessions
- // for the last seven days.
- $data = $this->getService()->data_ga->get(
- 'ga:' . $this->profileId,
- $startDate,
- $endDate,
- $metrics
- );
- return $this->parseResults($data);
- }
- /**
- * @param $metrics
- * @return bool|int
- */
- public function getRealtimeResults($metrics)
- {
- $data = $this->getService()->data_realtime->get(
- 'ga:' . $this->profileId,
- $metrics
- );
- return $this->parseResults($data);
- }
- /**
- * @param $results
- * @return bool|int
- */
- protected function parseResults($results)
- {
- // Parses the response from the Core Reporting API and prints
- // the profile name and total sessions.
- if (count($results->getRows()) > 0) {
- // Get the entry for the first entry in the first row.
- $rows = $results->getRows();
- $sessions = $rows[0][0];
- return (int)$sessions > 0 ? (int)$sessions : false;
- } else {
- return false;
- }
- }
- }
- $obj = new MKleine_Google_Analytics_API(
- $service_account_email,
- $key_file_location,
- $profile
- );
- /* echo $obj->getResults('7daysAgo', 'today', 'ga:pageviews').PHP_EOL; */
- /* echo $obj->getRealtimeResults('rt:activeUsers'); */
- /* echo $obj->getResults('7daysAgo', 'today', 'ga:pageviews'); */
- $mqtt = new phpMQTT($server, $port, $client_id);
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $mqtt->publish("/fhem/Web/ActiveUsers", $obj->getRealtimeResults('rt:activeUsers'), 0);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $mqtt->publish("/fhem/Web/PageviewsMonth", $obj->getResults(date('Y-m-01'), 'today', 'ga:pageviews'), 0);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $mqtt->publish("/fhem/Web/PageviewsToday", $obj->getResults(date('Y-m-d'), 'today', 'ga:pageviews'), 0);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $mqtt->publish("/fhem/Web/UserMonth", $obj->getResults(date('Y-m-01'), 'today', 'ga:users'), 0);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
- if ($mqtt->connect(true, NULL, $username, $password)) {
- $mqtt->publish("/fhem/Web/UserToday", $obj->getResults(date('Y-m-d'), 'today', 'ga:users'), 0);
- $mqtt->close();
- } else {
- echo "Time out!\n";
- }
Add Comment
Please, Sign In to add comment