Rather than using Javascript (which does not really allow for a 301 redirects, whith the possible exception of serverside javascript like node.js apps) you should use Googles measurement protocol, which allows you to create serverside implementations of GA indepentently from the programming language (as long as the analytics property you track to is set up for UNiversal Analytics).
Of course you do not need GA, you could also catch the request from the browser and store it to a database (but you tagged your question Google Analytics so I assume you want to use GA).