22

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.

Community
  • 1
  • 1
user2768
  • 794
  • 8
  • 31

3 Answers3

10

It is possible, but not in a direct way, you have to hookup you analytics quite dirty.

1) Configuration in Analytics Admin

Create two filters in Admin -> View -> Filters -> Custom -> Advanced

Create filter that listens on hitType exception and set Event Category - Exception

Filter hitType to Event Category

Create filter that replicates Exception description into Event Action

enter image description here

2) Create custom Goal

Create two filters in Admin -> View -> Goals -> Custom -> Event

Event Category equals Exception

Event Category equals Exception

3) Create Custom Alert

Custom alert by Goal containg exception

Custom alert by Goal containg exception

Do not forget your email

enter image description here

Try this and let me know!

Jakub Kriz
  • 1,501
  • 2
  • 21
  • 29
  • The only confusing step for me was "Event Category equals Exception". Also, it doesn't appear necessary to "Add an email address", since an email address is already registered. (Perhaps you could add some additional details for future readers.) I now need to test whether it works! – user2768 Nov 16 '15 at 19:05
  • It doesn't seem to be working: I've triggered exceptions (which are being reported by Analytics), but I'm not receiving emails. – user2768 Nov 18 '15 at 20:54
  • Are you able to modify the code sending the exception hit? – Jakub Kriz Nov 18 '15 at 21:48
  • 1
    Yes, I can modify the code, with some limitations. However, I don't think that's it. As I mentioned above, the only confusing bit for me was "Event Category equals Exception", I assumed this meant: during step 2, define "category" "equals to" "Exception". However, when I "Verify this Goal", I get a "0% conversion rate", which probably isn't right. (I suspect "Value" "greater than" "0" should be set instead, this produces a "25% conversion rater.") – user2768 Nov 18 '15 at 21:56
  • I you can make modifications, add normal event with excception call, where you will have EventCategory Eception, EventAction - ExceptionDescription and you can remova that filters and just keep goal and alert on. – Jakub Kriz Nov 19 '15 at 09:32
  • As-is, this solution does *not* work. Building upon the solution, 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. – user2768 Nov 20 '15 at 20:37
0

To get report on Mail Id there is no way to send directly from google analytics. We can send this error report handling it and send it to programmatically to mail id from our app.

Darshan Mothreja
  • 506
  • 4
  • 13
  • This code allows an app to report to Google Analytics, it does not send address the problem. – user2768 Nov 18 '15 at 16:16
  • The above answer has been edited. It certainly contains errors. For instance, it is possible to send email directly from Google Analytics. It is also nonsensical -- at least, to me -- in parts. – user2768 Nov 22 '15 at 13:43
0

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

Community
  • 1
  • 1
user2768
  • 794
  • 8
  • 31