Gearside Design

Track when users have JavaScript disabled with Google Analytics

As web developers, we constantly prepare our websites to work if a user has JavaScript disabled. Other practices of graceful degradation are measurable through metrics within Google Analytics, but how can we tell if our users are disabling JavaScript? Additionally, you are missing pageviews on your site by not counting visitors without JavaScript!

One lesser-known fact is that Google Analytics events can be triggered via server-side code like PHP! With some clever snippets, we can detect which page a user is viewing when they have JavaScript disabled.

Create a new PHP file (generally in an /includes directory) that will be included if a user does not have JS enabled. In that file, add the following (don’t forget to add your own Google Analytics account number!):

PHP

Once that is in place, add one of the following <noscript> into the top of the <body> to trigger the above file. The first uses placeholders for page data, and the second shows how that data can be passed using WordPress.

PHP