CMS Development

prasadcolumbus
Participant | Diamond Partner
Participant | Diamond Partner

How to Detect mobile device on hubl coding?

SOLVE

{{request.headers['user-agent']}} this variable showing so much information.Can any one help me on this. 

0 Upvotes
2 Accepted solutions
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

How to Detect mobile device on hubl coding?

SOLVE

Hey @prasadcolumbus 

 

Thats expression really shouldn't return a value that's very large at all, ~120-150 characters with spaces.

 

Take a look at this resource regarding the possible values it can return.

 

I wonder if you could do somehting like:

 

 

{% set agent = request.headers['user-agent']|lower|split(" ") %}

{% set isMobile = agent.index("mobile") > -1  ? true : false %}

 

 

 

Obviously this will need more research as I'm not familiar enought with the user agents to know if it alwyas contains " mobile".

 

Thoughts?

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

View solution in original post

georgemarket8
Solution
Contributor | Platinum Partner
Contributor | Platinum Partner

How to Detect mobile device on hubl coding?

SOLVE

Found it.
The agent value is different from a mobile to another. So splitting it doesn't work. Therefore I went for this solution below:
{% set agent = request.headers['user-agent']|lower|string %}
{% set is_mobile = false %}
{% if agent is string_containing "mobile" %}
{% set is_mobile = true %}
{% endif %}
Thank you.

View solution in original post

9 Replies 9
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

How to Detect mobile device on hubl coding?

SOLVE

Hey @prasadcolumbus 

 

Thats expression really shouldn't return a value that's very large at all, ~120-150 characters with spaces.

 

Take a look at this resource regarding the possible values it can return.

 

I wonder if you could do somehting like:

 

 

{% set agent = request.headers['user-agent']|lower|split(" ") %}

{% set isMobile = agent.index("mobile") > -1  ? true : false %}

 

 

 

Obviously this will need more research as I'm not familiar enought with the user agents to know if it alwyas contains " mobile".

 

Thoughts?

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
georgemarket8
Contributor | Platinum Partner
Contributor | Platinum Partner

How to Detect mobile device on hubl coding?

SOLVE

Hello Kevin,
I am getting isMobile = true for Android but not on iOS phones. What do you think the issue is?
Thank you in advance,
George

0 Upvotes
georgemarket8
Solution
Contributor | Platinum Partner
Contributor | Platinum Partner

How to Detect mobile device on hubl coding?

SOLVE

Found it.
The agent value is different from a mobile to another. So splitting it doesn't work. Therefore I went for this solution below:
{% set agent = request.headers['user-agent']|lower|string %}
{% set is_mobile = false %}
{% if agent is string_containing "mobile" %}
{% set is_mobile = true %}
{% endif %}
Thank you.

linealves
Participant

How to Detect mobile device on hubl coding?

SOLVE

Hey! where exactly I place this code? Should I add this to my forms? Sorry not a developer lol 

0 Upvotes
georgemarket8
Contributor | Platinum Partner
Contributor | Platinum Partner

How to Detect mobile device on hubl coding?

SOLVE

Heyy!
You can play with the below code in a custom module that you create:
{% set agent = request.headers['user-agent']|lower|string %}
{% set is_mobile = false %}

YOUR DESKTOP CONTENT GOES HERE......
{% if agent is string_containing "mobile" %}
{% set is_mobile = true %}

YOUR MOBILE CONTENT GOES HERE......
{% endif %}

dennisedson
HubSpot Product Team
HubSpot Product Team

How to Detect mobile device on hubl coding?

SOLVE

@georgemarket8 ,

thanks for adding your solution to the post 😀

0 Upvotes
georgemarket8
Contributor | Platinum Partner
Contributor | Platinum Partner

How to Detect mobile device on hubl coding?

SOLVE

😁

prasadcolumbus
Participant | Diamond Partner
Participant | Diamond Partner

How to Detect mobile device on hubl coding?

SOLVE

Thank you so much  @Kevin-C  it's working

0 Upvotes
stefen
Key Advisor | Partner
Key Advisor | Partner

How to Detect mobile device on hubl coding?

SOLVE

@prasadcolumbus I recommend either using Smart Content if you need to figure this out server side. Otherwise, you can do this client side fairly easily with either CSS media queries to show/hide specific things on mobile or desktop or use JS Match Media to change interaction of things on mobile/desktop.

Stefen Phelps, Community Champion, Kelp Web Developer