Not-Device Detection javascript, perl and php code

26 01 2009

While server based detection using mod_rewrite or similar will provide a much better level of performance, sometimes you just want to handle it from within a script. Below are examples in javascript, perl and php so you can choose your language/environment. I would strongly recommend using a server side script (e.g. perl or php) but I’ve included a javascript version for reference. Of course if you’re cutting edge then you could run the javascript on the server-side too.

I hope you find this code useful. If you find any bugs or logical errors please let me know.

NOTE: This code is designed to support 3 key classes of device – PC, iPhone and POM (Plain Old Mobile). See the comments by the winmo detection that shows where you may like to extend this for other high-end devices (e.g. Windows Mobile or Symbian).

Javascript Example:

/**
 *  Copyright © 2009
 *  Rob Manson, Sean McCarthy and http://MOBusiness.com.au
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 *  Javascript Not-Device Detection Function
 *  Find out what type of device this is
 *  returns string - either pc, pom or iphone
 */
function _not_device_detection() {
    var ua = navigator.userAgent;
    var qs = window.location.search.substring(1);
    var agent = "error";
    var re = {
        "pcswitch" : new RegExp("pc", "i"),
        "pomswitch" : new RegExp("pom", "i"),
        "iphoneswitch" : new RegExp("iphone", "i"),
        "iphone" : new RegExp("iP(hone|od)(;|\s)", "i"),
        "winmo" : new RegExp("Windows\s+CE", "i"),
        "linux" : new RegExp("Linux", "i"),
        "windows" : new RegExp("Windows", "i"),
        "mac" : new RegExp("OS\s+(X|9)", "i"),
        "solaris" : new RegExp("Solaris", "i"),
        "bsd" : new RegExp("BSD", "i")
    };
    if (qs.match(re.pcswitch)) {
        /* This assumes you have a single query string value of "pc" */
        agent = "pc";
    } else if (qs.match(re.pomswitch)) {
        /* This assumes you have a single query string value of "pom" */
        agent = "pom";
    } else if (qs.match(re.iphoneswitch)) {
        /* This assumes you have a single query string value of "iphone" */
        agent = "iphone";
    } else if (ua.match(re.iphone)) {
        /* This user agent should be an iPhone/iPod */
        agent = "iphone";
    } else if (ua.match(re.winmo)) {
        /* This user agent should be a Windows Mobile device - you may want a special class for this and possibly high-end Symbian too */
        agent = "pom";
    } else if (
        (!ua.match(re.linux)) &&
        (!ua.match(re.windows)) &&
        (!ua.match(re.mac)) &&
        (!ua.match(re.solaris)) &&
        (!ua.match(re.bsd))
    ) {
        /* This user agent is not Linux, Windows, a Mac, Solaris or BSD */
        agent = "pom";
    } else {
        /* Otherwise assume it's a PC */
        agent = "pc";
    }
    return agent;
}

Perl Example:

######################################################################################################
##  Copyright © 2009
##  Rob Manson, Sean McCarthy and http://MOBusiness.com.au
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see .

##
## Perl Not-Device Detection method 
## Find out what type of device this is
## returns string - either pc, pom or iphone
######################################################################################################
sub _not_device_detection() {
    # either pass in \%ENV or pack the UA and QUERY into a hashref and pass that in
    my $env = shift;
    my $ua = $env->{HTTP_USER_AGENT};
    my $qs = $env->{QUERY_STRING};
    my $agent = "error";
    if ($qs =~ /^pc$/i) {
        # This assumes you have a single query string value of "pc"
        $agent = "pc";
    } elsif ($qs =~ /^pom$/i) {
        # This assumes you have a single query string value of "pom"
        $agent = "pom";
    } elsif ($qs =~ /^iphone$/i) {
        # This assumes you have a single query string value of "iphone"
        $agent = "iphone";
    } elsif ($ua =~ /iP(hone|od)(;|\s)/i) {
        # This user agent should be an iPhone/iPod 
        $agent = "iphone";
    } elsif ($ua =~ /Windows\s+CE/i) {
        # This user agent should be a Windows Mobile device - you may want a special class for this and possibly high-end Symbian too
        $agent = "pom";
    } elsif (
        (!$ua =~ /Linux/i) &&
        (!$ua =~ /Win/i) &&
        (!$ua =~ /OS\s+(X|9)/i) &&
        (!$ua =~ /Solaris/i) &&
        (!$ua =~ /BSD/i)
    ) {
        # This user agent is not Linux, Windows, a Mac, Solaris or BSD 
        $agent = "pom";
    } else {
        # Otherwise assume it's a PC
        $agent = "pc";
    }
    return $agent;
}

PHP Example:

/**
 *  Copyright © 2009
 *  Rob Manson, Sean McCarthy and http://MOBusiness.com.au
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 * 
 * PHP Not-Device Detection Function
 * Find out what type of device this is
 * returns string - either pc, pom or iphone
 */
function _not_device_detection() {
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $qs = $_SERVER['QUERY_STRING'];
    $agent = "error";
    if (preg_match('/^pc$/i', $qs)) {
        /* This assumes you have a single query string value of "pc" */
        $agent = "pc";
    } else if (preg_match('/^pom$/i', $qs)) {
        /* This assumes you have a single query string value of "pom" */
        $agent = "pom";
    } else if (preg_match('/^iphone$/i', $qs)) {
        /* This assumes you have a single query string value of "iphone" */
        $agent = "iphone";
    } else if (preg_match('/.*iP(hone|od)(;|\s).*$/i', $ua)) {
        /* This user agent should be an iPhone/iPod */
        $agent = "iphone";
    } else if (preg_match('/Windows\s+CE/i', $ua)) {
        /* This user agent should be a Windows Mobile device - you may want a special class for this and possibly high-end Symbian too */
        $agent = "pom";
    } else if (
        (!preg_match('/Linux/i', $ua)) and
        (!preg_match('/Win/i', $ua)) and
        (!preg_match('/OS\s+(X|9)/i', $ua)) and
        (!preg_match('/Solaris/i', $ua)) and
        (!preg_match('/BSD/i', $ua))
    ) {
        /* This user agent is not Linux, Windows, a Mac, Solaris or BSD */
        $agent = "pom";
    } else {
        /* Otherwise assume it's a PC */
        $agent = "pc";
    }
    return $agent;
}





Not-Device Detection Example Code

25 01 2009

Well I guess it wasn’t “just” after xmas – been a bit busy enjoying the Australian summer!

We’ve had a lot of interest in the “Not-Device Detection” solution and lots of requests for example code so here it is.

Below is an example snippet from an httpd.conf (e.g. configuration for Apache) using mod_rewrite.  This assumes you have 3 main version of your homepage.

  1. An iPhone/iPod Touch version using /iphone/index.html
  2. A Plain Old Mobile (POM) version using /pom/index.html
  3. A standard PC version using /index.html

Of course you can apply this model to more sophisticated solutions that also single out other specific devices or more complex collections of sub-pages. Other examples of this could also be implemented using perl or php scripts, but the performance is much better at the web server configuration level.

So, for this example let’s step through the code.

First make sure you have the RewriteEngine switched “on”.
RewriteEngine   on
Then detect the iPhone/iPod Touch first since they’re so easy to identify.
# iPhone/iPod redirected to iPhone index
RewriteCond %{HTTP_USER_AGENT}  ^.*iP(hone|od)(;|\s).*$
RewriteRule     ^/$         /iphone/index.html [PT]

The line below allows a user to choose to view the PC version by adding ?pc to the URL (e.g. from a specific switcher icon)
RewriteCond %{QUERY_STRING}     !^pc$ [NC]
You may want to exclude a number of standard bots here using generic strings.
You may also want to add exclusions for specific monitoring tools etc.  here too
RewriteCond %{HTTP_USER_AGENT} !(spider|crawl|slurp|bot) [NC]
Then here’s the meat of the “Not-Device Detection” solution.  You’ll notice that it mostly consists of !^ statements that say if this browser is NOT Linux, NOT Windows (excluding Win CE), NOT OS X or OS 9, NOT Solaris and NOT BSD then we’ll assume it’s a Mobile Phone and redirect it to /pom/index.html.
# Plain Old Mobile (POM) redirection (by exclusion)
RewriteCond %{HTTP_USER_AGENT}  !^.*Linux.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*Win.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  ^.*Windows\s+CE.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*OS\s+(X|9).*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*Solaris.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*BSD.*$
RewriteRule     ^/$         /pom/index.html [PT]

Otherwise it should just receive /index.html as normal.

While this may look a little complex if you’re not comfortable reading regular expressions…I think you’ll agree it’s extremely simple when you compare it to this spaghetti diagram over on the .mobi forum.

I’d like to thank James for creating this work of art…now whenever I want to explain to a client why having a .mobi domain is a wrong headed and silly idea that makes your web strategy more complicated I just point them to this single diagram.

Anyway, I hope you find the example configuration described above useful. If you have any questions or would like any help implementing this please post a comment here.





Not-Device Detection

16 10 2008

Over the last 2 years we’ve been refining a simpler and more powerful approach to device detection than everyone else seems to be focusing on.

While WURFL, Device Atlas, etc. are all great resources for very specific capability profiling…we’ve found that turning the problem upside down made much more sense and delivered faster, better results.

The key thing that most developers seem to be trying to do is to identify if a user IS using a specific Mobile Device.

The problem with this is that the User Agent strings on Mobile Devices are inconsistent at best…and completely useless or even non-existent at worst.

Once you accept this the inverse answer becomes clear.

PC browsers are much more reliable and consistent in identifying the Operating System they belong to. Also, there is only a small handful off Operating Systems you need to detect. So this is how our solution works.

We call this !device-detection (“Not Device” Detection).

If your User Agent isn’t one of the following Operating Systems:

- Windows
- Macintosh/Mac OSX
- Linux
- FreeBSD
- Solaris
- (Add niche OSes here)

And your User Agent doesn’t contain one of the following strings:

- bot
- slurp
- spider
- crawl

Then you are almost guaranteed to be on a Mobile Device.

Within this group you can then also clearly identify the important groups that are consistent:

- iPhone/iPod Touch
- Windows Mobile

We have implemented this method for two major Telco’s a major Insurance company, our leading edge Mobile Payment service and our own web projects. This is a strategy that has proven to be very effective and only requires a simple set of regular expressions in the form of Apache mod_rewrite directives that can work in either Apache or IIS.

We also couple this with a simple design pattern that provides a link from the Mobile site back to the PC site just in case we accidentally mis-classify a user. After all they should be able to ask for the full PC version if they really want it.

We’ve also recently put together a case-study that looks at some well known large corporations in Australia to see how they are or are NOT handling automatic device detection. The results are very surprising.

A full copy of the presentation is online on slideshare

Now we thought it’s time we shared this strategy in order to broaden the on-going device detection discussion.





What does the MoMo Sydney crowd think?

14 04 2008

While the overall participation rate was excellent, it declined as the poll progressed, as usual.

What would you like to see more of at MoMo Sydney?

When asked “What would you like to see more of?” 27 people answered.  Product launches, Panels and International Speakers were the most popular options.  Vox pop was the least popular with only 1%.

What topic do you want MoMo to cover most this year?

When asked “What topic do you want MoMo to cover most this year?” 20 people answered.  Mobile User Experience was the clear leader with Mobile Social Software, LBS/GPS and Mobile Payments following.  Mobile Games and Bluetooth were the least popular with 4% each.

Who\'s here?

When asked “Who’s here?” 16 people responded.  Of this small group Technology Vendor, Marketing Services Executive and Publisher/Media Owner were the most popular.  This was followed by Brand Advertiser and then Mobile Application Developer, Mobile Services Agency, Mobile Content Producer and Other.  Interestingly Carrier and Government were completely absent.

The iPhone - Big Thing or Big Hype?

When asked if they thought the iPhone was “The Next Big Thing or the Next Big Hype?” 17 people responded.  Of this group 65% selected “Big Thing”.





MoMo Sydney Audience Profile

14 04 2008

MoMo Sydney Audience\'s Technology Profile

The overall audience size when the poll was run was about 75 people. 71 people tried to participate with 59 of them successfully joining in. The 12 that couldn’t join in all sent an SMS but either decided not to follow the link in the reply message or simply couldn’t because their mobile doesn’t support data or open internet access.

It was interesting to see that over 58% of users chose to join the Live Poll using SMS. This means they sent an SMS to our inbound number and we then sent them back an SMS with a link to join the Poll. A third of the users simply typed the TinyURL directly into their phone and 8% scanned the QR Code provided. All three of these Access Methods were listed on the postcard that was handed out to the audience.

Again we were quite surprised at how evenly the 3 major MNO’s in Australia were represented. This is similar to the results we saw at Web Directions last year and we believe this suggests that amongst Data Heavy Mobile Consumers the market share is much more evenly split. However these results are only meant to be indicative and are not statistically significant. I’ll make some analysis of the “Other” segment soon.

In terms of Device Manufacturers Nokia and SonyEricsson dominated as usual. However there were 7 iPhone’s (11%) which just highlights what a bunch of Early Adopters the MoMo crowd really is. We think this suggests that this audience is more representative of how the mainstream market will be in 12 to 18 months.

When it comes to Mobile Browsers the audience was fairly evenly split between Safari (iPhone and Symbian), NetFront, Pocket IE and Unknown. We captured the x_wap_profile headers for all of these users so I’ll provide some more info on the “Unknown” break down soon. Again it was very interesting to see how small the Opera slice was. And the IE/Firefox slices were from the PC’s we used on the stage.





Live Mobile Poll at MoMo Sydney

14 04 2008

Live Mobile Poll at MoMo Sydney

Last Monday we ran another MOB Live Mobile Poll…this time it was at MoMo Sydney.  The audience could join in by SMS, URL or QR Code.  The results of the audience profile and their answers where presented on screen in realtime.





Device Manufacturer Stats

7 11 2007

Mobile Device Manufacturer Stats

It’s really interesting to see how diverse and varied the types of devices that participated on the day. Nokia clearly has the most significant market share, and if you combine “Sony”, “Sony Ericsson” and “Ericsson” (which you probably shouldn’t do) then they are a close second.

I think most significant is how small the Blackberry share is. In this audience all the combinations of Windows Mobile devices (dopod, i-mate, O2 and possibly HP or even Samsung) are 3-4 times the the number of Blackberry’s.