Google Analytics PHP Server Side Tracking

Most of us tend to use Google Analytics with JavaScript for a simple and effective way of tracking our users interactions. However there are times where we need to do some tracking on the server side and Google does provide a server side tracking API. All of the normal analytics tracking tools are available via HTTP requests, and thanks to user Dan Cameron on Github, we also have a wrapper that will allow us to easily implement server side event tracking with PHP.

To get things setup, you will first need to download the class here.

Once you have downloaded it, place it in a directory that you can access and include it on the file you are wanting to track.

include('/includes/ss-ga.class.php');

Next, you will need to create an object with your Google analytics account id.

$ssga = new ssga( 'UA-YOUR_NUMBER', 'yoursite.com' );

If you would just like to send a “page view” then all you will need to do is the following:

//Set a pageview
$ssga->set_page( '/page.php' );
$ssga->set_page_title( 'Page Title' );

// Send
$ssga->send();
$ssga->reset();

Simple. Next, if you would like to setup some event tracking, then you will add the following:

$ssga->set_event( 'Feed', 'Categories', 'Label', 'Value');
$ssga->send();

That will send an event to your GA account and that’s it. You’re done! Google Analytics on the server side is now possible!

Questions? Comments? Leave a note in the comments below or reach out us at connect@mediacause.org. Follow us on Facebook and Twitter for more nonprofit marketing tips, and sign up for our newsletter for a monthly update from our nonprofit marketing pros!

Search

Related Posts