URL Shortening for Google Analytics Campaign Parameters

in Google Analytics

Google Analytics uses parameters in the URL query string to track referrer details such as the campaign source, medium, and keyword; however, these URLs can get quite long, and in some situations you need to make them shorter. This article discusses why you might want a shorter URL, then presents two approaches to doing it; one using the bit.ly API, and another using Apache Rewrite Rules.

Reasons to Shorten URL Parameters

As you are probably aware, the URLs used to track campaigns in Analytics can get quite long: For instance, here’s one I’m using on my LinkedIn company page (http://www.linkedin.com/company/ppc-bullet):

http://www.ppcbullet.com/home?utm_source=linkedin&utm_medium=organic&utm_content=page
A URL Cluttered with Parameters

Even without any campaign or keyword parameters it’s still quite long, and could be reduced for one of the following reasons:

  • It looks messy in situations where you don’t have control over how the link is formatted: For example, I’m using this link on LinkedIn where it’s wrapped across two lines and cut off at the end. This looks ugly when an profile URL would usually just contain the domain name; I really want a clean URL to put onto my profile.
  • A link like this isn’t suitable for Twitter: It’s a quite short example, but already 85 characters long.
  • You might need to pass links to other people, and they could mess up the parameters.

Using the bit.ly API To Reduce the URL Size

One simple way to shorten a URL is using a service such as bit.ly: You can either construct the URL and enter it into the normal shortening form, or you can use their API to create your own URL builder that combines parameter construction and shortening into one tool. Here’s an example PHP function that will take the parameters and call the API to generate the condensed URL:

define("BITLY_API_USER", "YOUR_API_USER"); 
define("BITLY_API_KEY", "YOUR_API_KEY"); 
 
function shortenUrl($url, $source = null, $medium = null, 
   $campaign = null, $term = null, $content = null) { 
   // Parse the URL 
   $urlParts = parse_url($url); 
   $scheme = isset($urlParts["scheme"]) ? $urlParts["scheme"] : "http"; 
   $host = isset($urlParts["host"]) ? $urlParts["host"] : ""; 
   $port = isset($urlParts["port"]) ? $urlParts["port"] : ""; 
   $path = isset($urlParts["path"]) ? $urlParts["path"] : "/"; 
   $query = isset($urlParts["path"]) ? $urlParts["query"] : ""; 
 
   // Construct the query 
   if ($source) { 
      $query .= "&utm_source=" . urlencode($source); 
   } 
   if ($medium) { 
      $query .= "&utm_medium=" . urlencode($medium); 
   } 
   if ($campaign) { 
      $query .= "&utm_campaign=" . urlencode($campaign); 
   } 
   if ($term) { 
      $query .= "&utm_term=" . urlencode($term); 
   } 
   if ($content) { 
      $query .= "&utm_content=" . urlencode($content); 
   } 
   $url = "$scheme://$host:$port$path?$query"; 
 
   // API Request 
   $file = "/v3/shorten?"; 
   $file .= "login=" . BITLY_API_USER; 
   $file .= "&apiKey=" . BITLY_API_KEY; 
   $file .= "&longUrl=" . urlencode($url); 
   $file .= "&format=json"; 
 
   // Send the request 
   $curl = curl_init(); 
   curl_setopt($curl, CURLOPT_URL, "http://api.bit.ly$file"); 
   curl_setopt($curl, CURLOPT_HEADER, false); 
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
   $curlData = curl_exec($curl); 
   curl_close($curl); 
 
   // Get the URL from the response 
   $obj = json_decode($curlData); 
   return $obj->data->url; 
}
Reducing the Size of a URL Using the bit.ly API

Most of this function deals with creating the URL from your campaign settings. The final part generates a request to the API, and parses the response to extract the short URL. To use it you need to enter an API username and token into the constants at the top. You can sign up here, then, once you’ve signed up, your key is located here. This is an example of using the function:

$longUrl = "http://www.example.com"; 
$source = "src"; 
$medium = "med"; 
$campaign = "camp"; 
$term = "kwd"; 
$content = "ad"; 
$shortUrl = shortenUrl($longUrl, $source, $medium, $campaign, $term, $content); 
print "$shortUrl\n";
Using the URL Shortening Function

Expanding a Shortened URL Using Apache Rewrite Rules

The bit.ly approach works in many cases, but sometimes you still want to keep the domain name in the URL. For example, in my LinkedIn case above, I want www.ppcbullet.com to appear in the profile, but not in the long query string.

This can be done by putting something into the URL that can be picked up on the server and used to redirect to the full URL with the appropriate campaign tracking parameters. One way of doing this is to use Apache mod_rewrite; one rule for each shortened URL. The following rewrite rule will redirect my profile URL:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} linkedin 
RewriteRule .* http://www.ppcbullet.com/?utm_source=linkedin&utm_medium=organic&utm_content=page [R=301,L]
Rewrite Rule that Adds Tracking Parameters to a Shorter URL

This rule redirects the URL www.ppcbullet.com/linkedin to http://www.ppcbullet.com/?utm_source=linkedin&utm_medium=organic&utm_content=page. It’s not perfect, but a lot better than the original. If you have many URLs you want to do this with or you don’t want to use mod_rewrite, then you could do a similar thing with a script that pulls the URL mapping out of a database.

Comments

I'm keen to get feedback on my posts, so if you have any questions or comments, then please send me a message and I'll be happy to help.