Google Analytics Glossary

Add Google Analytics to Ruby on Rails

Image of Iron Brands

Published on Nov 23, 2023 and edited on Apr 7, 2024 by Iron Brands

Adding Google Analytics to your Ruby on Rails app can help you understand user behavior and improve user experience. Here is how to add GA to your app, step by step.

Let's dive in!

  1. Integrating Google Analytics
    1. Set up Google Analytics
    2. Install a JavaScript Tag Manager (Optional)
    3. Add the GA4 Tracking Code to Your Application
    4. Verify the Integration
    5. Customize Event Tracking (Optional)
    6. Create New Properties (Optional)
  2. Final Thoughts

Before we dig in I want to show you something. I promise it's worth it...

Google Analytics is great, but also complex and a bit clunky. If you just want a straightforward dashboard with the insights you need, GA is not a great place to start. Additionally, Google doesn't care about privacy and GA requires an annoying cookie banner.

That's why I built Simple Analytics, a privacy-friendly and simple analytics tool - no personal data, no cookies, just the insights you need in a straightforward dashboard.

Here is how it looks vs GA. Feel free to check our live analytics to get an idea for your project. (It is free btw)

All right, enough about us. Now let's get into answering your question!

Can you spot the difference between the dashboards?

Integrating Google Analytics

Set up Google Analytics

  • Log into your GA profile (or create one, if you haven't already).
  • Create a GA property for your app.
  • Inspect the property and note down your Measurement ID. You will need it later.

Install a JavaScript Tag Manager (Optional)

Consider using Google Tag Manager to handle your tags. This step is optional but can make managing your scripts easier, especially if you plan to add other tracking tools in the future.

Add the GA4 Tracking Code to Your Application

Add the GA4 tracking code to your application layout. This ensures the tracking code loads on every page. The code should be placed in the head tag of your application layout (app/views/layouts/application.html.erb):

<head>
  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-XXXXXXXXXX');
  </script>
  ...
</head>

Make sure to replace G-XXXXXXXXXX with your actual GA4 Measurement ID!.

Verify the Integration

To confirm the integration, visit your site after deploying the changes and then check the real-time reports in Google Analytics. If your visits are being recorded, the setup is successful.

Customize Event Tracking (Optional)

GA4 is event-driven, so you might want to customize event tracking based on user interactions specific to your application. You can add event tracking code to the user interactions you want to track.

For instance, to track clicks on a specific button:

document.getElementById("your-button-id").addEventListener("click", function() {
  gtag('event', 'click', {
    'event_category': 'Button',
    'event_label': 'Your Button Label'
  });
});

This code sends an event to GA4 whenever the specified button is clicked.

Create New Properties (Optional)

To track multiple apps, create more GA properties and use the corresponding Measurement IDs. This prevents GA from conflating the data.

Final Thoughts

Adding Google Analytics to your app can give you great insights. However, ask yourself: is Google Analytics the right tool for you?

GA is an overpowered solution for straightforward analytics. If you're looking for a simple and intuitive dashboard with the insights you need, there are better alternatives. Yes, I’m talking about my own product (Simple Analytics), but there are others out there as well.

I hated using Google Analytics for my projects. It's clunky, there are hundreds of dashboards and it doesn't look appealing. Also Google doesn't care about privacy or ethics. That's why I decided to build my own and more intuitive web analytics tool.

If this resonates with you, feel free to give Simple Analytics a spin. You just need to add the script to your app and off you go. This takes about one minute- and there is a free version as well!

Enjoy!