Skip to content

Google Apps Script to query Google Analytics Realtime API on a schedule and send custom email alerts when user-defined metric thresholds are reached.

License

Notifications You must be signed in to change notification settings

99-metrics/ga-realtime-alerting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ga-realtime-alerting

Uses Google Apps Script to query the Google Analytics Realtime API on a schedule - every 5, 10, 15 or 30 minutes - and sends an email alert when user-defined metric thresholds are reached.

Example use cases

  1. Error alerts

Server errors are problems for any service, and you want to know about them straight away and if you don't have a separate service to monitor these errors Google Analytics is a good way to monitor them - so make sure they're tagged. Using the Real time API and this tool you could monitor 5XX errors and get an email alert if your service starts return them.

For this example you'd set your alerts as below. The first alert would send you an email if a service sees more than 5 500 errors in the past 30 min but the second alert would send an email if 5 of any internal server alert was seen by a user (500,501,502 etc) and show yuo which errors had been seen. Using this configuration this alert would be sent at any time day or night on any day. Once an alert email has been sent, the tool would wait 60 min before sending another alert.

  1. More then/fewer than expected number of views?

Do you expect a certain number of certain events during the working week? The first alert would alert you if more than your expected number of 'submissions' were seen the second if less..

The first alert would check if you’d had more than 500 submissions and send and email to you (and the head of warehouse!) to warn you there was a tonne of submissions coming in, because you know you’ll go in investigate this immediately this alert won’t send another email for 2 hours. The second alert will send an email if there have been fewer than 100 submissions in the last 30 min.

The emails aren’t super pretty and will look like the example below but you could add html if you wanted to pretty them up (see the bolding using strong tags in the google sheet cell). If you add a dimension as shown in example 1. you’ll get a basic table of the break down.

You could be clever and make your thresholds dynamic based on the latest GA information or change based on the time of day using google sheets formula… enjoy

Setup guide

Make a copy of the script template

From this read-only version of the script

This will hopefully be up to date with the latest version of the scripts but it's worth checking!

When you've made your copy you need to set up the script - jump to the script set up guide

or copy scripts from GitHub

To do this create a brand new Google Sheet and go to Tools > Script editor from the menu.

Create 6 scripts

called... (case sensitive)

Alerting, Common, Config-Sheet, Dims-Mets, Menu, Version,

Copy and paste the scripts from the raw GitHub versions (delete the “function myFunction() {}”)

Save your script (and name it)

Script setup guide

Go to Tools > Script Editor

then Resources > Advanced Google services.

ScreenShot

Turn on Google Analytics API v3.

Click the link to Google Developers Console.

ScreenShot

The Developers Console will open in a new tab.

Search for and select the Analytics API NOT Google Analytics Reporting API

Enable the API and close the Developers Console tab.

Click Okay to close the Advanced Google Services dialog box.

An updating settings message will display at the top of the screen.

ScreenShot

The script is now setup and you can close the Script Editor tab.

Create Alerts

Refresh the browser - This generates the App menus

You can either edit the example Alert provided in the sheet or

Go to GA Realtime > Create Configuration Sheet in the menu

The first time you run the script (by creating new alerts or config sheet or checking your alerts) you will be prompted to grant some permissions (text depends on what you named your script).

Select Continue and Allow permissions in the next window.

This is a standard alert/warning as the app script can access your GA data and the Google Sheet.

Click Advanced.

Click Go to Untitled project (unsafe) - (or whatever you called your script).

Add your parameters to the config sheet - To see tips for competing each section hover over row titles

To add another alert go to GA Realtime > Add new alert from the menu.

To check your alerts will run successfully go to GA Realtime > Check Alert Configurations

To schedule your alerts go to GA Realtime > Schedule Alerting and selected a checking frequency

Collecting anonymous usage data

We use Google Analytics to understand how many users/sheets are using this script and how often. To do this, a random anonymous client ID is generated for each sheet and stored in script properties. No sensitive data about the user or spreadsheet will be sent to Google Analytics.

To diable sending analytics data you can change the 'SEND_ANALYTICS_DATA' variable (line 15: Alerting.gs) to false.

Authors

  • Ed Guccione - Initial work - @triweasel
  • Dan Gilbert - Initial work - @dangilbertnow

License

This project is licensed under the MIT License - see the LICENSE file for details

About

Google Apps Script to query Google Analytics Realtime API on a schedule and send custom email alerts when user-defined metric thresholds are reached.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published