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 Bit.ly.
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 Bit.ly’s API. For the code to connect to Bit.ly’s API, I found the code snippet on David Walsh’s site.
<?php /* make a URL small */ function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') { //create the URL $bitly = 'http://api.bit.ly/shorten?version='.$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 'http://bit.ly/'.$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.
<?php //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) { $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 bit.ly 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"> <tr> <td width="250">Campaign Source:</td> <td width="450"><input type="text" name="campaign_source" /></td> </tr> <tr> <td>Campaign Term:</td> <td><input type="text" name="campaign_term" /></td> </tr> <tr> <td>Campaign Content:</td> <td><input type="text" name="campaign_content" /></td> </tr> <tr> <td>Campaign Name:</td> <td><input type="text" name="campaign_name" /></td> </tr> <tr> <td>Web address (full URL):</td> <td><input type="text" name="weblink" /></td> </tr> <tr> <td colspan=2><input type="submit" name="createLinks" value="Shorten" /></td> </tr> </table> </form>
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.
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.