Skip to main content

Automating email alerts for ‘trending’ content

Posted by: , Posted on: - Categories: Data insights, Google Analytics, Google Sheets, Performance Platform

I recently blogged about some of the ways we’ve been using real-time data in GDS. In this post I’ll be covering how we’ve been working with a couple of government departments to alert them of recently viewed popular content.

Intelligence alerts

With a site the size and complexity of GOV.UK it’s not always easy to see where significant user behaviour has occured. This is where intelligence alerts functionality in analytics vendors can be a real help - it can automatically detect significant statistical variations in traffic and notify when those variations have occurred. It’s quite a powerful but underused tool that can highlight many insights which might have otherwise been missed in the mass of data.

Google Analytics, our current vendor, provides an “Intelligence Events” feature through which two types of alerts can be generated.

Automatic alerts are triggered when Google Analytics finds a significant change in site traffic patterns. For example, the screenshot below shows that referrals from social media to GOV.UK decreased by 10% on 4th February compared with the same day the week before.

Example of an automatic alert
Example of an automatic alert

Custom alerts are triggered when traffic hits a specific threshold that you have manually configured in the interface. We’ve set up several custom alerts, such as the example below, to notify us that the site has gone down or the Google Analytics code has been taken off.

Example of a custom alert configuration
Example of a custom alert configuration

Limitations in the user interface

Although there are many different alerts that can be set up within Google Analytics one of the major drawbacks we’ve had is that it’s not possible to create alerts for departmental content as a whole. Departmental content is defined by a page-level custom variable, a metadata type value in the source code. Unfortunately, configuring alerts by custom variables is not currently available in the Google Analytics interface so we’ve had to get more creative.

No.10 user needs

The digital team at No.10 (the Prime Minister’s office) approached us as they were quite keen on being notified if any of their tagged content was getting a recent spike in visits. Rather than having to constantly go into the Google Analytics interface to monitor traffic activity they wanted to be automatically alerted if any of their content received more than 250 visits in the past hour. They also wanted to be informed if any of their content received more than 200 visits from Facebook or Twitter.


Working with our Google Analytics Partner, we wrote a Magic Script to pull usage data in the past hour and to send an email to members of the No.10 digital team if the above thresholds were reached.

Here are the settings to make the Google Analytics API calls:

Settings for the GA API calls
Settings for the GA API calls

In the above configuration, the ‘ga:hour’ value within the ‘filters’ setting is dynamically populated with the value from the preceding hour.  With some small additional tweaks, we used the built-in Google Spreadsheet function ‘=now()’ to obtain the dynamic values.

A custom written script then sends an email out to designated addresses if a value (or values) is present. Actual addresses used have been blanked for privacy in the screenshot below but each is separated by a comma.

Alerts script to send out email
Alerts script to send out email

The ‘control’ function is set to run every hour using the built-in Google Spreadsheet triggers.  In a nut shell we’re passing a refined query (including a filter that targets a specific hour) to the GA API and then sending an automated email to a defined distribution list if the response contains data.  Note that if no data is returned by the GA API, then the data sheets are blank and the email is not sent because the last row value returned within the runAlert function is not greater than zero.

Here’s an example of an email alert which was as a result of a tweet from @David_Cameron (note that alerts are based on visits in the last fixed hour e.g. 2pm - 3pm, 3pm - 4pm etc). It shows that the main Prime Minister’s Office page was visited 258 times between 12:00 and 1:00pm on this particular day.

Example of an email alert
Example of an email alert

Further development

The No.10 digital team have found these automated alerts very useful. We’ve also configured them for the digital team at the Treasury  which they found particularly helpful when the Autumn Statement was published.

We’re going to continue iterating this feature to make it more more flexible and user friendly. We’ll also be looking at ways to extend this out across government departments and possibly integrate it into the GOV.UK Performance Platform.

If you have any suggestions on how we can develop this alerts feature or have any interesting related stories please do share in the comments section below.

Sharing and comments

Share this page