Unlocking the full potential of your advertising budget with GA4 and server-side tracking

Introduction


In the competitive digital advertising landscape, every bit of data is crucial for making informed decisions. The advent of Google Analytics 4 (GA4) has brought about a paradigm shift in how businesses can track and analyze their website traffic, especially e-commerce transactions. One of the salient features of GA4 is its enhanced tracking capabilities, which, when combined with server-side tracking, can significantly improve the accuracy and comprehensiveness of data collected.

 

The importance of accurate transaction tracking


Accurate transaction tracking is the linchpin of any successful e-commerce advertising campaign. Every order placed on your website is a valuable piece of data that can be used as a signal for Google Ads to bid on. However, a common discrepancy often observed is the lack of server-side tracking which can lead to a data loss of around 14-18%. This means you could be sending an additional 14-18% of transaction data to Google Ads, providing more ammunition for better campaign optimization.

 

Bridging the gap with server-side tracking


Server-side tracking involves sending data directly from your server to Google Analytics, which includes client_id and session_id. This method is more reliable and accurate compared to traditional client-side tracking, as it’s less prone to issues like ad blockers or cookie restrictions. By implementing server-side tracking in GA4, you ensure that every transaction is tracked accurately, thereby maximizing the potential of your advertising budget.

 

GA4: A game changer in campaign optimization


GA4’s advanced features like predictive metrics, audience building, and cross-platform tracking provide a robust foundation for analyzing user behavior and optimizing ad campaigns. When coupled with server-side tracking, the synergy created can significantly enhance your campaign’s performance, ensuring that every dollar spent is effectively utilized towards reaching your advertising goals.

 

Example payload to GA4

Here’s an example payload you can send from your server, straight into GA4 using the GA4 measurement protocol.

So to start off, the endpoint:

https://www.google-analytics.com/mp/collect?measurement_id=[YOUR_MEASUREMENT_ID]&api_secret=[YOUR_API_SECRET]

You can find the Measurement ID in your GA4 admin -> “data streams”. Click your stream ( or create one if you don’t already have one ), and you’ll see your Measurement ID in the top right corner.

The API Secret needs to be created. So in the same view where you see the measurement ID, click “Measurement Protocol API secrets”, then “Create”, to create a new API Secret.

Now, before you can continue, you will need 2 things set up:

  1. Access to the order before it’s finalized.
  2. Fetch the session_id and client_id from the current GA4 session.

    Normally, when a customer visits the checkout of your store, an “unfinalized” order is created in the background, and for GA4 to attribute your conversions properly to the correct session, you will have to attach some extra meta data on the order: the “session_id”, and the “client_id”. There are different methods to gain access to the correct ID’s, but our normal approach is to use a Google Tag Manager community library tag, called GTAG GET API. You can find this by searching for it in Google Tag Manager Community Template Gallery.

    Select a trigger to trigger the GTAG GET API, which will push the session_id, and client_id into the dataLayer.

    On the checkout page, take the collected session_id and client_id, and add it to the meta-data of the order.

Now, when the order is created, a webhook is set up to send the order data to your server, you can proceed with formatting the event data from the information you receive from the webhook.

Here’s an example of a working purchase event:

{ 'client_id': client_id, "user_id": body["personId"], #optional, send if available. "timestamp_micros": timestamp_micros, "non_personalized_ads": False, 'events': [ { 'name': 'purchase', 'params': { 'currency': orderCurrency, 'value': orderTotalValue, 'discount': orderDiscount, #should be an absolute number, not a percentage. 'transaction_id': orderId, 'shipping': shipping, #the order shipping cost. 'items': items, #a list with items in the order "debug_mode": debug, #True or False, depending on if you're testing or have it deployed. "session_id": session_id, #the session ID should be under the 'params' key. } } ] }

You will also need to send some headers:

{ "User-Agent": user_agent, "Content-Type": "application/json", }

The “User-Agent” should also be attached on the order in the checkout stage and is simple to attain with:

const userAgent = window.navigator.userAgent;

Now, when you have all information ready, send the event to GA4. Here’s an example using the python requests package:

url = "https://www.google-analytics.com/mp/collect"
params = {
    "measurement_id": measurement_id,
    "api_secret": api_secret,
}
response = requests.post(url, data=json.dumps(event), headers=headers, params=params)

Conclusion


Investing time and resources in setting up server-side tracking with GA4 is a prudent decision for any business looking to gain a competitive edge in the digital advertising arena. The enhanced data accuracy and the subsequent impact on Google Ads campaign optimization is a lucrative proposition that promises better ROI and a stronger online presence. By embracing these advanced tracking methodologies, businesses are well-positioned to thrive in the ever-evolving digital marketing landscape.