Skip to content

Analytics Magic

A resource for Google Analytics 4

  • Home
  • Setting up server side GA4 in apache

Setting up server side GA4 in apache

Setting up Google Analytics 4 (GA4) for server-side tracking in Apache involves a different approach compared to client-side tracking. Here’s a general outline of the steps to configure GA4 for server-side tracking in Apache:

  1. Sign in to your Google Analytics account and create a new GA4 property for your website (see the creating property walkthrough). Retrieve the Measurement ID associated with the newly created property.
  2. On your Apache server, locate the Apache configuration file (httpd.conf) or the virtual host file for your website.
  3. Enable the Apache mod_log_config module if it is not already enabled. You can typically enable it by uncommenting or adding the following line in your Apache configuration file:
    LoadModule log_config_module modules/mod_log_config.so
  4. In the same Apache configuration file or virtual host file, add the following CustomLog directive:
    LogFormat “\”%{Referer}i\” \”%{User-Agent}i\” %{cookie}i” ga4log CustomLog /path/to/access.log ga4log
  5. Replace /path/to/access.log with the path where you want the server logs to be stored. This directive configures Apache to log the necessary information for GA4 server-side tracking.
  6. In your server-side code, before generating the response to the client, you’ll need to make an HTTP request to the GA4 Measurement Protocol endpoint to send the tracking data. You can use a suitable HTTP client library in your chosen programming language to make the request. Construct the request URL with the appropriate data, including the Measurement ID and the parameters you want to track. Here’s an example of sending a basic pageview hit using cURL in PHP:
<?php
$measurementId = 'YOUR_MEASUREMENT_ID';
$url = "https://www.google-analytics.com/mp/collect?measurement_id={$measurementId}&api_secret=YOUR_API_SECRET";

// Set up the tracking data
$data = array(
    'v' => '1', // API version
    't' => 'pageview', // Hit type - pageview
    'tid' => $measurementId, // Measurement ID
    'dp' => '/path/to/page', // Page path
    'ua' => $_SERVER['HTTP_USER_AGENT'], // User-Agent header
    'dr' => $_SERVER['HTTP_REFERER'], // Referrer header
    // Include any additional parameters as needed
);

// Send the request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);

// Process the response as needed
?>

Note that you’ll need to replace 'YOUR_MEASUREMENT_ID' and 'YOUR_API_SECRET' with your actual GA4 Measurement ID and API secret. Adjust the tracking parameters as necessary for your specific use case. Customize the server-side code based on your tracking requirements, such as tracking events, ecommerce transactions, or other user interactions.

Copyright © 2026 Analytics Magic.

Theme: Oceanly News Dark by ScriptsTown