Google Analytics Configuration
Google Analytics is a free web statistics service provided
by Google. This plugin simplifies the process of including
the basic Google Analytics code in your blog, so you don't
have to edit any PHP. If you don't have a Google Analytics account
yet, you can get one at
google.com/analytics.
In the Google interface, when you first register or when
you "Add Website Profile" you are shown a piece of JavaScript that you
are told to insert into your web pages. In that script is a unique
string that identifies the website you just defined, that is your User
Account string (shown in bold in the example below).
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "$mulch";
urchinTracker();
</script>
Once you have entered your User Account String in
the form below, your posts and pages should be trackable by
Google Analytics. Google updated their analytics tracking code in October 2007 from the old-style urchin.js code to a new ga.js. (more info) For new installations, the new style is recommended.
END;
$opt = get_option('analytics_uastring');
$opt2 = get_option('analytics_newstyle');
if (!isset($opt)) {
add_action('admin_footer', array('GA_Admin','warning'));
} else if (!isset($opt2)) {
add_action('admin_footer', array('GA_Admin','warning2'));
} else {
if ($opt == "") {
add_action('admin_footer', array('GA_Admin','warning'));
} else if ($opt2 == "") {
add_action('admin_footer', array('GA_Admin','warning2'));
} else {
if (isset($_POST['submit'])) {
if ($opt2 == "false") {
add_action('admin_footer', array('GA_Admin','success'));
} else {
add_action('admin_footer', array('GA_Admin','success2'));
}
}
}
}
} // end config_page()
function success() {
echo "
Options updated successfully. View the source of your blog pages and search for 'urchin' to see what this plugin adds.
";
} // end success()
function success2() {
echo "
Options updated successfully. View the source of your blog pages and search for 'pageTracker' to see what this plugin adds.
";
} // end success2()
function warning() {
echo "
Google Analytics is not active. You must enter your UA String in the box marked \"Analytics User Account\" for it to work.
";
} // end warning()
function warning2() {
echo "
Google Analytics is not active. You must decide if you want the new style of tracking (ga.js). The new style is recommended for all new installations.
";
} // end warning2()
} // end class GA_Admin
} //endif class_exists( 'GA_Admin' )
/**
* Code that actually inserts stuff into pages.
*/
if ( ! class_exists( 'GA_Filter' ) ) {
class GA_Filter {
function analytics_cats() {
global $dir, $post;
foreach (get_the_category($post->ID) as $cat) {
$profile = get_option('analytics_'.$cat->category_nicename);
if ($profile != "") {
return $profile;
}
}
return '';
} //end analytics_cats()
function spool_analytics() {
global $uastring, $post, $new_style;
// check if there's a post level profile
// and if so, use it.
if (function_exists("get_post_meta")) {
$ua = get_post_meta($post->ID, $uakey);
if ($ua[0] != "") {
GA_Filter::spool_this($ua, $new_style);
return;
}
}
// check if any of the categories this post
// belongs to have a profile, and if so
// use the first one that's found
//
// TO DO switch on when maintenence UI is done
//
// $ua = analytics_cats();
// if ($ua != "") {
// spool_this($ua, $new_style);
// return;
// }
// use the default channel if there is
if ($uastring != "") {
GA_Filter::spool_this($uastring, $new_style);
return;
}
// if we get here there is a problem
echo("\n");
} // end spool_analytics()
/*
* Insert the tracking code into the page
*/
function spool_this($ua, $new_style) {
global $includeUDN;
if ($new_style) {
$gaurl = "http://www.google-analytics.com/ga.js";
if($_SERVER['HTTPS'] == "on") $gaurl = "https://ssl.google-analytics.com/ga.js";
print <<
END;
} else {
$udn = "";
if ($includeUDN) $udn = "_udn = ".$_SERVER['HTTP_HOST']."\n";
print <<
END;
}
}
/* Create an array which contians:
* "domain" e.g. boakes.org
* "host" e.g. store.boakes.org
*/
function ga_get_domain($uri){
$hostPattern = "/^(http:\/\/)?([^\/]+)/i";
$domainPattern = "/[^\.\/]+\.[^\.\/]+$/";
preg_match($hostPattern, $uri, $matches);
$host = $matches[2];
preg_match($domainPattern, $host, $matches);
return array("domain"=>$matches[0],"host"=>$host);
}
/* Take the result of parsing an HTML anchor ($matches)
* and from that, extract the target domain. If the
* target is not local, then when the anchor is re-written
* then an urchinTracker call is added.
*
* the format of the outbound link is definedin the $leaf
* variable which must begin with a / and which may
* contain multiple path levels:
* e.g. /outbound/x/y/z
* or which may be just "/"
*
*/
function ga_parse_link($leaf, $matches){
global $origin, $new_style;
$target = GA_Filter::ga_get_domain($matches[3]);
$coolbit = "";
if ( $target["domain"] != $origin["domain"] ){
if ($new_style) {
$coolBit .= "onclick=\"javascript:pageTracker._trackPageview ('".$leaf."/".$target["host"]."');\"";
} else {
$coolBit .= "onclick=\"javascript:urchinTracker ('".$leaf."/".$target["host"]."');\"";
}
}
return '' . $matches[5] . '';
}
function ga_parse_article_link($matches){
global $post;
$slug = $post->post_name;
return GA_Filter::ga_parse_link("/outbound/article/$slug",$matches);
}
function ga_parse_comment_link($matches){
global $post;
$slug = $post->post_name;
return GA_Filter::ga_parse_link("/outbound/comment/$slug",$matches);
}
function the_content($text) {
if(is_feed()) return $text;
static $anchorPattern = '/(.*?)<\/a>/i';
$text = preg_replace_callback($anchorPattern,array('GA_Filter','ga_parse_article_link'),$text);
return $text;
}
function comment_text($text) {
if(is_feed()) return $text;
static $anchorPattern = '/(.*?)<\/a>/i';
$text = preg_replace_callback($anchorPattern,array('GA_Filter','ga_parse_comment_link'),$text);
return $text;
}
function comment_author_link($text) {
global $post, $new_style;
$slug = $post->post_name;
static $anchorPattern = '(.*href\s*=\s*)[\"\']*(.*)[\"\'] (.*)';
ereg($anchorPattern, $text, $matches);
if ($matches[2] == "") return $text;
$target = GA_Filter::ga_get_domain($matches[2]);
$coolbit = "";
$origin = GA_Filter::ga_get_domain($_SERVER["HTTP_HOST"]);
if ( $target["domain"] != $origin["domain"] ){
if ($new_style) {
$coolBit .= " onclick=\"javascript:pageTracker._trackPageview('/outbound/commentauthor/$slug/".$target["host"]."');\" ";
} else {
$coolBit .= " onclick=\"javascript:urchinTracker('/outbound/commentauthor/$slug/".$target["host"]."');\" ";
}
}
return $matches[1] . "\"" . $matches[2] . "\"" . $coolBit . $matches[3];
}
} // class GA_Filter
} // endif
$uakey = "analytics";
if (function_exists("get_option")) {
if ($wp_takes_precedence) {
$uastring = get_option('analytics_uastring');
$new_style = get_option('analytics_newstyle');
}
}
$mulch = ($uastring=""?"##-#####-#":$uastring);
if ($new_style == "true") $new_style = true;
else $new_style = false;
$gaf = new GA_Filter();
$origin = $gaf->ga_get_domain($_SERVER["HTTP_HOST"]);
if (!function_exists("add_GA_config_page")) {
} //endif
// adds the menu item to the admin interface
add_action('admin_menu', array('GA_Admin','add_config_page'));
// adds the footer so the javascript is loaded
add_action('wp_footer', array('GA_Filter','spool_analytics'));
// adds the footer so the javascript is loaded
add_action('bb_foot', array('GA_Filter','spool_analytics'));
// filters alter the existing content
add_filter('the_content', array('GA_Filter','the_content'), 99);
add_filter('the_excerpt', array('GA_Filter','the_content'), 99);
add_filter('comment_text', array('GA_Filter','comment_text'), 99);
add_filter('get_comment_author_link', array('GA_Filter','comment_author_link'), 99);
?>