Google Analytics to track your WordPress site statistics
* Version: 1.4.0
* Author: Aaron D. Campbell
* Author URI: http://ran.ge/
* License: GPLv2 or later
* Text Domain: wp-google-analytics
*/
define('WGA_VERSION', '1.4.0');
/* 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
*/
/**
* 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 {
/**
* @var wpGoogleAnalytics - Static property to hold our singleton instance
*/
static $instance = false;
static $page_slug = 'wp-google-analytics';
var $tokens = array();
/**
* This is our constructor, which is private to force the use of get_instance()
* @return void
*/
private function __construct() {
add_filter( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'get_footer', array( $this, 'insert_code' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) );
add_filter( 'plugin_action_links', array( $this, 'add_plugin_page_links' ), 10, 2 );
}
/**
* Function to instantiate our class and make it a singleton
*/
public static function get_instance() {
if ( !self::$instance )
self::$instance = new self;
return self::$instance;
}
public function init() {
load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$this->tokens = array(
array(
'token' => '%the_author%',
'callback' => 'get_the_author',
'callback_returns' => 'string',
'description' => __( 'Post author for current view', 'wp-google-analytics' ),
'retval' => __( "Post author's display name", 'wp-google-analytics' ),
'ignore_when' => array(
'is_home',
'is_front_page',
'is_post_type_archive',
'is_page',
'is_date',
'is_category',
'is_tag',
),
),
array(
'token' => '%the_category%',
'callback' => array( $this, 'token_the_category' ),
'callback_returns' => 'string',
'description' => __( 'Categories assigned to a post', 'wp-google-analytics' ),
'retval' => __( "Category names in a commma-separated list", 'wp-google-analytics' ),
'ignore_when' => array(
'is_home',
'is_front_page',
'is_page',
'is_post_type_archive',
'is_author',
'is_tag',
),
),
array(
'token' => '%context%',
'callback' => array( $this, 'token_context' ),
'callback_returns' => 'string',
'description' => __( 'Which view the visitor is on', 'wp-google-analytics' ),
'retval' => __( "Samples: 'home', 'category', 'post', 'author'" ),
),
array(
'token' => '%the_date%',
'callback' => 'get_the_date',
'callback_returns' => 'string',
'description' => __( 'Publication date for the current view', 'wp-google-analytics' ),
'retval' => __( "Format specified by 'Date Format' in Settings -> General", 'wp-google-analytics' ),
'ignore_when' => array(
'is_home',
'is_front_page',
'is_post_type_archive',
'is_page',
'is_author',
'is_category',
'is_tag',
),
),
array(
'token' => '%the_tags%',
'callback' => array( $this, 'token_the_tags' ),
'callback_returns' => 'string',
'description' => __( 'Tags assigned to a post', 'wp-google-analytics' ),
'retval' => __( "Tag names in a commma-separated list", 'wp-google-analytics' ),
'ignore_when' => array(
'is_home',
'is_front_page',
'is_page',
'is_post_type_archive',
'is_date',
'is_category',
'is_author',
),
),
array(
'token' => '%is_user_logged_in%',
'callback' => 'is_user_logged_in',
'callback_returns' => 'bool',
'description' => __( 'Whether or not the viewer is logged in', 'wp-google-analytics' ),
'retval' => __( "'true' or 'false'", 'wp-google-analytics' ),
),
);
$this->tokens = apply_filters( 'wga_tokens', $this->tokens );
}
/**
* This adds the options page for this plugin to the Options page
*/
public function admin_menu() {
add_options_page(__('Google Analytics', 'wp-google-analytics'), __('Google Analytics', 'wp-google-analytics'), 'manage_options', self::$page_slug, array( $this, 'settings_view' ) );
}
/**
* Register our settings
*/
public function admin_init() {
register_setting( 'wga', 'wga', array( $this, 'sanitize_general_options' ) );
add_settings_section( 'wga_general', false, '__return_false', 'wga' );
add_settings_field( 'code', __( 'Google Analytics tracking ID:', 'wp-google-analytics' ), array( $this, 'field_code' ), 'wga', 'wga_general' );
add_settings_field( 'additional_items', __( 'Additional items to log:', 'wp-google-analytics' ), array( $this, 'field_additional_items' ), 'wga', 'wga_general' );
add_settings_field( 'do_not_track', __( 'Visits to ignore:', 'wp-google-analytics' ), array( $this, 'field_do_not_track' ), 'wga', 'wga_general' );
add_settings_field( 'custom_vars', __( 'Custom variables:', 'wp-google-analytics' ), array( $this, 'field_custom_variables' ), 'wga', 'wga_general' );
}
/**
* Where the user adds their Google Analytics code
*/
public function field_code() {
// Display the tokens in the right column of the page
echo '
';
echo '' . __( 'Use tokens in your custom variables to make your fields dynamic based on context. Here are some of the tokens you can use:' ) . '';
echo '