Track JavaScript Disabled Visits with Google Analytics in WordPress

Update: With the latest GA4 replacing Universal Analytics, this code doesn’t work anymore. However, Google will still continue to collect data from Universal Analytics, though we don’t know when they’ll stop. As of this writing, they haven’t retired it yet – I’m continuing to use it myself.

As you know, Google Analytics requires you to place JavaScript into your pages to perform its magic. This works great under normal circumstances. But more and more people are using ad-blockers and other similar tools to disable scripting on the pages they visit. How do you track JavaScript-disabled visits using the same Google Analytics interface that you use for regular visits?

Most solutions on the web require a workaround like inserting code into images, iframes, and other shady techniques. While they might work, they’re not “clean”. Luckily for us, Google introduced something called the “Measurement Protocol” that allows us to manually send hits to our Google Analytics properties. Using a bit of PHP magic, we can easily track these users even if they visit via a browser that has JavaScript disabled.

As an example, I’m going to write some code that informs Analytics that a visitor came to our website from Google – in other words, most likely a search result. You can then modify the code to suit your own needs.

How to Track JavaScript Disabled Visits from Google

In the place where you normally insert custom PHP code, paste the following:

function send_google_hits() {

	$ref = $_SERVER['HTTP_REFERER'];
	if(!strstr($ref, "google.com"))
		return;

	$title = "Hit from server";
	$current_url = get_permalink();

	$data = array(
		'v' => 1,
		'tid' => 'UA-XXXXXXXX-X',
		'cid' => get_current_user_id(),
		't'   => 'pageview',
		'dh'  => 'testing.wp-tweaks.com',
		'dp'  => $current_url,
		'dt'  => $title,
	);

	wp_remote_post(
        	'https://ssl.google-analytics.com/collect',
        	array( 'body' => $data )
	);

}
add_action( 'wp_head', 'send_google_hits', 10, 1 );

Replace the code in bold with your own Google Analytics UA number. If you don’t know where it is, you can find it here in the GA interface:

get-ua-id

This code will track all visits coming from Google, and display them in Google Analytics with a message like “Hit from server”.

Track Javascript disabled visits

Using this, you can create a custom filter to get the exact number of visitors regardless of whether or not they have JavaScript disabled.

How it Works

The Measurement Protocol employed by Google Analytics is dead simple. It’s just a URL with a bunch of parameters – each parameter gives a piece of information about the visit. Here’s the complete documentation of the Measurement Protocol. In the above example, I first check to see if the visitor is coming from Google. If so, I create the URL parameters in a “$data” object and send it via a wp_remote_post function call.

You can add as much or as little information in the parameters as you want. For example, you can create a new “dr” parameter to hold the referrer – in this case Google. But you don’t have to use this code only for visits from Google. You can use it to track any visit and send back whatever info you need.

You can get the complete list of parameters used with the Measurement Protocol here. There are four parameters that are necessary.

  1. v – the version number (I use 1)
  2. tid – the tracking number
  3. cid – anonymous client ID (I just use junk value or the current user ID
  4. t – hit type. This can be ‘pageview’, ‘screenview’, ‘event’, ‘transaction’, ‘item’, ‘social’, ‘exception’, ‘timing’.

The rest of the parameters are optional. You can also use this hit builder to construct your data pieces properly, send them to Google, and check if it’s working

So that’s how you track Javascript-disabled visits via Google Analytics. A simple piece of code without messing around with pictures, iFrames, or any other “under the table” technique!

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Speak Your Mind

*

WP-Tweaks