Add Google Analytics to a Shiny app
Google Analytics is a free service offered by Google that collects information about who visits your website and what they do while they are there. You can learn more about Google Analytics at support.google.com/analytics, which explains how to set up and use Google Analytics with a web page.
From time to time Google changes how they do content tracking. The following information is current as of March 2020. If you find that the information is out of date at any point, please let us know on RStudio Community.
Add analytics to an app
You can use Google Analytics with a Shiny app, since Shiny apps are a type of web page. In this article, we will add Google Analytics to the Sunshine app, pictured below.
You can add analytics to the app (and collect results) with the five steps below.
Step 1 - Create an account
To use Google Analytics, you must open a free account at google.com/analytics.
Step 2 - Add a property
Next, you must register your Shiny app as a property in your Google Analytics account. You can do this on the sign up page, or—if you already have a Google Analytics account—you can do this in the Admin tab.
You will need to provide a web address for your app to Google Analytics. Since the Sunshine app is hosted at https://gallery.shinyapps.io/google-analytics, I’ll use this address.
This script will allow Google Analytics to track traffic to and from your app. To use it, you’ll need to put the script in the head of your app’s DOM, the subject of Step 3.
Step 3 - Embed tracking script
You should place the Google Analytics tracking script at the end of the head section of the HTML DOM that describes your Shiny app.
This is very easy to do if you build your Shiny app around an HTML file, as described in Build your entire UI with HTML.
If you built your Shiny app with a app.R file (the traditional method), use the
includeHTML functions to include the script.
For example, Google Analytics has given us the script below to embed in the Sunshine app. Note that the numbers following
UA will be different for you, they are an identifier for the content you created on Google Analytics.
To include the script in your app, first save it to its own file. Here I’ve saved the script as a file named google-analytics.html, which I have placed in the working directory of the Sunshine app.
To embed the script in the Sunshine app, call
tags$head(includeScript("google-analytics.js")) in the ui.R file.
includeScript will import the script and pass it to
tags$head(), which will place the script in the head section of your app’s DOM.
includeHTML requires one argument:
"google-analytics.html", the file path from the App directory to the google-analytics.html file.
The final ui of my app will look like the following.
Since the app is hosted on shinyapps.io, I will need to redeploy the app to shinyapps.io for the new app.R file to take effect.
The Google Analytics script tracks how visitors move from one web page to the next. With it, you can learn a little about:
- who visits your app
- where they come from
- how long they stay on the app while they are there
You can also use Google Analytics to track how visitors use your app, but to do that you will need to set up specific event trackers.
Step 4 - Create event trackers
An event tracker is a piece of code that notifies Google Analytics whenever a visitor interacts with a specific part of your app, such as a link or a widget. You create a separate event tracker for each unique event that you want to track.
With Google Analytics, you use the basic script to track how people move to and from your page, and you use event trackers to track what they do while they are there. Note that you will need to embed the basic Google Analytics tracking script into your app before you create any event trackers. Event trackers will not work without the basic script.
When the element executes the command,
ga sends an event notification to Google Analytics that let’s Google Analytics know that an event occurred. The notification will contain the values of
value that you have supplied for this type of event. Later on, you will be able to see these values, as well as when the event occurred, from your Google Analytics dashboard.
For example, you can track when users click on a specific link by having the link’s
onClick attribute call
This works for tracking events on simple web elements, but you must use a different approach to track events on Shiny widgets.
Shiny does not let you set arbitrary attributes on widgets when you create them. To set an event tracker on a Shiny widget, you will need to identify and modify the widget after it has been created, which you can do with a jQuery script.
For example, the Sunshine app has two widgets: a select box and a button. I would like to track how users interact with each of the widgets. To do this I will need to create two event trackers, one for the select box widget and one for the button widget. I will also need to set these event trackers on the widgets with jQuery.
The following script attaches an event handler to the select box widget. The handler will execute for change events that occur on the widget. In other words, the tracker will notify Google Analytics whenever a visitor changes the value of the select box widget.
I’ve chosen to have the event report include “widget” for the
category value and “select data” for the
action value. The last argument will return the value of the new selection as the
label argument. This tracker will not return a
value arguments are optional.
I can track the app’s “Update Plot” button in a similar way. The script below will send an event notification whenever a user clicks on a button class object in the DOM, e.g. the “Update Plot” button:
Note: to write effective jQuery code, you will need to be able to uniquely identify the widgets that you wish to track. This may require you to explore the document structure of the finished app, for example in your browser’s developer tools console.
When working with Shiny apps, use jQuery code that creates delegated event handling, like the code above does. Delegated events work more nimbly with the dynamic nature of Shiny apps than do direct events.
Now, that I’ve written the code that will allow event tracking, I need to add it to the app.R file of the Sunshine app. The easiest way to do this is to include the code in the google-analytics.html file that gets added to the app’s head.
My final google-analytics.html file will look like this:
I’ll need to redeploy the app to shinyapps.io to make sure the hosted version of the app uses this code.
Step 5 - Collect reports
Once you have set up your app to use Google Analytics, you can use your account portal as a dashboard to track traffic on your app. The Real-Time tracking features should begin almost immediately, but other tracking features may take a couple hours to a couple days before they start reporting results.
You can also extract and analyze traffic data from Google Analytics data with R. The googleAnalyticsR package has extensive documentation with some examples.
Add Google Analytics with Shiny Server Open Source and Shiny Server Pro
Alternatively, you can use Shiny Server to add Google Analytics to your apps. This will let you manage your Google Analytics configuration at a higher level—so if you want all apps deployed on a server to share Google Analytics code, you could put this code at the top level of your config and all your apps would automatically inherit it. To learn more, visit the Shiny Server user guide.
To add Google Analytics to a Shiny app:
- Set up a Google Analytics account.
- Add the Shiny app to the account as a web property.
- Place the Google Analytics tracking script into the head section of your app’s DOM.
- Create event trackers to track specific events within your app. The easiest way to do this is to modify Shiny widgets with delegated event handlers managed by jQuery.
- Use R or your Google Analytics dashboard to explore the results.
Google Analytics isn’t the only website monitoring service, but it is the most popular. You can add similar services to your app with the same techniques.
These techniques serve as a proof of concept that will allow you to track how visitors use your apps. We will look to make it easier to track Shiny apps in future developement.
If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community. Our developers monitor these forums and answer questions periodically. See help for more help with all things Shiny.