本文介绍了如果(崩溃和)例外发生,Google Analytics(分析)能否向我发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Analytics 正确报告我的Android应用程序抛出的异常。我可以使用预定电子邮件将此报告发送给我。然而,当没有任何要报告的情况下(即报告告诉我发生零例外情况),每天收到一封电子邮件是很乏味的。因此,我只希望在需要报告的情况下收到电子邮件(即报告告诉我发生了一个或多个异常情况)。看起来 Custom Alerts 可以用于此目的。但是,自定义警报似乎与例外不兼容。这使我想到我的问题。



可以将自定义提醒配置为提供异常电子邮件通知吗?

b $ b

或者更一般地说,

可以将Google Analytics配置为提供异常电子邮件通知吗?



另外,它是否也可以用于崩溃?

UPDATE(2015年11月22日,2015年12月1日)
$ b (部分)答案。我提供,使服务器(不是Google Analytics)能够配置为提供异常电子邮件通知,这可能对于很多人来说这是一个充分的解决方案。

(几乎是)答案。 提供了,但它现在不起作用。基于这个答案,我可以配置Google Analytics在发生异常时发送电子邮件。这与所要求的完全相反。不幸的是,当发生一个或多个例外情况时,我一直无法收到电子邮件。



替代方向。 提出了,从而使用正常事件,而不是异常事件。我还没有尝试过这个方向。


解决方案

服务器(而不是Google Analytics)可以配置为提供异常电子邮件通知,可能足够首先,您需要一个服务帐户,可以创建 https://console.developers.google.com/project/_/apiui/credential 。您将创建一个密钥文件( MyAnalytics.p12 )。

其次,我们配置我们的分析客户端( MyAnalytics.php ):

 <?php 
/ /您需要安装google-api-php-client
(https://github.com/google/google-api-php-client)
require_once'Google / autoload.php ;

class MyAnalytics
{
//登录到Google Analytics时,您会看到一个网址,该网址看起来像
//类似于https://www.google。 com / analytics / web /?authuser = 0#home / a00w11p22 /
//您的个人资料ID是p
const PROFILE_ID ='22'后的所有内容。

//这是您在步骤1中构建的服务帐户电子邮件
const SERVICE_ACCOUNT_EMAIL ='blah@developer.gserviceaccount.com';

//这是您在步骤1中构建的文件。
const KEY_FILE_LOCATION ='MyAnalytics.p12';

私人$客户;
私人$分析;
私人$ cred;

public function __construct(){
$ this-> client = new Google_Client();
$ this-> analytics =新Google_Service_Analytics($ this->客户端);
$ 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-> analytics;
}
}

?>

第三,我们查询并报告异常( exceptions.php

 <?php 
require_once'MyAnalytics.php';

$ myAnalytics = new MyAnalytics();
$ analytics = $ myAnalytics-> getAnalytics();

$ results = $ analytics-> data_ga-> get(
'ga:'。MyAnalytics :: PROFILE_ID,
'昨天',
'今天',
'ga:例外'
);

$ a = $ results-> getTotalsForAllResults();
$ count = $ a ['ga:exceptions'];

echo $ count;

if(is_numeric($ count)&& $ count> 0){
//处理异常,例如发送一封电子邮件
// https://stackoverflow.com/a/5335311/3664487)
}
?>

第四,配置cron运行 exceptions.php (参见)。


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.

Can Custom Alerts be configured to provide email notification on exceptions?

Or, more generally,

Can Google Analytics be configured to provide email notification on exceptions?

Also, does this work for crashes too?

UPDATE (22 Nov 2015, 1 Dec 2015)

(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.

(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.

Alternate direction. jakub-kriz has proposed an alternative solution, whereby normal events are used, rather than exception events. I haven't tried this direction.

A complete solution has not yet been proposed.

解决方案

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):

<?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 = 'blah@developer.gserviceaccount.com';

    //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;
    }
}

?>

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)
    }       
?>

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

这篇关于如果(崩溃和)例外发生,Google Analytics(分析)能否向我发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:10