Google Analytics to track your WordPress site statistics * Version: 1.2.2 * Author: Aaron D. Campbell * Author URI: http://xavisys.com/ */ define('WGA_VERSION', '1.2.2'); /** * Changelog: * 06/08/2008: 1.2.2 * - Fixed problem with code affecting Admin Javascript such as the TinyMCE editor * * 06/08/2008: 1.2.1 * - Bug fix for the stats gathering * * 06/08/2008: 1.2.0 * - No longer parses outgoing links in the admin section. * - Uses get_footer instead of wp_footer. Too many themes aren't adding the wp_footer call. * - Options page updated * - Added optional anonymous statistics collection * * 04/26/2008: 1.1.0 * - Major revamp to work better with the new Google Tracking Code. It seems that outgoing links weren't being tracked properly. * * 04/17/2008: 1.0.0 * - Added to wordpress.org repository * * 07/03/2007: 0.2 * - Fixed problem with themes that do not call wp_footer(). If you are reading this and you are a theme developer, USE THE HOOKS! That's what they're there for! * - Updated how the admin section is handled * * 02/21/2007: 0.1 * - Original Version */ /* Copyright 2006 Aaron D. Campbell (email : wp_plugins@xavisys.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ //If this is set to true, extra info will be dumped to the browser. //ONLY do this if you really need it define('WGA_DEBUG', false); /** * wpGoogleAnalytics is the class that handles ALL of the plugin functionality. * It helps us avoid name collisions * http://codex.wordpress.org/Writing_a_Plugin#Avoiding_Function_Name_Collisions */ class wpGoogleAnalytics { /** * This adds the options page for this plugin to the Options page */ function admin_menu() { add_options_page(__('Google Analytics'), __('Google Analytics'), 'manage_options', str_replace("\\", "/", __FILE__), array('wpGoogleAnalytics', 'options')); } /** * This is used to display the options page for this plugin */ function options() { /** * @var WP_Roles */ global $wp_roles; //Get our options $wga = wpGoogleAnalytics::get_options(); //Echo debug info if needed if (WGA_DEBUG) { echo '
',var_dump($wga),''; } //We will fill $roles with checkboxes to ignore each role $roles = ''; foreach ($wp_roles->roles as $role=>$role_info) { $checked = (isset($role_info['capabilities']['wga_no_track']) && $role_info['capabilities']['wga_no_track'])? ' checked="checked"':''; $role_info['name'] .= (strtolower(substr($role_info['name'], -1)) != 's')? 's':''; $roles .= "
',var_dump($wga),''; } //If $admin is true (we're in the admin_area), and we've been told to ignore_admin_area, return without inserting code if (is_admin() && (!isset($wga['ignore_admin_area']) || $wga['ignore_admin_area'] != 'false')) { $ret = "\r\n"; if ($output) { echo $ret; } return $ret; } elseif (is_404() && (!isset($wga['log_404s']) || $wga['log_404s'] != 'false')) { //Set track for 404s, if it's a 404, and we are supposed to $track['data'] = $_SERVER['REQUEST_URI']; $track['code'] = '404'; } elseif (is_search() && (!isset($wga['log_searches']) || $wga['log_searches'] != 'false')) { //Set track for searches, if it's a search, and we are supposed to $track['data'] = $_REQUEST['s']; $track['code'] = "search"; } //If we need to generate a special tracking URL if (isset($track)) { //get the tracking URL $track['url'] = wpGoogleAnalytics::get_url($track); //adjust the code that we output, account for both types of tracking $wga['code'] = str_replace("urchinTracker()","urchinTracker('{$track['url']}')", $wga['code']); $wga['code'] = str_replace("pageTracker._trackPageview()","pageTracker._trackPageview('{$track['url']}')", $wga['code']); //Echo debug info if needed if (WGA_DEBUG) { echo '
',var_dump($track, $site_url),''; } } //output the Google Analytics code if ($output) { echo $wga['code']; } return $wga['code']; } else { //If the Google Analytics code has not been set in admin area, return without inserting code $ret = "\r\n"; if ($output) { echo $ret; } return $ret; } } /** * Used to get one or all of our plugin options * * @param string[optional] $option - Name of options you want. Do not use if you want ALL options * @return array of options, or option value */ function get_options($option = null) { $o = get_option('wga'); if (isset($option)) { if (isset($o[$option])) { return $o[$option]; } else { return false; } } else { return $o; } } /** * Start our output buffering with a callback, to grab all links * * @todo If there is a good way to tell if this is a feed, add a seperate option for tracking outgoings on feeds */ function start_ob() { $log_outgoing = wpGoogleAnalytics::get_options('log_outgoing'); // Only start the output buffering if we care, and if it's NOT an XMLRPC REQUEST & NOT a tinyMCE JS file & NOT in the admin section if (($log_outgoing == 'true' || $log_outgoing === false) && (!defined('XMLRPC_REQUEST') || !XMLRPC_REQUEST) && !is_admin() && stripos($_SERVER['REQUEST_URI'], 'wp-includes/js/tinymce') === false) { ob_start(array('wpGoogleAnalytics', 'get_links')); } } /** * Grab all links on the page. If the code hasn't been inserted, we want to * insert it just before the