Enable Google Analytics for Your Shiny App

Learn how to set up and configure Google Analytics for your Shiny apps to track user interactions and improve your app’s performance.
Analytics
Free-Tier
Author

Juan C. Rodriguez

Published

December 2, 2024

Keywords

Shiny, Google Analytics, Web Analytics, R

Why Add Google Analytics to Your Shiny App?

Understanding how users interact with your Shiny app is key to improving its performance and user experience. Google Analytics provides powerful tools to track app usage, identify popular features, and gain actionable insights. Best of all, it’s free to get started.

In this guide, we’ll walk you through the process of integrating Google Analytics with your Shiny app, step by step.


Step 1: Create a Google Analytics Account

  1. Visit Google Analytics, and click “Start measuring”.

Click 'Start measuring'.

  1. Fill in your “Account details”, agree to the terms, and click “Next”.

Fill the account details and click 'Next'.

Step 2: Set Up Your Property

  1. Enter your “Property details”, such as your app name and time zone, then click “Next”.

Fill the property details and click 'Next'.

  1. Provide your “Business details”, and click “Next”.

Fill the business details and click 'Next'.

  1. Select your business objectives (e.g., measure user engagement), and click “Create”.

Select objectives and click 'Create'.

  1. Accept the terms of service to proceed, and click “I Accept”.

Read the ToS and click 'I Accept'.

Step 3: Configure Your Data Stream

  1. Select the “Web” platform.

Select 'Web' platform.

  1. Enter the public “Website URL” of your Shiny app, give a “Stream name”, and click “Create & continue”.

Enter your app's URL and stream name, and click 'Create & continue'.

  1. Copy the Google Analytics ID generated for your app (e.g., G-ABCD12E34F).

Copy the Google Analytics ID.

Step 4: Add Google Analytics to Your Shiny App

To connect your Shiny app to Google Analytics:

  1. Include the Google Analytics gtag code in your app’s UI. To simplify this, use the pre-written R function from this gist. Copy the use_google_analytics function definition in your code, or directly source the gist:
source(paste0(
  "https://gist.githubusercontent.com/jcrodriguez1989/a6a9c4168262e99ef2f9c70a110886ab/raw/",
  "ea6b48e1cdaaed2839524ad700a930f6d17583d2/ga_setup.R"
))
  1. Update your Shiny app’s UI code, it should look smimilar as shown below:
ui <- fluidPage(
  use_google_analytics("G-ABCD12E34F"),
  ...
  1. Re-deploy your app to your hosting platform.

Step 5: Test Your Installation

  1. Return to the Google Analytics dashboard and click “Test installation”.

Click 'Test installation'.

  1. If everything is set up correctly, you’ll see a success message confirming that Google Analytics is tracking your app.

Google Analytics tag success message.

Final Step: Monitor Your Data

  1. Wait a few hours for data collection to begin. Then, use the Google Analytics dashboard to explore user interactions, track app performance, and gain insights into how users engage with your app.

Wait for data to start appearing.

Google Analytics at work.


Congratulations!

You’ve successfully integrated Google Analytics into your Shiny app. With this setup, you can make data-driven decisions to improve your app and deliver a better experience for your users.

Let me know in the comments if you have any questions or need further assistance.