Google Analytics Glossary

Add Google Analytics to Alpine JS

Image of Iron Brands

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

Alpine.js does not have built-in support for analytics, but you can manually integrate Google Analytics for better insights on user behavior. Here is how to do it, step by step.

Let's dive in!

  1. Integrating Google Analytics
    1. Set up Google Analytics
    2. Include GA Tracking Code
    3. Track Page Views and Events
    4. Testing and Verification
    5. (Optional) Create New Properties
  2. Final Thoughts

Before we dig in you need to know that Google Analytics is complex and a bit clunky. There are other options that give you the same insights in a slick and straightforward dashboard.

Simple Analytics is one of them. A privacy-friendly and simple analytics tool - just the insights you need in a straightforward dashboard. (And its also free yes).

Can you spot the difference between the dashboards?

All right, now let's get into answering your question!

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.

Include GA Tracking Code

  • Inspect the property and copy your GA tracking code
  • Add the tracking code to the main HTML file of your Alpine.js application.

Track Page Views and Events

Since Alpine.js is often used in SPAs (Single Page Applications) and does not have a built-in router, you'll need to manually handle page view tracking:

  1. Page View Tracking: Attach a method to handle page view tracking whenever the URL changes. This can be done using Alpine.js directives or JavaScript:

        function trackPageView() {
       const path = window.location.pathname;
       gtag('config', 'YOUR_MEASUREMENT_ID', {'page_path': path});
     }
    
     window.addEventListener('popstate', trackPageView);
     

Make sure to replace 'YOUR_MEASUREMENT_ID' with your actual measurement ID from your GA property!

  1. Event Tracking: For specific events like button clicks, you can use Alpine.js’s @click directive:

        <button @click="gtag('event', 'click', {'event_category': 'button', 'event_label': 'MyButton'}); trackButtonEvent()">Click me</button>
     

Testing and Verification

After implementing GA, test your application to ensure that data is being captured correctly. Tools like the Google Analytics Debugger for Chrome can help with debugging.

(Optional) Create New Properties

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.

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!