Drupal, Google Analytics 4 and commerce events

In the ever-changing digital landscape, the impending retirement of Google Analytics 3 (Universal Analytics) in favor of Google Analytics 4 (GA4) is a significant shift that website owners and developers must grapple with. For those utilizing Drupal 9/10 and Drupal Commerce, this transition is particularly challenging.

The current Drupal module for Google Commerce Analytics (Commerce Google Tag Manager), even if updated 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 an Event Subscriber. 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 addToCart 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.


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.


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. With just these two simple steps, you'll be ready to track your e-commerce events seamlessly.


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 Data Streams settings of your property. It typically begins with "G-".


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 or Google Tag module and configure it.

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!

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.