Tracking server-side HTTP Redirects with Google Analytics

When you need to be able to create HTTP redirects within WordPress, it’s hard to beat the Taylor Lovett’s Safe Redirect Manager.

Safe Redirect Manager logo

Safe Redirect Manager creates a WordPress interface for managing HTTP redirects. Did you accidentally share the wrong URL with your customers? Fix the link, then simply create a redirect to ensure the user reaches their destination.

Here at Growella, we’re using Safe Redirect Manager for branded affiliate links; instead of sending users to <some-partner-url>/<some-long-affiliate-string>, we’re able to create URLs like https://growella.com/r/<partner-name>. Should the partner URL ever change, we’ll be able to update the target in a single place: within Safe Redirect Manager.

Being the bunch of data nerds that we are, we wanted to make sure we’re able to keep track of how often people are using our redirect links (a feature Safe Redirect Manager doesn’t offer out of the box). The HTTP redirects should show up in our Nginx logs, but we wanted something more readable: namely, Google Analytics.

Google Analytics Measurement Protocol

Fortunately, Google Analytics has an API — the Google Analytics Measurement Protocol — for sending raw data to the platform. By passing a few basic parameters in an HTTP POST request, we’re able to record data each time a user hits our redirect via Safe Redirect Manager.

The code to do this is pretty simple: we’ll build an array of details about the redirect, then POST it to Google Analytics using a non-blocking wp_remote_post() call:

/**
 * Log a redirect from Safe Redirect Manager.
 *
 * Just before calling wp_safe_redirect(), Safe Redirect
 * Manager fires the "srm_do_redirect" action, which acts
 * as the entry point for this function.
 *
 * For a full list of configuration options available for
 * Google Analytics' Measurement Protocol:
 *
 * @link https://goo.gl/DOz6mZ
 *
 * @param string $request The URL requested by the browser.
 * @param string $redirect The redirected URL.
 * @param int $status_code The HTTP status code for the
 *                         HTTP redirect.
 */
function log_redirect( $request, $redirect, $status_code ) {
 
  /*
   * Get your Google Analytics profile ID from whatever
   * plugin, setting, etc. you're using.
   */
  $profile_id = 'UA-XXXXXXXX-X';
  
  // Get the Google Analytics client ID.
  if ( isset( $_COOKIE['_ga'] ) ) {
    $client_id = sanitize_text_field( $_COOKIE['_ga'] );
  } else {
    $client_id = 'some-default-client-id';
  }

  // Assemble the POST body.
  $post_body  = array(
    'v'   => 1,           // API version.
    't'   => 'event',     // Type of hit.
    'tid' => $profile_id, // Google Analytics profile ID.
    'ec'  => 'Redirects', // "Event Category".
    'ea'  => $redirect,   // "Event Action".
    'el'  => $request,    // "Event Label".
    'cid' => $client_id,  // The client ID.
  );

  // Send the result to Google Analytics.
  wp_remote_post(
    'https://www.google-analytics.com/collect',
    array(
      'blocking'    => false,
      'body'        => $post_body,
      'httpversion' => '1.1',
  ) );
}
add_action( 'srm_do_redirect', 'log_redirect', 10, 3 );

This allows us to collect information about when our HTTP redirects (specifically those managed through Safe Redirect Manager) are used. Google Analytics Measurement Protocol also includes a number of optional parameters, which allow you to collect even more information about the user making the request.

We’ve literally just rolled this out on Growella, but we’re excited at the prospect of having nice, clean affiliate links with tracking in Google Analytics!

Leave a Reply