Wednesday, February 19, 2014

Mobile iRule - Honor user preference to view desktop site

Use case:
If the user is accessing from mobile, redirect to mobile website.
When the user choose the option to view desktop site from mobile webpage, redirect to desktop site. Keep the user in desktop site for 30 mins and put them back on mobile website.

Solution:
The solution uses cookie expiry logic.
When the user clicks "view desktop site" from mobile webpage, a query parameter (Ex: "allowMobile=y") is passed for tracking.

1) Please refer to the post mobile detection iRule on this blog. This rule is extension based on previous blog item.

2) Update or create an iRule named "mobile-rule-v2"

when HTTP_REQUEST {
 set useragent [string tolower [HTTP::header User-Agent]]
 set mobilehost "m.mydomain.com"
 set uri [HTTP::uri]
 set query [string tolower [HTTP::query]]
 set ismobile false

 if { [matchclass $useragent contains mobile-user-agents ] } {
  set ismobile true
 }
 elseif { $useragent contains "android" } {
  if { $useragent contains "mobile" } {
   set ismobile true
  }
 }

 if { $ismobile } {
   set hascookie false
   if { $query contains "allowMobile=y" } {
     if { not ( [HTTP::cookie exists "my-mobile-cookie"] ) } {
HTTP::cookie insert name "my-mobile-cookie" value "none" path "/"
HTTP::cookie expires "my-mobile-cookie" 1800
set hascookie true
     }
   }

   if { not ( $hascookie ) } {
    # Do the redirect logic here
   }
 }
}

# This response block is important
when HTTP_RESPONSE {
    if { [info exists "my-mobile-cookie"] } {
unset "my-mobile-cookie"
HTTP::cookie insert name "my-mobile-cookie" value "none" path "/"
HTTP::cookie expires "my-mobile-cookie" 1800
    }
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

Newer›  ‹Older