A TEXT POST

New Referrals (links) As Google Analytics’ Goals

I’ve been experimenting with setting up a goal in Google Analytics that tracks new referrals, ie, links.  This way if I have a blog post or seo tool that becomes popular I can track how popular it actually was in terms of new links pointing to it.   Consider this scenario: You create a great piece of link bait on your website.  Next you need to promote it.  Part of your off page strategy involves ppc ads on Google in order to gain visibility and in turn generate links.  With the ability to track new links as goals, you can then import that goal into Google Adwords and track it as a conversion.  In order to accomplish this you need to use the Google Analytics Data Export API to check if the referring url is already in Google Analytics as well as jQuery.  Enough said, here is how I do it:

1) Create a file called trackReferrer.php to do the work with Google Analytics Export API. I prefer using the GAPI class. What we need to do is query GA for a referring url. If it exists, it is not a new link. I really only care about new domains that link, not new pages on sites already linking, so im just querying the host, not the path.

< ?php
//load the GAPI class
include
("/path/to/gapi.class.php");
define
('ga_email','xxxxxxx@gmail.com');
define
('ga_password','xxxxxx');
define
('ga_profile_id','xxxxxx');

$ga
= new gapi(ga_email,ga_password);

//pass the referrer via a POST parameter
$url
= filter_input(INPUT_POST,
                     
'url',
                      FILTER_SANITIZE_STRING
);

$url
= parse_url($url);
//GA filters our www subdomains, so we must do the same
if(@substr($url['host'],0,4)=='www.') {
  $host
=  str_replace('www.','',$url['host']);
} else {
  $host
=@ $url['host'];
}

$sort
=null;
$filter
="source=~'^$host' && medium==referral'";

//this date should be one day before you installed GA
$start_date
= '2006-01-01';
$end_date
= date('Y-m-d',strtotime("+1 day"));
$start_index
=1;
$max_results
=100000;

$ga
->requestReportData(ga_profile_id,
                      array
('source','referralPath'),
                      array
('visits'),
                      $sort
,
                      $filter
,
                      $start_date
,
                      $end_date
,
                      $start_index
,
                      $max_results
);

//if greater than 0, its not new
if (count($ga->getResults())==0)
{
  echo
1;
} else {
 
//new domain linking
  echo
0;
}

2) The next step is to add a bit of code to the footer of each page of your site that passes the HTTP_REFERER to the new page we just created. I use jQuery to post the referrer after the page is loaded because the GA Export API can take some time to load, so i don’t want to slow down my site for the user.

< script type="text/javascript" >
$(document).ready(function(){   $.ajax({
          url: 'trackReferrer.php',
          dataType: "HTML",
          type: 'POST',
          data: {'url': '< ?php echo $_SERVER['HTTP_REFERER']; ? >'},
          success: function(msg) {
            if(msg==1) {
               pageTracker._trackPageview('/virtual/new_link')
            }
         }
   })
 });
 < /script >

3) The last step is to setup a regular expression goal in Google Analytics with the url path being /virtual/new_link(.*), then import that goal to adwords if you are doing it for that sake.