Plain Dev Blog
← Back to all posts

Server Side Analytics: Google

·

Server Side Analytics: Google

Before a simple pixel on the front-end would’ve did just fine, but those days are behind, now you need a bit more in combination with the pixels to get a better understanding of the bigger picture is.

Server side analytics, most of the services that provides ads and analytics provide this type of API where you can sent additional information for users’ behavior, depending on what events you need and what events are configured in your analytics dashboard.

There is a package that can be very helpful of implementing this if you don’t need anything custom or more specific.

What you need to sent events to the API:

  • client_id — to authenticated

  • user_id — hashed user email with md5

  • event_data — the array with the data you wish to sent

  • GA4_MEASUREMENT_ID — from Dashboard analytics

  • GA4_MEASUREMENT_PROTOCOL_API_SECRET — also from the dashboard

If you wish to use the package you can installed like so:

composer require luketowers/laravel-ga4-event-tracking

gather the required keys from Google Analytics Dashboard and set them in your project environment file, when those are set the event push is pretty simple:

use LukeTowers\GA4EventTracking\Exceptions\MissingClientIdException; 
use LukeTowers\GA4EventTracking\Exceptions\ReservedEventNameException;  
use LukeTowers\GA4EventTracking\GA4;
  
// ...  
  
$event_data = [  
    'name' => $event_name,
    'params' => array_merge([  
    'email' => $user->email,  
    'value' => $amount_paid,  
        'gclid' => $user->google_ads_click_id,  
    ], $extra_data),  
];  
  
$google_analytics = new GA4;  
$google_analytics_client_id = $user->google_analytics_client_id;  
  
$response = $google_analytics  
    ->setClientId($google_analytics_client_id)  
    ->setUserId(md5($user->email))
    ->sendEvent($event_data);

You need to collect the response and add the appropriate checks if there is any errors returned, but pretty much that’s it. After the events start to received them in your dashboard.

If you don’t want to use the package, the implementation can be done using Laravel’s HTTP client to sent the events for Google there are no specifics for the authentication so there is nothing to worry about.

One thing the should mentioned here is that it’s important to have the gclid sent, it the connection between the ads you run and the data that’s received, basically the gclid it’s set when the user clicks on your ad and clicks on it. after the implementation is done take a good look at is sent, so you can optimize the data if required.

Conclusion

The combination between front-end pixel and server side events sent is your best option to get the best possible tracking data from Google, you can always combined it with other platforms for more coverage.