How to use Siri as a Voice User Interface for Google Analytics

Fredrik Cederlöf
Jun 2 · 5 min read

This might not be the most effective way of receiving audio based insights, but it works (actually both robust and fast) and maybe it will inspire others to start building automated insights through a Voice User Interface using a similar method.

Generated audio file from RStudio Server and Google Analytics to Cloud Text-to-Speech
Using Shortcuts to iOS to trigger audio-file from Google Cloud Storage

My obsession with automation is timewise unhealthy, but everything that suits to be automated should be according to me. In the following example, we’re going to let Siri trigger a generated audio file containing data insights (through a voice command) which are located on Cloud Storage. This is possible using the app Shortcuts for iOS. The audio-file is originally generated in R with RStudio Server hosted on a VM on Google Cloud where I every week collect Google Analytics data and then convert text to a *.wav file with the Google Text-to-Speech API. Finally, the file is uploaded to a storage bucket. And there you have it.

I’m not going through how to set up a Virtual Machine on Google Cloud, but I do recommend to visit Linus Larsson’s post Scheduling data imports in Google Cloud with RStudio before going any further.

Step 1. Make the API Call

We’re going to stay in our VM in the Google Cloud environment using RStudio Server. First, we load our libraries that we need to use and authorize googleAuthR to access our Google Analytics data and receive an authentication token. For more information on how to extract GA data in R, please visit Mark Edmonson. In the end, we use cronR to schedule our script to run every Monday morning.

Next, we set our GA ID and date range for our given time period. We’re going to forecast expected weekly conversion for 5 different web goals so let’s set our metrics and dimensions to match these criteria’s.

When your data is received, hopefully without any errors you can inspect your stored data frame by writing View(df). I do recommend to extend your data range for better predictions and also rename your columns to something more descriptive.

Data Frame containing date, users, and goals

Step 2. Conduct Forecasting

Now let’s prepare for forecasting using the library(forecast) and Auto ARIMA modeling to reduce the number of steps and find the best-suited model for predicting values. I did some tests and evaluation before choosing the right forecast model and ARIMA has always been very close to the actual result.

We calculate the forecast for each goal based on our time period and store the values in a separate list. Then we create a new data frame for each goal and round values.

Forecast values

Now check your newly created data frame to fully understand what’s going on. The Lo and Hi represent the lower and higher upper bounds of each prediction interval. A 95 % confidence means that you’re 95 % confident that the lowest conversions for a specific day will be 1.06 and 18.28 at the highest. Quite a large safety margin 😱. Instead, we’re going to use Point.Forecast which gives us the estimated median value.

Step 3. Create a Storage Bucket

So, we got a seven-day forecast for all our goals, well done! Next, we create a Cloud Storage Bucket to enable our audio-file to be stored in the cloud. It’s important that you assign permissions (1) so that the file (2) can be publicly accessed (3).

Cloud Storage

Before creating our audio file and uploading it to Cloud Storage we need to set an environment variable with googleCloudStorageR. You can specify the location of a service account with a JSON file taken from your Google Project. In my case, this file is stored in the RStudio Repository on my VM. The file is uploaded with the command gcs_upload(filename).

Step 4. Convert Text-to-Speech & upload file

Google Cloud Text-to-Speech converts text into human-like speech in more than 100 voices across 20+ languages and variants. Since the Swedish voice sounded like a 25-year-old female without feelings I rather work with a more happy life voice from down under. input controls what message that should be converted to speech. There is a bunch of settings you can do, I would recommend testing different language codes and audio settings before uploading. The file forecast.wav will be stored in my root-folder. Then we upload i to Cloud Storage.

Step 5. Schedule script with cronR

This is one of the reasons why we choose to run the script in the cloud and not locally since we don’t want to be dependent on a local machine to run the script.

After installing the package cronR you need to start the service. Open the Terminal in RStudio and write sudo cron start and hit enter. Then in RStudio navigate to “Tools — Addins — Browse Addins..” and choose cronR from the list. Select your Rscript from your directory, then set the date and hour including schedule routine for your script to run and then create your cron job. The audio-file will be replaced every week. Well done! 👏🏻 As you can imagine, there is plenty of possibilities for several other scheduled scripts to be created.

Step 6. Use iOS Shortcuts to play your audio-file

Shortcuts on iOS

We move on to the last thing to do. First, go to Cloud Storage and copy the public URL-link of your forecast.wav file (https://storage.googleapis.com/your-bucket/forecastSound. Then download and open Shortcuts on your iPhone. Click here from your iOS device to copy the Shortcuts template.

Now you can easily create your own Siri phrase that should trigger the shortcut or you can simply place it on your home screen. There you go!

Generated audio file from RStudio Server and Google Analytics to Cloud Text-to-Speech

I would really appreciate any feedback and hear about your own results using a similar method for creating audio-based insights.

Fredrik Cederlöf

Written by

Head of Customer Experience & Analytics at Collector Bank, Sweden. Enjoys crunching data and build data-driven organizations.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade