Google Analytics on a PHP page

I have a short url website like bitly which currently redirect users with php from my site to a site of their choice. I want to add google analytics to the redirect pages but i am stuck on the problem where javascript is run after the php code is executed so it wont work.

Bellow is the last part of the php code that redirects users from the short url to the url they shortend. i wonder if there is anyway to make this work OR change my code somehow so that google analytics can be executed either with php or something else that can do the “redirection” ?

$shortUrlObj contains the short url code. If the short url is https://example.com/1234 $shortUrlObj contains 1234. fullUrl is the website destination they choose to shorten. So if the user shortened https://stackexchange.com fullUrl contains https://stackexchange.com

switch ($redirect)
{
    default:
        header('HTTP/1.1 302 Moved Temporarily');
        redirect($shortUrlObj->fullUrl);
        break;
}