Mining Google Analytics data with links

Facebook, LinkedIn, and Twitter – Who’s the cooler audience?

Using Google Analytics, you’re able to track information about your site, its performance, and other cool information such as traffic sources, medium, and campaign information. Being connected and using social networks such as Facebook, Twitter, and LinkedIn, you would want to know which social network produces the highest impact on traffic to your site. In essence, what you really need to do is create three separate links and then shorten it with

I recently wrote a quick script that does just that – allows embedding of Google campaign information, and produces three separate links to be shortened using’s API. For the code to connect to’s API, I found the code snippet on David Walsh’s site.

/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
  //create the URL
  $bitly = ''.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
  //get the url
  //could also use cURL here
  $response = file_get_contents($bitly);
  //parse depending on desired format
  if(strtolower($format) == 'json')
    $json = @json_decode($response,true);
    return $json['results'][$url]['shortUrl'];
  else //xml
    $xml = simplexml_load_string($response);
    return ''.$xml->results->nodeKeyVal->hash;

The code above simply connects to their API and shortens the URL. Your code would have to supply their API with the various links first. With a little modification, you can include a form that takes in as parameter the Google Analytics information and the link.

//The User's input
$campaign_source = $_POST['campaign_source'];
$campaign_term = $_POST['campaign_term'];
$campaign_content = $_POST['campaign_content'];
$campaign_name = $_POST['campaign_name'];
$weblink = $_POST['weblink'];

$utm_source = "?utm_source=";
$bitlyUser = "your_bitly_username";
$bitlyAPI = "your_bitly_api_password";

if (strstr($weblink,'?')) {
	$utm_source = "&utm_source=";

//Prepare the URL structure
function createLink($campaign_medium) {
	global $campagin_source;
	global $campaign_term;
	global $campaign_content;
	global $campaign_name;
	global $weblink;
	$newLink = $weblink.$utm_source.$campaign_source."&utm_medium=".$campaign_medium."&utm_term=".$campaign_term."&utm_content=".$campaign_content."&utm_campaign=".$campaign_name;
	return $newLink;

$mediumFacebook = createLink('facebook');
$mediumLinkedIn = createLink('linkedin');
$mediumTwitter = createLink('twitter');

//Create the shortened URLs
$shortFacebook = make_bitly_url($mediumFacebook,$bitlyUser,$bitlyAPI,'json');
$shortLinkedIn = make_bitly_url($mediumLinkedIn,$bitlyUser,$bitlyAPI,'json');
$shortTwitter = make_bitly_url($mediumTwitter,$bitlyUser,$bitlyAPI,'json');

//Display results
echo "<h3><strong>Your Information</strong></h3>";
echo "<p>Campaign Source: ".$campaign_source."<br />";
echo "Campaign Term: ".$campaign_term."<br />";
echo "Campaign Content: ".$campaign_content."<br />";
echo "Campaign Name: ".$campaign_name."<br />";
echo "Web address: ".$weblink."</p>";

echo "<h3><strong>Shortened Links</strong></h3>";

echo "<p><strong>Facebook</strong><br />";
echo "<em>Long:</em> ".$mediumFacebook."<br />";
echo "<em>Short:</em> <a href=$shortFacebook>".$shortFacebook."</a></p>";

echo "<p><strong>LinkedIn</strong><br />";
echo "<em>Long:</em> ".$mediumLinkedIn."<br />";
echo "<em>Short:</em> <a href=$shortLinkedIn>".$shortLinkedIn."</a></p>";

echo "<p><strong>Twitter</strong><br />";
echo "<em>Long:</em> ".$mediumTwitter."<br />";
echo "<em>Short:</em> <a href=$shortTwitter>".$shortTwitter."</a></p>";

In the above code, I’m not capturing the campaign medium as this is specified in the function. My medium is either Facebook, LinkedIn or Twitter. The function will create a unique link for all three services, and with that you can track which social network gets you more traffic on your site.

The fields you can capture for Google are below. The descriptions are taken from the Google URL Builder site. Simply add a form to take in these values and you’re well on your way to mining more data from your shortened links.

<form name="BITLY" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
  <table width="700" border="0" cellspacing="0" cellpadding="5">
      <td width="250">Campaign Source:</td>
      <td width="450"><input type="text" name="campaign_source" /></td>
      <td>Campaign Term:</td>
      <td><input type="text" name="campaign_term" /></td>
      <td>Campaign Content:</td>
      <td><input type="text" name="campaign_content" /></td>
      <td>Campaign Name:</td>
      <td><input type="text" name="campaign_name" /></td>
      <td>Web address (full URL):</td>
      <td><input type="text" name="weblink" /></td>
      <td colspan=2><input type="submit" name="createLinks" value="Shorten" /></td>

Campaign Source: utm_source
Use utm_source to identify a search engine, newsletter name, or other source.

Campaign Medium: utm_medium
Use utm_medium to identify a medium such as email or cost-per- click.

Campaign Term: utm_term
Used for paid search. Use utm_term to note the keywords for this ad.

Campaign Content: utm_content
Used for A/B testing and content-targeted ads. Use utm_content to differentiate ads or links that point to the same URL.

Campaign Name: utm_campaign
Used for keyword analysis. Use utm_campaign to identify a specific product promotion or strategic campaign.

1 Comment

  1. Patti Murphy

    I love that your solution involves just cutting and pasting and URL and all that shit’s done for you. That’s my kinda app. Those screenshots made my eyes hurt.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>