br33f / php-GA4-Measurement-Protocol

PHP GoogleAnalytics4 Measurement Protocol Library
88 stars 19 forks source link

User Agent Override #29

Closed reformed closed 1 year ago

reformed commented 1 year ago

Google's documentation allows for a user agent string to be sent, using the ua parameter, but this library does not appear to offer this functionality.

This is needed for an application that sends GA4 data directly via server-side. I see that you said "if you provide client_id in MP request it automatically binds sent event to client data already existing in GA4" but this does not account for new users who do not already exist.

br33f\php-ga4-mp\src\Service.php sets the uip parameter, please also allow the ua parameter to be set as well.

reformed commented 1 year ago

In the mean time this can be resolved by extending the Service class:

class GA4Service extends \Br33f\Ga4\MeasurementProtocol\Service
{
    public string $userAgentOverride = '';

    public function getQueryParameters(): array
    {
        $parameters = parent::getQueryParameters();

        if (!empty($this->userAgentOverride)) {
            $parameters['ua'] = $this->userAgentOverride;
        }

        return array_filter($parameters);
    }
}
chludwig commented 1 year ago

@reformed Have you tried if you get a result in analytics when sending a ua parameter by query? We've tested nearly every parameter that is sent when using a pure GA4 gtag.js code and many more. But we sent the info in the body as param and as user property without success until now.
Also see https://stackoverflow.com/questions/65249816/how-to-set-user-agent-in-measurement-protocol-google-analytics-4 which is still unanswered since years.

reformed commented 1 year ago

@chludwig Good catch, I didn't realize I was looking at UA documentation. So until Google fixes that there's nothing this library can do.