问题描述
Google Analytics 正确报告了我的 Android 应用引发的异常.我可以使用预定电子邮件将这个报告发送给我.但是,在没有任何报告(即报告告诉我发生零异常)时每天收到一封电子邮件是乏味的.因此,我希望仅在有要报告的事情时才收到电子邮件(即,报告告诉我发生了一个或多个异常).似乎自定义警报可以用于此目的.但是,自定义警报似乎与例外不兼容.这引出了我的问题.
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 daily email when there isn't anything to report (i.e., the report tells me that zero exceptions occurred) is tedious. Thus, I'd like to receive emails only when there is something to report (i.e., the report tells me that one or more exceptions occurred). It seems that Custom Alerts can be used for this purpose. However, Custom Alerts do not appear to be compatible with Exceptions. This leads me to my question.
是否可以配置自定义警报以提供有关异常的电子邮件通知?
或者,更一般地说,
是否可以将 Google Analytics(分析)配置为针对例外情况提供电子邮件通知?
此外,这也适用于崩溃吗?
Also, does this work for crashes too?
更新(2015 年 11 月 22 日,2015 年 12 月 1 日)
(部分)答案.我提供了一个答案,使服务器(不是 Google Analytics)能够配置为提供有关异常的电子邮件通知,这可能是对于许多人来说,这是一个足够的解决方案.
(Partial) answer. I provide an answer that enables a server (not Google Analytics) to be configured to provide email notification on exceptions, which is probably a sufficient solution for many.
(几乎是)答案.jakub-kriz 提供了一个 详细的答案,但它不能按原样工作.基于答案,我能够将 Google Analytics 配置为在没有异常发生时发送电子邮件.这与所需的完全相反.很遗憾,当出现一个或多个异常时,我一直无法收到电子邮件.
(Almost an) answer. jakub-kriz has provided a detailed answer, but it does not work as-is. Building upon the answer, I was able to configure Google Analytics to email when no exceptions occur. This is the exact opposite of what is required. Unfortunately, I have been unable to get emails when one or more exceptions occur.
替代方向.jakub-kriz 提出了一个 替代解决方案,其中使用正常事件,而不是异常事件.我没试过这个方向.
Alternate direction. jakub-kriz has proposed an alternative solution, whereby normal events are used, rather than exception events. I haven't tried this direction.
尚未提出完整的解决方案.
推荐答案
可以配置服务器(不是 Google Analytics)来提供有关异常的电子邮件通知,这对于许多人来说可能是一个足够的解决方案.
A server (not Google Analytics) can be configured to provide email notification on exceptions, which is probably a sufficient solution for many.
首先,您需要一个服务帐户,可以创建https://console.developers.google.com/project/_/apiui/credential.您将创建一个密钥文件 (MyAnalytics.p12
).
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
).
其次,我们配置我们的分析客户端 (MyAnalytics.php
):
Secondly, we configure our analytics client (MyAnalytics.php
):
<?php
//You'll need to install google-api-php-client
//(https://github.com/google/google-api-php-client)
require_once 'Google/autoload.php';
class MyAnalytics
{
//When logged into Google Analytics you'll have a URL that looks
//something like https://www.google.com/analytics/web/?authuser=0#home/a00w11p22/
//Your profile id is everything after the p
const PROFILE_ID = '22';
//This is the service account email that you constructed in step 1
const SERVICE_ACCOUNT_EMAIL = '[email protected]';
//This is the file that you constructed in step 1.
const KEY_FILE_LOCATION = 'MyAnalytics.p12';
private $client;
private $analytics;
private $cred;
public function __construct() {
$this->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;
}
}
?>
第三,我们查询和报告异常(exceptions.php
):
Thirdly, we query and report on exceptions (exceptions.php
):
<?php
require_once 'MyAnalytics.php';
$myAnalytics = new MyAnalytics();
$analytics = $myAnalytics->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)
}
?>
第四,配置cron运行exceptions.php
(参见https://stackoverflow.com/a/22358929/3664487).
Fourth, configure cron to run exceptions.php
(cf. https://stackoverflow.com/a/22358929/3664487).
这篇关于如果(崩溃和)异常发生,Google Analytics 可以给我发电子邮件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!