Google's Analytics. After enabling this plugin visit the settings page and enter your Google Analytics' UID and enable logging. * Author: Ronald Heft * Author URI: http://ronaldheft.com/ * Text Domain: google-analyticator */ define('GOOGLE_ANALYTICATOR_VERSION', '6.2'); // Constants for enabled/disabled state define("ga_enabled", "enabled", true); define("ga_disabled", "disabled", true); // Defaults, etc. define("key_ga_uid", "ga_uid", true); define("key_ga_status", "ga_status", true); define("key_ga_admin", "ga_admin_status", true); define("key_ga_admin_disable", "ga_admin_disable", true); define("key_ga_admin_role", "ga_admin_role", true); define("key_ga_dashboard_role", "ga_dashboard_role", true); define("key_ga_adsense", "ga_adsense", true); define("key_ga_extra", "ga_extra", true); define("key_ga_extra_after", "ga_extra_after", true); define("key_ga_event", "ga_event", true); define("key_ga_outbound", "ga_outbound", true); define("key_ga_outbound_prefix", "ga_outbound_prefix", true); define("key_ga_downloads", "ga_downloads", true); define("key_ga_downloads_prefix", "ga_downloads_prefix", true); define("key_ga_widgets", "ga_widgets", true); define("key_ga_sitespeed", "ga_sitespeed", true); define("ga_uid_default", "XX-XXXXX-X", true); define("ga_status_default", ga_disabled, true); define("ga_admin_default", ga_enabled, true); define("ga_admin_disable_default", 'remove', true); define("ga_adsense_default", "", true); define("ga_extra_default", "", true); define("ga_extra_after_default", "", true); define("ga_event_default", ga_enabled, true); define("ga_outbound_default", ga_enabled, true); define("ga_outbound_prefix_default", 'outgoing', true); define("ga_downloads_default", "", true); define("ga_downloads_prefix_default", "download", true); define("ga_widgets_default", ga_enabled, true); define("ga_sitespeed_default", ga_enabled, true); // Create the default key and status add_option(key_ga_status, ga_status_default, ''); add_option(key_ga_uid, ga_uid_default, ''); add_option(key_ga_admin, ga_admin_default, ''); add_option(key_ga_admin_disable, ga_admin_disable_default, ''); add_option(key_ga_admin_role, array('administrator'), ''); add_option(key_ga_dashboard_role, array('administrator'), ''); add_option(key_ga_adsense, ga_adsense_default, ''); add_option(key_ga_extra, ga_extra_default, ''); add_option(key_ga_extra_after, ga_extra_after_default, ''); add_option(key_ga_event, ga_event_default, ''); add_option(key_ga_outbound, ga_outbound_default, ''); add_option(key_ga_outbound_prefix, ga_outbound_prefix_default, ''); add_option(key_ga_downloads, ga_downloads_default, ''); add_option(key_ga_downloads_prefix, ga_downloads_prefix_default, ''); add_option('ga_profileid', '', ''); add_option(key_ga_widgets, ga_widgets_default, ''); add_option('ga_google_token', '', ''); add_option('ga_compatibility', 'off', ''); add_option(key_ga_sitespeed, ga_sitespeed_default, ''); # Check if we have a version of WordPress greater than 2.8 if ( function_exists('register_widget') ) { # Check if widgets are enabled if ( get_option(key_ga_widgets) == 'enabled' ) { # Include Google Analytics Stats widget require_once('google-analytics-stats-widget.php'); # Include the Google Analytics Summary widget require_once('google-analytics-summary-widget.php'); $google_analytics_summary = new GoogleAnalyticsSummary(); } } // Create a option page for settings add_action('admin_init', 'ga_admin_init'); add_action('admin_menu', 'add_ga_option_page'); // Initialize the options function ga_admin_init() { # Load the localization information $plugin_dir = basename(dirname(__FILE__)); load_plugin_textdomain('google-analyticator', 'wp-content/plugins/' . $plugin_dir . '/localizations', $plugin_dir . '/localizations'); } # Add the core Google Analytics script, with a high priority to ensure last script for async tracking add_action('wp_head', 'add_google_analytics', 999999); add_action('login_head', 'add_google_analytics', 999999); # Initialize outbound link tracking add_action('init', 'ga_outgoing_links'); // Hook in the options page function function add_ga_option_page() { $plugin_page = add_options_page(__('Google Analyticator Settings', 'google-analyticator'), 'Google Analytics', 'manage_options', basename(__FILE__), 'ga_options_page'); # Include javascript on the GA settings page add_action('admin_head-' . $plugin_page, 'ga_admin_ajax'); } add_action('plugin_action_links_' . plugin_basename(__FILE__), 'ga_filter_plugin_actions'); // Add settings option function ga_filter_plugin_actions($links) { $new_links = array(); $new_links[] = '' . __('Settings', 'google-analyticator') . ''; return array_merge($new_links, $links); } add_filter('plugin_row_meta', 'ga_filter_plugin_links', 10, 2); // Add FAQ and support information function ga_filter_plugin_links($links, $file) { if ( $file == plugin_basename(__FILE__) ) { $links[] = '' . __('FAQ', 'google-analyticator') . ''; $links[] = '' . __('Support', 'google-analyticator') . ''; $links[] = '' . __('Donate', 'google-analyticator') . ''; } return $links; } function ga_options_page() { // If we are a postback, store the options if (isset($_POST['info_update'])) { # Verify nonce check_admin_referer('google-analyticator-update_settings'); // Update the status $ga_status = $_POST[key_ga_status]; if (($ga_status != ga_enabled) && ($ga_status != ga_disabled)) $ga_status = ga_status_default; update_option(key_ga_status, $ga_status); // Update the UID $ga_uid = $_POST[key_ga_uid]; if ($ga_uid == '') $ga_uid = ga_uid_default; update_option(key_ga_uid, $ga_uid); // Update the admin logging $ga_admin = $_POST[key_ga_admin]; if (($ga_admin != ga_enabled) && ($ga_admin != ga_disabled)) $ga_admin = ga_admin_default; update_option(key_ga_admin, $ga_admin); // Update the admin disable setting $ga_admin_disable = $_POST[key_ga_admin_disable]; if ( $ga_admin_disable == '' ) $ga_admin_disable = ga_admin_disable_default; update_option(key_ga_admin_disable, $ga_admin_disable); // Update the admin level if ( array_key_exists(key_ga_admin_role, $_POST) ) { $ga_admin_role = $_POST[key_ga_admin_role]; } else { $ga_admin_role = ""; } update_option(key_ga_admin_role, $ga_admin_role); // Update the dashboard level if ( array_key_exists(key_ga_dashboard_role, $_POST) ) { $ga_dashboard_role = $_POST[key_ga_dashboard_role]; } else { $ga_dashboard_role = ""; } update_option(key_ga_dashboard_role, $ga_dashboard_role); // Update the extra tracking code $ga_extra = $_POST[key_ga_extra]; update_option(key_ga_extra, $ga_extra); // Update the extra after tracking code $ga_extra_after = $_POST[key_ga_extra_after]; update_option(key_ga_extra_after, $ga_extra_after); // Update the adsense key $ga_adsense = $_POST[key_ga_adsense]; update_option(key_ga_adsense, $ga_adsense); // Update the event tracking $ga_event = $_POST[key_ga_event]; if (($ga_event != ga_enabled) && ($ga_event != ga_disabled)) $ga_event = ga_event_default; update_option(key_ga_event, $ga_event); // Update the outbound tracking $ga_outbound = $_POST[key_ga_outbound]; if (($ga_outbound != ga_enabled) && ($ga_outbound != ga_disabled)) $ga_outbound = ga_outbound_default; update_option(key_ga_outbound, $ga_outbound); // Update the outbound prefix $ga_outbound_prefix = $_POST[key_ga_outbound_prefix]; if ($ga_outbound_prefix == '') $ga_outbound_prefix = ga_outbound_prefix_default; update_option(key_ga_outbound_prefix, $ga_outbound_prefix); // Update the download tracking code $ga_downloads = $_POST[key_ga_downloads]; update_option(key_ga_downloads, $ga_downloads); // Update the download prefix $ga_downloads_prefix = $_POST[key_ga_downloads_prefix]; if ($ga_downloads_prefix == '') $ga_downloads_prefix = ga_downloads_prefix_default; update_option(key_ga_downloads_prefix, $ga_downloads_prefix); // Update the profile id update_option('ga_profileid', $_POST['ga_profileid']); // Update the widgets option $ga_widgets = $_POST[key_ga_widgets]; if (($ga_widgets != ga_enabled) && ($ga_widgets != ga_disabled)) $ga_widgets = ga_widgets_default; update_option(key_ga_widgets, $ga_widgets); // Update the compatibility options $ga_compatibility = $_POST['ga_compatibility']; if ( $ga_compatibility == '' ) $ga_compatibility = 'off'; update_option('ga_compatibility', $ga_compatibility); // Update the sitespeed option $ga_sitespeed = $_POST[key_ga_sitespeed]; if (($ga_sitespeed != ga_enabled) && ($ga_sitespeed != ga_disabled)) $ga_sitespeed = ga_widgets_default; update_option(key_ga_sitespeed, $ga_sitespeed); // Give an updated message echo "

" . __('Google Analyticator settings saved.', 'google-analyticator') . "

"; } // Output the options page ?>

Like Google Analyticator? Help support it by donating to the developer. This helps cover the cost of maintaining the plugin and development time toward new features. Every donation, no matter how small, is appreciated.

DISABLED.', 'google-analyticator'); ?>
\n"; echo "\n"; echo "\n"; echo "\n"; ?>

authenticating with Google is optional.', 'google-analyticator'); ?>

\n"; ?>

where can I find my UID?). The UID is needed for Google Analytics to log your website stats.', 'google-analyticator'); ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

get_names(); $selected_roles = get_option(key_ga_admin_role); if ( !is_array($selected_roles) ) $selected_roles = array(); # Loop through the roles foreach ( $roles AS $role => $name ) { echo ' ' . _x($name, 'User role') . '
'; } ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

Google Analytics\' Site Speed tracking report.', 'google-analyticator'); ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

event tracking in Analytics, this is the recommended way to track these types of actions. Only disable this option if you must use the old pageview tracking method.', 'google-analyticator'); ?>

\n"; ?>

mp3,pdf. Outbound link tracking must be enabled for downloads to be tracked.', 'google-analyticator'); ?>

\n"; ?>

\n"; ?>

\n"; ?>

"; echo stripslashes(get_option(key_ga_extra))."\n"; ?>

before the Google Analytics tracker is initialized. Read Google Analytics tracker manual to learn what code goes here and how to use it.', 'google-analyticator'); ?>

"; echo stripslashes(get_option(key_ga_extra_after))."\n"; ?>

after the Google Analytics tracker is initialized. Read Google Analytics tracker manual to learn what code goes here and how to use it.', 'google-analyticator'); ?>

\n"; ?>

get_names(); $selected_roles = get_option(key_ga_dashboard_role); if ( !is_array($selected_roles) ) $selected_roles = array(); # Loop through the roles foreach ( $roles AS $role => $name ) { echo ' ' . _x($name, 'User role') . '
'; } ?>

\n"; echo "\n"; echo "\n"; echo "\n"; ?>

\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; ?>

$sorted_ga_account) { usort($sorted_ga_accounts[$id], 'ga_sort_account_list'); } # Create a select box echo ''; ?>

checkLogin() ) return false; # Get a list of accounts $accounts = $stats->getAnalyticsAccounts(); # Return the account array if there are accounts if ( count($accounts) > 0 ) return $accounts; else return false; } /** * Add http_build_query if it doesn't exist already **/ if ( !function_exists('http_build_query') ) { function http_build_query($params, $key = null) { $ret = array(); foreach( (array) $params as $name => $val ) { $name = urlencode($name); if ( $key !== null ) $name = $key . "[" . $name . "]"; if ( is_array($val) || is_object($val) ) $ret[] = http_build_query($val, $name); elseif ($val !== null) $ret[] = $name . "=" . urlencode($val); } return implode("&", $ret); } } /** * Echos out the core Analytics tracking code **/ function add_google_analytics() { # Fetch variables used in the tracking code $uid = stripslashes(get_option(key_ga_uid)); $extra = stripslashes(get_option(key_ga_extra)); $extra_after = stripslashes(get_option(key_ga_extra_after)); $extensions = str_replace (",", "|", get_option(key_ga_downloads)); # Determine if the GA is enabled and contains a valid UID if ( ( get_option(key_ga_status) != ga_disabled ) && ( $uid != "XX-XXXXX-X" ) ) { # Determine if the user is an admin, and should see the tracking code if ( ( get_option(key_ga_admin) == ga_enabled || !ga_current_user_is(get_option(key_ga_admin_role)) ) && get_option(key_ga_admin_disable) == 'remove' || get_option(key_ga_admin_disable) != 'remove' ) { # Disable the tracking code on the post preview page if ( !function_exists("is_preview") || ( function_exists("is_preview") && !is_preview() ) ) { # Add the notice that Google Analyticator tracking is enabled echo "\n"; # Add the Adsense data if specified if ( get_option(key_ga_adsense) != '' ) echo '\n"; # Include the file types to track $extensions = explode(',', stripslashes(get_option(key_ga_downloads))); $ext = ""; foreach ( $extensions AS $extension ) $ext .= "'$extension',"; $ext = substr($ext, 0, -1); # Include the link tracking prefixes $outbound_prefix = stripslashes(get_option(key_ga_outbound_prefix)); $downloads_prefix = stripslashes(get_option(key_ga_downloads_prefix)); $event_tracking = get_option(key_ga_event); ?> \n"; echo " \n"; } } } /** * Adds outbound link tracking to Google Analyticator **/ function ga_outgoing_links() { # Fetch the UID $uid = stripslashes(get_option(key_ga_uid)); # If GA is enabled and has a valid key if ( (get_option(key_ga_status) != ga_disabled ) && ( $uid != "XX-XXXXX-X" ) ) { # If outbound tracking is enabled if ( get_option(key_ga_outbound) == ga_enabled ) { # If this is not an admin page if ( !is_admin() ) { # Display page tracking if user is not an admin if ( ( get_option(key_ga_admin) == ga_enabled || !ga_current_user_is(get_option(key_ga_admin_role)) ) && get_option(key_ga_admin_disable) == 'remove' || get_option(key_ga_admin_disable) != 'remove' ) { add_action('wp_print_scripts', 'ga_external_tracking_js'); } } } } } /** * Adds the scripts required for outbound link tracking **/ function ga_external_tracking_js() { wp_enqueue_script('ga-external-tracking', plugins_url('/google-analyticator/external-tracking.min.js'), array('jquery'), GOOGLE_ANALYTICATOR_VERSION); } /** * Determines if a specific user fits a role **/ function ga_current_user_is($roles) { if ( !$roles ) return false; global $current_user; get_currentuserinfo(); $user_id = intval( $current_user->ID ); if ( !$user_id ) { return false; } $user = new WP_User($user_id); // $user->roles foreach ( $roles as $role ) if ( in_array($role, $user->roles) ) return true; return false; } /** * EXPERIMENTAL: Retrieve Google's visits for the given page * More work needs to be done. Needs caching, needs to be less resource intensive, and * needs an automated way to determine the page. * Function may/will change in future releases. Only use if you know what you're doing. * * @param url - the page url, missing the domain information * @param days - the number of days to get * @return the number of visits **/ function get_analytics_visits_by_page($page, $days = 31) { require_once('class.analytics.stats.php'); # Create a new API object $api = new GoogleAnalyticsStats(); # Get the current accounts accounts $accounts = ga_get_analytics_accounts(); # Verify accounts exist if ( count($accounts) <= 0 ) return 0; # Loop throught the account and return the current account foreach ( $accounts AS $account ) { # Check if the UID matches the selected UID if ( $account['ga:webPropertyId'] == get_option('ga_uid') ) { $api->setAccount($account['id']); break; } } # Encode the page url $page = urlencode($page); # Get the metric information from Google $before = date('Y-m-d', strtotime('-' . $days . ' days')); $yesterday = date('Y-m-d', strtotime('-1 day')); $stats = $api->getMetrics('ga:visits', $before, $yesterday, 'ga:pagePath', false, 'ga:pagePath%3D%3D' . $page, 1); # Check the size of the stats array if ( count($stats) <= 0 || !is_array($stats) ) { return 0; } else { # Loop through each stat for display foreach ( $stats AS $stat ) { return $stat['ga:visits']; } } } ?>