Drupal, Google Analytics 4 and commerce events

In today’s fast-evolving digital world, Google is set to retire Universal Analytics (Google Analytics 3) and replace it with Google Analytics 4 (GA4). This is a major change that website owners and developers need to prepare for. If you’re using Drupal and Drupal Commerce, this transition can be especially tough to navigate.

The current Drupal module for Google Commerce Analytics (Commerce Google Tag Manager), even if updated (and it won't be updated because, according to the official module page, the module is marked as obsolete and unsupported) to support commerce events for GA4, raises some reliability concerns due to its underlying architecture. This could potentially affect the tracking of essential e-commerce metrics, which are vital for businesses to stay competitive in the online market.

Recognizing this challenge, I created a new module called GA Server Side to ensure seamless continuity of analytics for Drupal Commerce users. The new module leverages Google's Measurement Protocol to send event data directly to GA4 via HTTPS.

This module operates by using event subscribers. What this means is that it listens and reacts to specific e-commerce events as they occur on your site. When an event such as add_to_cart or purchase happens, the module builds the necessary data corresponding to that event. This data is then securely transmitted directly to Google Analytics via HTTPS, ensuring your e-commerce metrics are consistently tracked and updated.

Image

This represents a considerable upgrade from the older method that the Commerce Google Tag Manager module uses, which involves a complex and error-prone process of sending data from Drupal to JavaScript, which was then picked up by Google Tag Manager and then finally sent to Google Analytics.

Image

My module eliminates this cumbersome process, resulting in a more streamlined, efficient, and accurate data transmission to Google Analytics. As a result, users can continue to monitor their commerce events and derive valuable insights without interruption, even as GA3 approaches its end of life on July 1, 2023.

The module is designed to support a variety of e-commerce events crucial for comprehensive analysis, including:

  • view_item
  • add_to_cart
  • remove_from_cart
  • begin_checkout
  • purchase

This ensures that you have a well-rounded view of your customer's journey, leading to more data-driven decision-making.

Configuration and usage

Setting up the module is a breeze! All you need to do is enter your Measurement ID and API secret values in the module's configuration settings, located at /admin/config/services/ga-server-side. With just these two simple steps, you'll be ready to track your e-commerce events seamlessly.

Image

Locating your Measurement ID and API secret values in Google Analytics is straightforward. First, log into your Google Analytics account. Your Measurement ID can be found in the Admin section, under the Property settings -> Data collection and modification -> Data Streams settings of your property. It typically begins with "G-".

Image

On the same Data Streams settings page where you found your Measurement ID, you'll see an option for Measurement Protocol API secrets. Click on it, then click Create. This will generate your unique API Secret, which is crucial for the secure transmission of your e-commerce data.

To track user sessions, it is also necessary to install the Google Analytics, GoogleTag Manager or Google Tag module and configure it. Another option is to use my module to insert the GTM container ID, which eliminates the need for another module to insert Google Analytics into your website.

Image

A humorous side note to this tale of innovation: I didn't create this module out of a deep love for analytics, but rather, a love of a different kind. My wife, who operates an e-commerce webshop, "gently nudged" me into developing this solution, proving once again that behind every great module, there's an even greater woman with a to-do list!

Do you need help configuring Google Analytics Server Side for Drupal?

Maximize your website's potential with my expert help in configuring the Google Analytics Server Side for Drupal. Contact me today to enhance your data tracking and improve your marketing strategies!

About the Author

Goran Nikolovski is a web and AI developer with over 10 years of expertise in PHP, Drupal, Python, JavaScript, React, and React Native. He founded this website and enjoys sharing his knowledge.