Expand my Community achievements bar.

SOLVED

Get user device info

Avatar

Level 2

Hello, guys, how are you?

I am wondering, is there a way I can get informations about the device the user is using to navigate the site, perhaps from the request object?
Namely, I'd be interested in informations such as the device group to which the user device belongs, and - ideally - informations about the screen size.

I did try adapting the request object to the Device.class, as well as retrieving (successfully so) an instance of the DeviceMapper and then calling the method deviceMapper.getDeviceInstance(request), but I have had zero luck obtaining a non-null instance of the Device class.

In general, I am dubious whether it is even possible to infer informations about the user's device e device's size while working server-side.

 

I would be very grateful if any of you had any considerations and were willing to illuminate me.

Thank you so much,

Parag.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @parag_dalal,

I am not sure why you are getting the device type; if this the only requirement then you can easily get it from User-Agent request header from the request in either sling model or wcmusepojo.like: request.getHeader("User-Agent");

But if your requirement is to use the device size resolution then I would go with viewport from the front-end rather than back-end java. because it will have some impact on your site performance ultimately.

Hope this will help.

Umesh Thakur

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi @parag_dalal,

I am not sure why you are getting the device type; if this the only requirement then you can easily get it from User-Agent request header from the request in either sling model or wcmusepojo.like: request.getHeader("User-Agent");

But if your requirement is to use the device size resolution then I would go with viewport from the front-end rather than back-end java. because it will have some impact on your site performance ultimately.

Hope this will help.

Umesh Thakur

Avatar

Level 2

This is the correct answer. Ideally, I would still be able to know the device size resolution on the back-end, without involving the front-end logic: but that's mainly because of our client's peculiar requirements.

Thank you, Umesh, for the prompt reply.

Avatar

Community Advisor

@parag_dalal 

If you are doing it in server side, consider that the page will be cached and will always give the device info of the first requested device.

Avatar

Level 2
Thanks, that's a useful observation: thanks for pointing it out.