By its very nature the detection for a client device has to be client-side code, as you allude to.
AMPScript is server-side code, so you wont have any luck with that in an email.
You can do this with server-sided code only once people click on a link to a cloudpage with AMPScript or SSJS, here is the AMPScript function. Just put on a CloudPage and preview to see your own device:
If it's only about controlling different layouts in an email, then CSS Media Queries are the way to go.
Media Queries in CSS are client-side code inside an email used to detect the device, but they control only styling, so you could display different content per device. This of course does not log anything or help with other data-driven decisions.