Gearside Design

Using server-side Google Analytics for sending pageviews and event tracking

Most of the time websites send data to Google Analytics using their JavaScript method, and most of the time this works great. However, there are other times that you really need to send a pageview or an event to Google Analytics from the server-side. Converting your server-side data to JavaScript in order to be sent is bloated and unnecessary- and sometimes just will not work. Fortunately, Google has provided other ways of sending data to their analytics software (did you know you can send data to Google Analytics using CSS? It’s also great for tracking email data.). In this post, I’ll be showing you how to create some PHP functions to send data to Google Analytics without needing JavaScript!

Setting up the functions

Simply copy and paste these PHP functions onto your site to enable Google Analytics data using only your server-side code. Special thanks to stumiller.me for his research- I’ve customized some of his functions to work on any WordPress (or other PHP) website.

PHP

Be sure to change the Google Analytics tracking ID to your own in both places!

Sending data to Google Analytics

Now that you have the utility functions in place, all you have to do is call one of the following to send data. To send a pageview, use this syntax:

ga_send_pageview($hostname, $page, $title);

Actual example:

ga_send_pageview('gearside.com', 'https://gearside.com/nebula/', 'Nebula by Gearside');

To send an event, use this syntax:

ga_send_pageview($category, $action, $label);

Actual example:

ga_send_pageview('Contact Form', 'Submit', 'Feature Request');

Use cases

I use server-side Google Analytics tracking to supplement my JavaScript tracking for things like the following: