Blog

One tool every front-end developer should be using is a solid template or framework for new projects. "Frameworks" in general have gotten a bad reputation for being bloated with unnecessary code and classes just to save a few minutes. I don't use any layout or CSS frameworks precisely for those reasons. However, creating an HTML framework that is customized to your own coding style and tastes, while leveraging the latest technologies is definitely worth considering.

The goal of a framework is to set a foundation for new projects so you can hit the ground running. It should include elements and styles that you use for most projects, but the key is to not waste any lines of code. As a general rule, I only include code in my framework if I use it in 80-90% of my projects. If not, I cut it out to make sure I'm not wasting valuable kilobytes.

There are currently two open source HTML5 frameworks: HTML5 Boilerplate and HTML5 Reset. I think both are very solid and worth checking out when you create or update your framework. The problem is that I don't believe there is a "one size fits all" solution for this stuff. Every developer has their own style and preferences, so in the end you should build a framework that suits you. Hopefully this article will give you a good foundation of resources, code and advice to get going.

Can we use HTML5 yet?

Whether to use HTML5 or not should be assessed on a case-by-case basis. I'm using it in client projects unless over 2% of the visitors have javascript disabled, which means I am using it in almost all new projects. HTML5 is already very well supported by modern browsers and it is simple to bring old ones up to date with a small amount of javascript. Richard Clark and Remy Sharp put together a really good article summarizing the positives and negatives of HTML5 on HTML5doctor.com, called "How to use HTML5 in your client work right now". It's a very good read.

While CSS3 is a bit more complex with regards to browser support and new syntax, HTML5 is straightforward and easier to pick up. Adding HTML5 support to your project involves one line of CSS:

section, article, header, footer, nav, aside, hgroup { display:block; }

Of course, Internet Explorer isn't exactly what we would call "modern". It requires a 4kb javascript file called html5shiv, created by Remy Sharp. I have seen estimates that put javascript adoption above 99% at this point, so it's safe for most projects to depend on a small javascript file for proper rendering in IE.

If it's learning HTML5 you are hesitant about, fear not! Anyone that has a good understanding of HTML4 should be able to pickup the new HTML5 elements in 2-3 hours. Here are a few resources I used to get up to speed:

Code Cleanup

Whether you choose to use HTML5 elements in your framework/projects or not, much of the code in your framework can be still cleaned up, especially in the <head>. Take a look at how we can clean up the DOCTYPE declaration:

Before:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd">

After:

<!DOCTYPE html>

The only downside of updating your doctype is that validation is a bit more difficult. The W3C validator now includes "experimental support" for HTML5 ... it's just not full proof yet.

Another nice little change is that the "type=" attribute is no longer necessary. Here are a couple of examples:

Before

<script type="text/javascript" src="js/script.js"></script>
<link type="text/css" rel="stylesheet" media="screen, projection"
href="css/styles.css" />

After

<script src="js/script.js"></script>
<link rel="stylesheet" media="screen, projection" href="css/styles.css">

You can update the DOCTYPE and lose the type attribute now, as all common browsers support these changes (including Internet Explorer 6). View source on the Google home page and you will see that even they are following both of these rules.

Meta Tags

Must-haves

<meta charset="utf-8">
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" content="IE=8">

The first two lines are straightforward and don't require any explanation. The third line forces "standards mode" in Internet Explorer 8. I find it hilarious and somewhat sad that Microsoft let users choose whether they wanted to render pages properly or not in IE8, but at least we have a way to force standards support.

HTML5 Boilerplate stretches IE a little further and forces support for the very latest IE rendering engine with the following code:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

I'm with Aaron Gustafson and his article, "Beyond DOCTYPE: Web Standards, Forward Compatibility, and IE8". He says that using IE=edge is risky. There's no way for us to predict IE's next move and I would prefer it not breaking any of my sites. Still, this is something worth your consideration.

Optional

<meta name="robots" content="index, follow">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="Copyright" content="(c) 2010. All Rights Reserved.">
<meta name="viewport" content="width=device-width; initial-scale=1.0;
maximum-scale=1.0;">

Keywords: I want to touch on keywords and why they are now an optional meta tag. For search engine optimization purposes, keywords are practically useless because they are far too easy to manipulate. None of the major search engines consider them for this reason. The only reason to include them now is if you have a site search that uses meta keywords to assist with indexing.

Viewport: Apple introduced the viewport meta tag for mobile Safari on iOS devices. Setting the scale at 1.0 prevents the user from zooming or scaling the site, so it should only be used if your site is optimized for mobile devices. However, now that Mobile Firefox (aka Fennec) and Android WebKit support the tag as well, it could get tricky as devices differ in screen resolution. If you are optimizing for mobile, the best practice at this point is to use the tag and test it as thoroughly as possible across different browsers.

There are still many other optional meta tags, but none seem common enough to include in a framework. I found it interesting that HTML5Reset has DC (Dublin Core) meta tags in their framework even though major search engines are not recognizing it yet. I'm not willing to go there yet.

Javascript

Browser Selector

Browser Selector is a very useful 1 kb javascript file I use on some projects, but it's not yet included in any HTML5 frameworks. The script started as an idea by 37signals back in 2006. Within a couple of days there was a working version. Browser Selector adds classes to the <body> tag that define the user's operating system, browser, javascript support and in most cases, browser version. Using the script, you can target users on virtually any specific platform and create styles uniquely for them. I comment this script out by default, but it can come in handy every so often.

When there's only a need to optimize for Internet Explorer, Paul Irish has a nice solution for using conditional comments to isolate different versions without the use of javascript. However, in most cases I prefer browser selector because of how flexible and expansive it is.

Modernizer

Modernizer is a popular 5 kb javascript file that uses some of the same awesomeness that Browser Selector does. Instead of targeting specific platforms though, it detects the platform's level of support for CSS3/HTML5 elements. For instance, if the browser supports border-radius, a class of "borderradius" will be added to the body tag. If it does not, a class of "no-borderradius" is added. This way you can create specific styles for each case.

An added bonus of Modernizr is that it adds support for the new HTML5 elements, thus eliminating the need for html5shiv. For a great tutorial on using Modernizr, check out this A List Apart article.

DD_belatedPNG

There are several scripts and solutions available for adding alpha-transparent PNG support to IE6. I think DD_belatedPNG is by far the best, easiest to implement and most flexible. The compressed file is only 7 kb.

Not every project needs to support alpha-transparent PNGs in IE6, but this is a great solution to have on hand when it is required. When building your framework, this is the kind of script you can comment out by default and activate when necessary. Just be sure to read through the known issues before getting started.

Google Analytics

I know including this script is a bit obvious for analytics users, but I mention it so that your framework has the latest version of the Google asynchronous tracking code. Google says the optimal position for the code is on the bottom of the <head>, after any other scripts have been loaded, so that's where I like to keep it.

Stylesheets

One thing to keep in mind that some HTML/CSS frameworks have forgotten is that each stylesheet is another HTTP connection the browser has to make when loading the page. Don't load five or six stylesheets on every page just because you are a neat freak, because it slows everything down.

One to three stylesheets is enough for most projects. I like to have one stylesheet with default framework styles that are re-used on each project, then one stylesheet with styles that are all specific to the project. Here are some good tips:

Browser Reset

The godfather Eric Meyer gave us Reset CSS in order to put all browsers on a level playing field. More recently Richard Clark gave us the HTML5 Reset Stylesheet. The only thing I don't like about Richard's version is that he goes a little further than doing a browser reset. Adding any colors, borders or text styles should not be included in a reset, but that's just my opinion. Study these two in order to define your own reset styles.

Clear Fix

Another snippet of code that is a great addition to most default stylesheets is the clearfix, which most of you are familiar with using. Simply add the class to any div containing floats to clear them properly. For a more detailed explanation, Position is Everything has a nice article on it. Here is the syntax I use:

.clear { display: inline-block; }   
.clear:after, .container:after { content: "."; display: block; height: 0;
clear: both; visibility: hidden; }
* html .clear { height: 1%; }
.clear { display: block; }

Default Styles

I found a few different resources to be helpful in creating default styles over the years. The first is Blueprint CSS. Some of the text and form styles in their screen.css file are great, but I don't use anything else because it's too much to include in a framework. The second helpful resource is HTML5 Boilerplate. Some of the styles included in the style.css file are really useful ... kudos to that team. I have tweaked the classes to my liking but, here are a few of my favorites:

.hide { display:none; visibility:hidden; } /* hide from browser & screenreader */
.browserHide { position:absolute !important; clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px); } /* hide from browser, not screenreader */
.inv { visibility: hidden; } /* hide from screenreader, not browser */

h1,h2,h3,h4,h5,h6 { text-rendering: optimizeLegibility; }
html { -webkit-font-smoothing: antialiased; }

.replace { display:block; text-indent:-9000px; overflow:hidden; }
/* for image replacement */

My final source of inspiration for default styles is personal experience. I regularly review my default styles, deleting code that is used in less than 80-90% of my projects. It keeps the styles from getting bloated, as page load time is always priority one in my opinion.

Print Styles

Like many people, my first print stylesheet was based off Eric Meyer's 2001 article called, "Going to Print". It serves as a nice introduction, but more recently I started implementing an open source print framework called Hartija. With some minor modifications, those styles have served my projects well for quite a while now.

One unfortunate thing about print stylesheets is that they are loaded whether the media is set as print or not. So in many cases, it's an extra HTTP connection that is not necessary to render the web page. In other words, it wastes precious page load time. I learned a solution on Stoyan Stefanov's blog (in the comments), which has now been implemented as part of HTML5 Boilerplate too. Simply add your print styles to the default stylesheet like so:

@media print { ... styles here ... }

By using the media declaration instead of a separate file, the styles don't require another HTTP connection and are still loaded when users print the page.

Other Elements

Favicon and Webclip

<link rel="shortcut icon" href="images/favicon.ico">
<link rel="apple-touch-icon" href="images/webclip.png">

Two must-have links as part of your framework are the favicon and the webclip icon. The favicon is a mainstay, so we won't cover that. Webclip icons are specifically for iOS devices. They appear when someone saves your website to their home screen. The dimensions are now 114x114 thanks to the new high-resolution iPhone4 retina display.

Print Logo

Another strategy I've blogged about before that has not yet made it to the public frameworks is printer-friendly logos. Having the company logo show up on printed pages is a nice wow-factor that most coders seem to ignore.

The company logo is usually a background image on standards-compliant websites, but background images don't show up on printed pages. The solution isn't innovative or sexy, but it works really well.

  1. At the top of your wrapper, add the printer-friendly logo as a static image, giving it a unique ID like so:

    <img src="images/print-logo.png" alt="Company Name" id="printLogo">
  2. In your main stylesheet, make sure to set the following styles:

    #print-logo { display:none; visibility:hidden; }
  3. Now in the print stylesheet or separate print styles, set the following and the logo will print beautifully:

    #printLogo { display:block; }

Images Folder

Images Folder

One final strategy that works really well for a framework is organizing your images folder properly. I like the idea of setting up two sub-folders, called "content" and "temp". Any temporary or placeholder images from the mockups should be stored in the temp folder so it can easily be emptied at a later date. Images that are part of the website content should be stored in a separate folder in case the website is ever re-designed. Then you know to keep images in the content directory and all others can be replaced with the new design.

My Framework

Framework

It's only fair to do all of this talking about frameworks if I make mine available for download, so you can download it here and use it however you like.

As a whole, my framework does less than what is out there. HTML5 Boilerplate and HTML5Reset both go a little further than I'm willing to, but that's totally fine. For example, they both include jQuery, whereas I'd prefer to include it on a case-by-case basis. HTML5 Boilerplate even includes an .htaccess file, which is very cool, but not for me because those things tend to vary for my projects.

Otherwise there's nothing special about my framework other than the fact that I've created it to suit my style and needs perfectly. That's exactly what I'm encouraging you to do.

HTML5 frameworks can only exist as a result of web developers sharing their ideas and code with others. Each framework is the result of many, many smart people sharing their ideas with the web community. It's up to you to take these great resources, make them your own and eventually contribute cool things to add back to the community as your framework improves.

Finally, it is inevitable that some great resources and ideas have been left out of this article, so I encourage you to contribute what you know in the comments or to an existing framework so that everyone can benefit.

Posted in Code - Join the Discussion

My first article as an author for Smashing Magazine published today and I'm extremely pleased with it. It's called "Web Development for the iPhone and iPad: Getting Started".

Vitally and the team at Smashing are first class in every way. I'm looking forward to writing more for them if they let me. If you don't already, subscribe to their feed. It's one of the very best for web designers and developers.

Posted in Code - Design - Join the Discussion (2 Comments)

If you create websites or web apps for the iPhone OS (iPhone, iPad, iPod Touch), it's important to setup a solid local development environment for testing. Using a desktop web browser (even Safari) or one of the many available "emulators" for testing is worthless because the real thing looks different.

The only way to test properly is with Apple's iPhone Simulator. Since you can't just type a local file path into the simulator, I'll show you step-by-step how to get it working (on a Mac).

What You Need

1- Install the SDK (with Xcode) and MAMP

The SDK is about 2.8GB so you should give it a while to download. Installation took about ten minutes for me.

What's MAMP?

MAMP is a great little application that gets you up and running with the local server environment on your Mac in about five minutes. All you need to do is install it like any other application.

2- Setup the location of your website or web app

Create a folder for your project. It can be anywhere on your computer. Place an index.html file in it with "hello world" or whatever you need to test that it's working when we open the simulator.

For this tutorial I'm going to call my folder "iphone-site" and put it in /Users/Nick/Sites/ on my computer.

Folder

3- Create an alias in MAMP

Go to the location where you installed MAMP (probably Applications), then open this file: /MAMP/conf/apache/httpd.conf. Around line 655 you should see the following code:

Alias /MAMP "/Applications/MAMP/bin/mamp"

<Directory "/Applications/MAMP/bin/mamp">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Add a line of space after this code, then create your alias with this code:

Alias /iphone-site/ "/Users/Nick/Sites/iphone-site/"
<Directory "/Users/Nick/Sites/iphone-site/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Replace the file path above with the file path to your site in both places.

Save the file and close it. Please note that if you ever make edits to the httpd.conf file while your MAMP server is running, you will have to restart the server to see those changes take effect. Also, if you ever change the location of your site, of course this file will have to be updated with the new file path.

What this does is keep the URL clean and easy to remember. Setting up an alias is not technically required, but I highly recommend it. Instead of the URL being this:

http://localhost:8888/Users/Nick/Sites/iphone-site/

The new, clean URL would look like this:

http://localhost:8888/iphone-site/

4- Test It

Launch MAMP and make sure that the server is running. The window should look like this:

MAMP Window

Now it's time to open the iPhone simulator. Open a finder window and navigate to the drive you installed the developer kit on (eg. "Macintosh HD"). The simulator is in the /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/ folder. It's a good idea to add it to your dock so you can access it easily in the future.

Once the simulator is running, open Safari. Navigate to http://localhost:8888/iphone-site/. Of course you will want to substitute iphone-site in the URL for the name of your project. You should see "hello world" (in very tiny text), which means you are good to go!

iPhone Simulator

Now you can develop iPhone websites and web apps without depending on an internet connection, which should speed up development time significantly. Whenever you start another iPhone project, simply add another alias to your httpd.conf file and get started!

Testing on the iPad

The iPhone OS SDK version 3.2 and up includes support for the iPad in the iPhone Simulator application. It's a little confusing, but think of the iPhone as more of an operating system rather than a device in this context. From the iPhone Simulator, click "Hardware" in the main menu, then "Device" and you can select between the iPhone and iPad.

Device Menu

What about debugging?

Mobile Safari has a debugging console, but I didn't find it too helpful for basic front-end development. There isn't a substitute for Firebug or Safari's developer tools, so I would suggest continuing to use those in a desktop browser if you get stuck. Using the same localhost URL will work in other browsers as well as the simulator.

Resource Library

Once you have your test environment going, it's important to familiarize yourself with the mobile Safari browser and how to make the most of it. Here is a set of articles to get you started:

Posted in Apple - Code - Join the Discussion (11 Comments)

CSS Sprites are a technique made popular back in 2004 by Dave Shea. In short, creating a sprite means combining a bunch of images into one, then using CSS background positioning to display images individually. Built properly, sprites cut down on page weight and minimize http connections required in order to load a web page. Bottom line: it makes things a lot faster.

Still, using sprites isn't all that mainstream because it takes longer to code a website with them, and making regular updates can be tedious without proper planning. For many smaller sites, the benefits simply aren't overwhelming enough to invest the additional time.

If you haven't been using sprites on projects for whatever reason, my hope is that this case study will tip the scales for you.

The website we worked on for the case study is ErgonomicsMadeEasy.com. We created this site a couple of years ago with only one sprite for the navigation. After improving the home page with sprites, image optimization and roughly five hours of time, here are the results we saw:

Ergo comparison

Highlights

  • Total page weight decreased by 270.7k or 42%
  • 55% fewer HTTP calls
  • Page loaded in 2 seconds, a 70% improvement
  • Total number of images decreased by 45 or 62%

Four total sprites come together on this website, three of which were new. The navigation was already a sprite, so it remained the same. Here is a link to see each:

Why four sprites and not one big one? Two reasons:

1. When optimizing images for minimum file size as a PNG-8, the image quality deteriorates with more color diversity.

2. Big huge sprites are typically very hard to maintain.

Non-sprite optimization

As part of this project I also did two other things not related to sprites that made a nice dent in the overall file size on the home page:

1. I highly recommend saving any JPG images with Adobe Fireworks CS4. Don't ask me why, but the process is completely different from how Photoshop saves JPGs for web. It's quite common to save 20% or more in file size by using Fireworks over Photoshop. However, I still use Photoshop for PNGs.

2. I noticed about ten background images that were being loaded on the home page through the stylesheet, which were only necessary on category pages. So I took that CSS code and put it in a separate stylesheet that is only loaded on categories. If you have large background images being referenced in your stylesheets that are only necessary on a couple of templates, I recommend putting them in a different stylesheet and only calling it when they are needed.

What Now?

These numbers should be proof enough that sprites play a crucial role in maintaining a fast website. Not only that, but running your own tests and optimizing is important before launching a site.

It takes time to understand the right uses for sprites, but the only way to learn is to work with them. Here is a collection of my favorite articles on creating sprites:

Lastly, I don't recommend any auto-magic sprite generators because they don't account for some important criteria like images that repeat or may not have a defined width/height. Create/code your sprites from scratch and I believe you will get much better results in most cases.

Posted in Code - Web - Join the Discussion (11 Comments)

Mar 16, 2010

Printer-friendly Logos

Part of your website development process should include coding and testing a print stylesheet. One thing that took me a long time to figure out is how to properly insert a logo at the top of the page. The technique is very easy.

Two things are tricky about printer-friendly logos:

  1. Logos are typically background images and print stylesheets don't print background images
  2. Often times the logo is over a colored background, which you don't want on white paper, so you need to specify a different image for printing

The code I use will work every time, no matter what the website looks like in a browser. Start with the following at the top of your layout in the main container:

<img src="/images/print-logo.png" alt="Website/Company Name" 
id="printLogo" />

Your main stylesheet should have the following:

#printLogo {
	display:none;
}

Your print stylesheet should have the following:

#printLogo {
	display:block;
}

Easy, right? For the longest time I was looking for a way to do this without adding an image to the top of the layout, but I've found this to be the most consistent, reliable method.

A nice looking print stylesheet is a huge wow-factor; especially when you have taken the time to add the logo to the top. Here are a few examples of sites we have done that have the two different logos:

Andy Andrews

Canine Inc

Walden's Ridge

Posted in Code - Project83 - Join the Discussion

Building on the momentum from my last post, it's a perfect time to talk about when it's okay for web designers to drop support for IE6. Of course WE want it, but I don't believe designers and developers get to make this call when they choose. Lots of prominent designers disagree with me, but I don't think it's up to us to decide.

Fact is, studies show that most people still using IE6 don't have a choice to upgrade. As of May 2009, 60% of companies still use IE6 as their default browser. That's something we are compelled to take into account.

In recent memory we've seen popular web apps like 37signals apps, Apple's MobileMe, Google Apps and YouTube stop support for this browser. Since these apps cater to a more computer savvy audience, I believe it's makes complete sense for them. However, it doesn't provide just cause for people that build public websites to stop supporting it without more research. I don't believe Apple.com will stop optimizing for IE6 anytime soon if it means they can sell more Macs.

I did a quick survey of a bunch of our clients. I saw anywhere from 5-15% of users to our client websites are still using IE6. That's too high for me, so we're going to keep supporting it. Until that percentage creeps to roughly 3% or lower, I believe it's our obligation to make sure it works.

"Making it work" can mean a number of things. I don't think websites have to look exactly the same in IE6 as they do in modern web browsers. We geeks call this progressive enhancement, but as long as the website is still functional and degrades gracefully I have no issue with it.

A slightly more aggressive approach is being taken by the creators of IE6nomore.com, which I find pretty interesting. They have created some really simple code people can add to their website, which shows a banner like this to any IE6 visitors:

Outdated Browser
That's a pretty cool idea!

No matter what, if you do choose to drop support for IE6 make sure you are doing it for the right reasons. Doing it out of frustration, laziness or indifference isn't the best way to communicate expertise to clients and potential clients.

Posted in Code - Join the Discussion (1 Comments)

By far the most difficult thing to master when learning to code with web standards is browser optimization. It seems like every layout presents a different challenge than the last one. It's taken me years to figure out pretty much every bug Internet Explorer can throw my way, but I still struggle from time to time.

Part of coping with Internet Explorer (versions 6 and 7 in particular) means having some nifty tricks that can help you in a pinch. Some of my tricks aren't very well publicized, so I'm posting them here in hopes of saving at least one person from taking their frustration out on their innocent laptop, wife or dog.

Target IE6 or IE7 with a single character

A quick way to set a specific style that applies only to IE6 or IE7 without the use of conditional comments or javascript is to try the following simple syntax in your stylesheet. Just add a simple asterisk or underscore before the property:

#someElement {     
background: red; /* modern browsers */
*background: green; /* IE 7 and below */
_background: yellow; /* IE6 exclusively */
}

Credit: NetTuts.com- http://bit.ly/8TIJSs

Using text-indent on an input in IE6

If you use image sprites (which I highly recommend), you are likely to run into this problem. The text-indent property does not work on an input. So you may get an image that looks like this:

Submit Button

Instead of hiding the text and replacing it with an image, both show up in IE6. The following three lines must be added to your IE stylesheet for the input in question to fix the problem:

display:block;
font-size: 0px;
line-height: 0px;

Usually the display:block property is unnecessary if you are using a sprite or image replacement technique to replace the text (because it's already a block level element).

Credit: ProductiveDreams.com- http://bit.ly/12YKXi

IE8 Rendering Modes

In typical Microsoft fashion, they actually present you with a bunch of rendering mode options in Internet Explorer 8 instead of just doing it the right way all of the time. So if you write code properly and it validates, you will want to add the following meta tag to the <head> ALL of your layouts moving forward:

<meta http-equiv="X-UA-Compatible" content="IE=8" />	

This tag enables the IE8 standards compliance mode on your website, which you would think should be active all the time.

Credit: Zurb.com- http://bit.ly/4RGzk

CSS Browser Selector

When all else fails, I use an amazing 1.1k javascript file called CSS browser selector. This script lets you code styles for tons of different technical configurations. You can set attributes by operating system, browser and browser version. It also cleverly adds a "js" class when javascript is enabled so that you can set styles up for when it is disabled. Pure awesomeness ...

Credit: http://rafael.adm.br/css_browser_selector/

The Alpha-transparent PNG Paradox

One super-annoying issue with IE6 is that it doesn't know how to read alpha-transparent images properly. So if you have an image with a shadow over a transparent background or something with an opacity less than 100%, chances are it won't look so hot in IE6.

This isn't an easy fix. There are a number of recipes that cover different situations. Frankly, we try to avoid all of them if we can and simply use different images in IE6 that don't have alpha-transparency. It doesn't look as cool, but that's what they get for using a crappy browser. Here are the best solutions I know about:

Posted in Code - Join the Discussion

A few weeks ago, the Google Analytics team released some fantastic updates to their software. Of particular interest to me was enhanced mobile tracking and reporting capabilities.

The first thing to understand about mobile website reporting is that it works differently than regular websites. Many mobile browsers do not yet support javascript, the language that platforms like Analytics depend on to track website stats. Instead of the browser doing the work, mobile tracking leans on some server-side code to track visitors properly.

Don't be intimidated by all the geek-speak though. It's not rocket science to implement analytics on your mobile site. If you've got five minutes, know how to use FTP and don't mind copy/pasting some code, you can do this!

1. Add a new profile

From your Google Analytics account, add your mobile site as a new domain.

Analytics

2. Get advanced tracking code

Once your site is added, Analytics will give you a Web Property ID like normal. Keep this around for future reference. In the example below, the Property ID is highlighted.

Click on the "Advanced" tab under the instructions heading. Then click on the radio button to select "A site built for a mobile phone". Finally, select the server-side language. I will be using PHP for this example.

Analytics

3. Insert the snippets

There are two snippets of code that you must copy/paste onto each page of your mobile website. One goes at the top of each page, right after the <html> tag. The other goes at the bottom of each page, prior to the </body> tag.

Note that the third line of the top snippet includes your Web Property ID. This varies for each domain, so be sure to change it if you ever re-use this code.

4. Upload the file

Lastly, you have to download this file called "ga.php" and upload it to the root directory your domain. This means if your domain is m.yourdomain.com, this file needs to be at m.yourdomain.com/ga.php.

5. Test and Celebrate!

Give it a few minutes and refresh the tracking status in Google Analytics. Assuming you followed the directions, you should be in great shape.

Known Limitations

Lastly, it's important to familiarize yourself with the currently known issues, published by Google in the sample code instructions:

  • Multiple instances- you can't run multiple copies of the script on your website, or use it in conjunction with the standard javascript tracking code.
  • Inaccurate locations- Since Google determines users' location using their IP address, tracking this from a mobile device is not as accurate.
  • Server load- Due to the additional code required to track visitors properly, additional server load may be possible.

Posted in Code - Software - Join the Discussion (8 Comments)