Can Google Analytics email me if (crashes and) exceptions occur?

后端 未结 3 1002
悲&欢浪女
悲&欢浪女 2021-02-12 10:03

Google Analytics is correctly reporting exceptions thrown by my Android app. And I can use Scheduled Emails to send this report to me. However, receiving a dai

3条回答
  •  孤城傲影
    2021-02-12 10:24

    A server (not Google Analytics) can be configured to provide email notification on exceptions, which is probably a sufficient solution for many.

    First, you need a service account, which can be created https://console.developers.google.com/project/_/apiui/credential. You'll create a key file (MyAnalytics.p12).

    Secondly, we configure our analytics client (MyAnalytics.php):

    client = new Google_Client();
            $this->analytics = new Google_Service_Analytics($this->client);
            $key = file_get_contents(self::KEY_FILE_LOCATION);
    
            $this->cred = new Google_Auth_AssertionCredentials(
              self::SERVICE_ACCOUNT_EMAIL,
              array(Google_Service_Analytics::ANALYTICS_READONLY),
              $key
            );
        }
    
        public function getAnalytics() {
            $this->client->setAssertionCredentials($this->cred);
    
            if($this->client->getAuth()->isAccessTokenExpired()) {
               $this->client->getAuth()->refreshTokenWithAssertion($this->cred);
            }
    
            return $this->analytics;
        }
    }
    
    ?>
    

    Thirdly, we query and report on exceptions (exceptions.php):

    getAnalytics();
    
        $results = $analytics->data_ga->get(
             'ga:' . MyAnalytics::PROFILE_ID,
             'yesterday',
             'today',
             'ga:exceptions'
        );
    
        $a = $results->getTotalsForAllResults();
        $count = $a['ga:exceptions'];
    
        echo $count;
    
        if (is_numeric($count) && $count > 0) {
            //handle the exception, e.g., send an email
            //(cf. https://stackoverflow.com/a/5335311/3664487)
        }       
    ?>
    

    Fourth, configure cron to run exceptions.php (cf. https://stackoverflow.com/a/22358929/3664487).

提交回复
热议问题