.
*/
// This plugin was originally based on Rich Boakes' Analytics plugin: http://boakes.org/analytics
define( 'GAWP_VERSION', '4.2.8' );
/*
* Admin User Interface
*/
if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) && !class_exists( 'GA_Admin' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'yst_plugin_tools.php';
require_once plugin_dir_path( __FILE__ ) . '/wp-gdata/wp-gdata.php';
$options = get_option( 'Yoast_Google_Analytics' );
global $wp_version;
if ( version_compare( $wp_version, '3.3', '>=' ) && !isset( $options['tracking_popup'] ) )
require_once plugin_dir_path( __FILE__ ) . 'class-pointer.php';
if ( isset( $options['yoast_tracking'] ) && ( 'on' == $options['yoast_tracking'] || true === $options['yoast_tracking'] ) )
require_once plugin_dir_path( __FILE__ ) . 'class-tracking.php';
class GA_Admin extends Yoast_GA_Plugin_Admin {
var $hook = 'google-analytics-for-wordpress';
var $filename = 'google-analytics-for-wordpress/googleanalytics.php';
var $longname = 'Google Analytics Configuration';
var $shortname = 'Google Analytics';
var $ozhicon = 'images/chart_curve.png';
var $optionname = 'Yoast_Google_Analytics';
var $homepage = 'http://yoast.com/wordpress/google-analytics/';
var $toc = '';
/**
* PHP4 Constructor
*/
function GA_Admin() {
$this->__construct();
}
/**
* Constructur, load all required stuff.
*/
function __construct() {
$this->upgrade();
$this->plugin_url = plugins_url( '', __FILE__ ) . '/';
// Register the settings page
add_action( 'admin_menu', array( &$this, 'register_settings_page' ) );
// Register the contextual help for the settings page
// add_action( 'contextual_help', array(&$this, 'plugin_help'), 10, 3 );
// Give the settings page a nice icon in Ozh's menu
add_filter( 'ozh_adminmenu_icon', array( &$this, 'add_ozh_adminmenu_icon' ) );
// Give the plugin a settings link in the plugin overview
add_filter( 'plugin_action_links', array( &$this, 'add_action_link' ), 10, 2 );
// Print Scripts and Styles
add_action( 'admin_print_scripts', array( &$this, 'config_page_scripts' ) );
add_action( 'admin_print_styles', array( &$this, 'config_page_styles' ) );
// Print stuff in the settings page's head
add_action( 'admin_head', array( &$this, 'config_page_head' ) );
// Drop a warning on each page of the admin when Google Analytics hasn't been configured
add_action( 'admin_footer', array( &$this, 'warning' ) );
// Save settings
// TODO: replace with Options API
add_action( 'admin_init', array( &$this, 'save_settings' ) );
// Authenticate
add_action( 'admin_init', array( &$this, 'authenticate' ) );
}
function config_page_head() {
if ( isset( $_GET['allow_tracking'] ) ) {
$options = get_option( 'Yoast_Google_Analytics' );
$options['tracking_popup'] = 'done';
if ( $_GET['allow_tracking'] == 'yes' )
$options['yoast_tracking'] = true;
else
$options['yoast_tracking'] = false;
update_option( 'Yoast_Google_Analytics', $options );
}
global $current_screen;
if ( 'settings_page_' . $this->hook == $current_screen->id ) {
$options = get_option( $this->optionname );
if ( !empty( $options['uastring'] ) ) {
$uastring = $options['uastring'];
} else {
$uastring = '';
}
?>
hook ) {
$contextual_help = '
' . __( 'Having problems?' ) . ' ' .
'' . sprintf( __( "If you're having problems with this plugin, please refer to its FAQ page ." ), 'http://yoast.com/wordpress/google-analytics/ga-wp-faq/' ) . '
';
}
return $contextual_help;
}
function toc( $modules ) {
$output = '';
foreach ( $modules as $module => $key ) {
$output .= '' . $module . ' ';
}
$output .= ' ';
return $output;
}
function save_settings() {
$options = get_option( $this->optionname );
if ( isset( $_REQUEST['reset'] ) && $_REQUEST['reset'] == "true" && isset( $_REQUEST['plugin'] ) && $_REQUEST['plugin'] == 'google-analytics-for-wordpress' ) {
$options = $this->set_defaults();
$options['msg'] = "" . __( 'Google Analytics settings reset.' ) . "
\n";
} elseif ( isset( $_POST['submit'] ) && isset( $_POST['plugin'] ) && $_POST['plugin'] == 'google-analytics-for-wordpress' ) {
if ( !current_user_can( 'manage_options' ) ) die( __( 'You cannot edit the Google Analytics for WordPress options.' ) );
check_admin_referer( 'analyticspp-config' );
foreach ( array( 'uastring', 'dlextensions', 'domainorurl', 'position', 'domain', 'customcode', 'ga_token', 'extraseurl', 'gajsurl', 'gfsubmiteventpv', 'trackprefix', 'ignore_userlevel', 'internallink', 'internallinklabel', 'primarycrossdomain', 'othercrossdomains' ) as $option_name ) {
if ( isset( $_POST[$option_name] ) )
$options[$option_name] = $_POST[$option_name];
else
$options[$option_name] = '';
}
foreach ( array( 'extrase', 'trackoutbound', 'admintracking', 'trackadsense', 'allowanchor', 'allowlinker', 'allowhash', 'rsslinktagging', 'advancedsettings', 'trackregistration', 'theme_updated', 'cv_loggedin', 'cv_authorname', 'cv_category', 'cv_all_categories', 'cv_tags', 'cv_year', 'cv_post_type', 'outboundpageview', 'downloadspageview', 'trackcrossdomain', 'gajslocalhosting', 'manual_uastring', 'taggfsubmit', 'wpec_tracking', 'shopp_tracking', 'anonymizeip', 'trackcommentform', 'debug', 'firebuglite', 'yoast_tracking' ) as $option_name ) {
if ( isset( $_POST[$option_name] ) && $_POST[$option_name] == 'on' )
$options[$option_name] = true;
else
$options[$option_name] = false;
}
if ( isset( $_POST['manual_uastring'] ) && isset( $_POST['uastring_man'] ) ) {
$options['uastring'] = $_POST['uastring_man'];
}
if ( $options['trackcrossdomain'] ) {
if ( !$options['allowlinker'] )
$options['allowlinker'] = true;
if ( empty( $options['primarycrossdomain'] ) ) {
$origin = GA_Filter::ga_get_domain( $_SERVER["HTTP_HOST"] );
$options['primarycrossdomain'] = $origin["domain"];
}
}
if ( function_exists( 'w3tc_pgcache_flush' ) )
w3tc_pgcache_flush();
if ( function_exists( 'w3tc_dbcache_flush' ) )
w3tc_dbcache_flush();
if ( function_exists( 'w3tc_minify_flush' ) )
w3tc_minify_flush();
if ( function_exists( 'w3tc_objectcache_flush' ) )
w3tc_objectcache_flush();
if ( function_exists( 'wp_cache_clear_cache' ) )
wp_cache_clear_cache();
$options['msg'] = "Google Analytics settings updated .
\n";
$options['msg'] .= "";
}
update_option( $this->optionname, $options );
}
function save_button() {
return '
';
}
function upgrade() {
$options = get_option( $this->optionname );
if ( isset( $options['version'] ) && $options['version'] < '4.04' ) {
if ( !isset( $options['ignore_userlevel'] ) || $options['ignore_userlevel'] == '' )
$options['ignore_userlevel'] = 11;
}
if ( !isset( $options['version'] ) || $options['version'] != GAWP_VERSION ) {
$options['version'] = GAWP_VERSION;
}
update_option( $this->optionname, $options );
}
function config_page() {
$options = get_option( $this->optionname );
if ( isset( $options['msg'] ) )
echo $options['msg'];
$options['msg'] = '';
update_option( $this->optionname, $options );
if ( !isset( $options['uastring'] ) )
$options = $this->set_defaults();
$modules = array();
if ( !isset( $options['manual_uastring'] ) )
$options['manual_uastring'] = '';
?>
false,
'allowanchor' => false,
'allowhash' => false,
'allowlinker' => false,
'anonymizeip' => false,
'customcode' => '',
'cv_loggedin' => false,
'cv_authorname' => false,
'cv_category' => false,
'cv_all_categories' => false,
'cv_tags' => false,
'cv_year' => false,
'cv_post_type' => false,
'debug' => false,
'dlextensions' => 'doc,exe,js,pdf,ppt,tgz,zip,xls',
'domain' => '',
'domainorurl' => 'domain',
'extrase' => false,
'extraseurl' => '',
'firebuglite' => false,
'ga_token' => '',
'ga_api_responses' => array(),
'gajslocalhosting' => false,
'gajsurl' => '',
'ignore_userlevel' => '11',
'internallink' => false,
'internallinklabel' => '',
'outboundpageview' => false,
'downloadspageview' => false,
'othercrossdomains' => '',
'position' => 'footer',
'primarycrossdomain' => '',
'theme_updated' => false,
'trackcommentform' => true,
'trackcrossdomain' => false,
'trackadsense' => false,
'trackoutbound' => true,
'trackregistration' => false,
'rsslinktagging' => true,
'uastring' => '',
'version' => GAWP_VERSION,
);
update_option( $this->optionname, $options );
return $options;
}
function warning() {
$options = get_option( $this->optionname );
if ( !isset( $options['uastring'] ) || empty( $options['uastring'] ) ) {
echo "";
}
} // end warning()
function authenticate() {
if ( isset( $_REQUEST['ga_oauth_callback'] ) ) {
$o = get_option( $this->optionname );
if ( isset( $o['gawp_oauth']['oauth_token'] ) && $o['gawp_oauth']['oauth_token'] == $_REQUEST['oauth_token'] ) {
$gdata = new WP_GData(
array(
'scope' => 'https://www.google.com/analytics/feeds/',
'xoauth_displayname' => 'Google Analytics for WordPress by Yoast'
),
$o['gawp_oauth']['oauth_token'],
$o['gawp_oauth']['oauth_token_secret']
);
$o['gawp_oauth']['access_token'] = $gdata->get_access_token( $_REQUEST['oauth_verifier'] );
unset( $o['gawp_oauth']['oauth_token'] );
unset( $o['gawp_oauth']['oauth_token_secret'] );
$o['ga_token'] = $o['gawp_oauth']['access_token']['oauth_token'];
}
update_option( $this->optionname, $o );
wp_redirect( menu_page_url( $this->hook, false ) );
exit;
}
if ( !empty( $_GET['reauth'] ) ) {
$gdata = new WP_GData(
array(
'scope' => 'https://www.google.com/analytics/feeds/',
'xoauth_displayname' => 'Google Analytics for WordPress by Yoast'
)
);
$oauth_callback = add_query_arg( array( 'ga_oauth_callback' => 1 ), menu_page_url( 'google-analytics-for-wordpress', false ) );
$request_token = $gdata->get_request_token( $oauth_callback );
$options = get_option( $this->optionname );
unset( $options['ga_token'] );
unset( $options['gawp_oauth']['access_token'] );
$options['gawp_oauth']['oauth_token'] = $request_token['oauth_token'];
$options['gawp_oauth']['oauth_token_secret'] = $request_token['oauth_token_secret'];
update_option( $this->optionname, $options );
wp_redirect( $gdata->get_authorize_url( $request_token ) );
exit;
}
} //end reauthenticate()
} // end class GA_Admin
$ga_admin = new GA_Admin();
} //endif
/**
* Code that actually inserts stuff into pages.
*/
if ( !class_exists( 'GA_Filter' ) ) {
class GA_Filter {
/**
* Cleans the variable to make it ready for storing in Google Analytics
*/
function ga_str_clean( $val ) {
return remove_accents( str_replace( '---', '-', str_replace( ' ', '-', strtolower( html_entity_decode( $val ) ) ) ) );
}
/*
* Insert the tracking code into the page
*/
function spool_analytics() {
global $wp_query;
// echo '';
$options = get_option( 'Yoast_Google_Analytics' );
if ( !isset( $options['uastring'] ) || $options['uastring'] == '' ) {
if ( current_user_can( 'manage_options' ) )
echo "\n";
return;
}
/**
* The order of custom variables is very, very important: custom vars should always take up the same slot to make analysis easy.
*/
$customvarslot = 1;
if ( yoast_ga_do_tracking() && !is_preview() ) {
$push = array();
if ( $options['allowanchor'] )
$push[] = "'_setAllowAnchor',true";
if ( $options['allowlinker'] )
$push[] = "'_setAllowLinker',true";
if ( $options['anonymizeip'] )
$push[] = "'_gat._anonymizeIp'";
if ( isset( $options['domain'] ) && $options['domain'] != "" )
$push[] = "'_setDomainName','" . $options['domain'] . "'";
if ( isset( $options['trackcrossdomain'] ) && $options['trackcrossdomain'] )
$push[] = "'_setDomainName','" . $options['primarycrossdomain'] . "'";
if ( isset( $options['allowhash'] ) && $options['allowhash'] )
$push[] = "'_setAllowHash',false";
if ( $options['cv_loggedin'] ) {
$current_user = wp_get_current_user();
if ( $current_user && $current_user->ID != 0 )
$push[] = "'_setCustomVar',$customvarslot,'logged-in','" . $current_user->roles[0] . "',1";
// Customvar slot needs to be upped even when the user is not logged in, to make sure the variables below are always in the same slot.
$customvarslot++;
}
if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
if ( $options['cv_post_type'] ) {
$post_type = get_post_type();
if ( $post_type ) {
$push[] = "'_setCustomVar'," . $customvarslot . ",'post_type','" . $post_type . "',3";
$customvarslot++;
}
}
} else if ( is_singular() && !is_home() ) {
if ( $options['cv_post_type'] ) {
$post_type = get_post_type();
if ( $post_type ) {
$push[] = "'_setCustomVar'," . $customvarslot . ",'post_type','" . $post_type . "',3";
$customvarslot++;
}
}
if ( $options['cv_authorname'] ) {
$push[] = "'_setCustomVar',$customvarslot,'author','" . GA_Filter::ga_str_clean( get_the_author_meta( 'display_name', $wp_query->post->post_author ) ) . "',3";
$customvarslot++;
}
if ( $options['cv_tags'] ) {
$i = 0;
if ( get_the_tags() ) {
$tagsstr = '';
foreach ( get_the_tags() as $tag ) {
if ( $i > 0 )
$tagsstr .= ' ';
$tagsstr .= $tag->slug;
$i++;
}
// Max 64 chars for value and label combined, hence 64 - 4
$tagsstr = substr( $tagsstr, 0, 60 );
$push[] = "'_setCustomVar',$customvarslot,'tags','" . $tagsstr . "',3";
}
$customvarslot++;
}
if ( is_singular() ) {
if ( $options['cv_year'] ) {
$push[] = "'_setCustomVar',$customvarslot,'year','" . get_the_time( 'Y' ) . "',3";
$customvarslot++;
}
if ( $options['cv_category'] && is_single() ) {
$cats = get_the_category();
if ( is_array( $cats ) && isset( $cats[0] ) )
$push[] = "'_setCustomVar',$customvarslot,'category','" . $cats[0]->slug . "',3";
$customvarslot++;
}
if ( $options['cv_all_categories'] && is_single() ) {
$i = 0;
$catsstr = '';
foreach ( (array) get_the_category() as $cat ) {
if ( $i > 0 )
$catsstr .= ' ';
$catsstr .= $cat->slug;
$i++;
}
// Max 64 chars for value and label combined, hence 64 - 10
$catsstr = substr( $catsstr, 0, 54 );
$push[] = "'_setCustomVar',$customvarslot,'categories','" . $catsstr . "',3";
$customvarslot++;
}
}
}
$push = apply_filters( 'yoast-ga-custom-vars', $push, $customvarslot );
$push = apply_filters( 'yoast-ga-push-before-pageview', $push );
if ( is_404() ) {
$push[] = "'_trackPageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer";
} else if ( $wp_query->is_search ) {
$pushstr = "'_trackPageview','" . get_bloginfo( 'url' ) . "/?s=";
if ( $wp_query->found_posts == 0 ) {
$push[] = $pushstr . "no-results:" . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'";
} else if ( $wp_query->found_posts == 1 ) {
$push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'";
} else if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) {
$push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'";
} else {
$push[] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'";
}
} else {
$push[] = "'_trackPageview'";
}
$push = apply_filters( 'yoast-ga-push-after-pageview', $push );
if ( defined( 'WPSC_VERSION' ) && $options['wpec_tracking'] )
$push = GA_Filter::wpec_transaction_tracking( $push );
if ( $options['shopp_tracking'] ) {
global $Shopp;
if ( isset( $Shopp ) )
$push = GA_Filter::shopp_transaction_tracking( $push );
}
$pushstr = "";
foreach ( $push as $key ) {
if ( !empty( $pushstr ) )
$pushstr .= ",";
$pushstr .= "[" . $key . "]";
}
if ( current_user_can( 'manage_options' ) && $options['firebuglite'] && $options['debug'] )
echo '';
?>
' . "\n" . '
\n";
}
}
/*
* Insert the AdSense parameter code into the page. This'll go into the header per Google's instructions.
*/
function spool_adsense() {
$options = get_option( 'Yoast_Google_Analytics' );
if ( $options["uastring"] != "" && yoast_ga_do_tracking() && !is_preview() ) {
echo '' . "\n";
}
}
function ga_get_tracking_prefix() {
$options = get_option( 'Yoast_Google_Analytics' );
return ( empty( $options['trackprefix'] ) ) ? '/yoast-ga/' : $options['trackprefix'];
}
function ga_get_tracking_link( $prefix, $target, $jsprefix = 'javascript:' ) {
$options = get_option( 'Yoast_Google_Analytics' );
if (
( $prefix != 'download' && $options['outboundpageview'] ) ||
( $prefix == 'download' && $options['downloadspageview'] )
) {
$prefix = GA_Filter::ga_get_tracking_prefix() . $prefix;
$pushstr = "['_trackPageview','" . $prefix . "/" . esc_js( esc_url( $target ) ) . "']";
} else {
$pushstr = "['_trackEvent','" . $prefix . "','" . esc_js( esc_url( $target ) ) . "']";
}
return $jsprefix . "_gaq.push(" . $pushstr . ");";
}
function ga_get_domain( $uri ) {
$hostPattern = "/^(http:\/\/)?([^\/]+)/i";
$domainPatternUS = "/[^\.\/]+\.[^\.\/]+$/";
$domainPatternUK = "/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/";
preg_match( $hostPattern, $uri, $matches );
$host = $matches[2];
if ( preg_match( "/.*\..*\..*\..*$/", $host ) )
preg_match( $domainPatternUK, $host, $matches );
else
preg_match( $domainPatternUS, $host, $matches );
if ( isset($matches[0]) ) {
return array( "domain"=> $matches[0], "host"=> $host );
} else {
return false;
}
}
function ga_parse_link( $category, $matches ) {
$origin = GA_Filter::ga_get_domain( $_SERVER["HTTP_HOST"] );
$options = get_option( 'Yoast_Google_Analytics' );
// Break out immediately if the link is not an http or https link.
if ( strpos( $matches[2], "http" ) !== 0 ) {
$target = false;
} else if ( ( strpos( $matches[2], "mailto" ) === 0 ) ) {
$target = 'email';
} else {
$target = GA_Filter::ga_get_domain( $matches[3] );
}
$trackBit = "";
$extension = substr( strrchr( $matches[3], '.' ), 1 );
$dlextensions = explode( ",", str_replace( '.', '', $options['dlextensions'] ) );
if ( $target ) {
if ( $target == 'email' ) {
$trackBit = GA_Filter::ga_get_tracking_link( 'mailto', str_replace( 'mailto:', '', $matches[3] ), '' );
} else if ( in_array( $extension, $dlextensions ) ) {
$trackBit = GA_Filter::ga_get_tracking_link( 'download', $matches[3], '' );
} else if ( $target["domain"] != $origin["domain"] ) {
$crossdomains = array();
if ( isset( $options['othercrossdomains'] ) )
$crossdomains = explode( ',', str_replace( ' ', '', $options['othercrossdomains'] ) );
if ( isset( $options['trackcrossdomain'] ) && $options['trackcrossdomain'] && in_array( $target["host"], $crossdomains ) ) {
$trackBit = '_gaq.push([\'_link\', \'' . $matches[2] . '//' . $matches[3] . '\']); return false;"';
} else if ( $options['trackoutbound'] && in_array( $options['domainorurl'], array( 'domain', 'url' ) ) ) {
$url = $options['domainorurl'] == 'domain' ? $target["host"] : $matches[3];
$trackBit = GA_Filter::ga_get_tracking_link( $category, $url, '' );
}
$trackBit = GA_Filter::ga_get_tracking_link( $category, $url, '' );
} else if ( $target["domain"] == $origin["domain"] && isset( $options['internallink'] ) && $options['internallink'] != '' ) {
$url = preg_replace( '|' . $origin["host"] . '|', '', $matches[3] );
$extintlinks = explode( ',', $options['internallink'] );
foreach ( $extintlinks as $link ) {
if ( preg_match( '|^' . trim( $link ) . '|', $url, $match ) ) {
$label = $options['internallinklabel'];
if ( $label == '' )
$label = 'int';
$trackBit = GA_Filter::ga_get_tracking_link( $category . '-' . $label, $url, '' );
}
}
}
}
if ( $trackBit != "" ) {
if ( preg_match( '/onclick=[\'\"](.*?)[\'\"]/i', $matches[4] ) > 0 ) {
// Check for manually tagged outbound clicks, and replace them with the tracking of choice.
if ( preg_match( '/.*_track(Pageview|Event).*/i', $matches[4] ) > 0 ) {
$matches[4] = preg_replace( '/onclick=[\'\"](javascript:)?(.*;)?[a-zA-Z0-9]+\._track(Pageview|Event)\([^\)]+\)(;)?(.*)?[\'\"]/i', 'onclick="javascript:' . $trackBit . '$2$5"', $matches[4] );
} else {
$matches[4] = preg_replace( '/onclick=[\'\"](javascript:)?(.*?)[\'\"]/i', 'onclick="javascript:' . $trackBit . '$2"', $matches[4] );
}
} else {
$matches[4] = 'onclick="javascript:' . $trackBit . '"' . $matches[4];
}
}
return '' . $matches[5] . ' ';
}
function ga_parse_article_link( $matches ) {
return GA_Filter::ga_parse_link( 'outbound-article', $matches );
}
function ga_parse_comment_link( $matches ) {
return GA_Filter::ga_parse_link( 'outbound-comment', $matches );
}
function ga_parse_widget_link( $matches ) {
return GA_Filter::ga_parse_link( 'outbound-widget', $matches );
}
function ga_parse_nav_menu( $matches ) {
return GA_Filter::ga_parse_link( 'outbound-menu', $matches );
}
function widget_content( $text ) {
if ( !yoast_ga_do_tracking() )
return $text;
static $anchorPattern = '/(.*?)<\/a>/i';
$text = preg_replace_callback( $anchorPattern, array( 'GA_Filter', 'ga_parse_widget_link' ), $text );
return $text;
}
function the_content( $text ) {
if ( !yoast_ga_do_tracking() )
return $text;
if ( !is_feed() ) {
static $anchorPattern = '/ (.*?)<\/a>/i';
$text = preg_replace_callback( $anchorPattern, array( 'GA_Filter', 'ga_parse_article_link' ), $text );
}
return $text;
}
function nav_menu( $text ) {
if ( !yoast_ga_do_tracking() )
return $text;
if ( !is_feed() ) {
static $anchorPattern = '/ (.*?)<\/a>/i';
$text = preg_replace_callback( $anchorPattern, array( 'GA_Filter', 'ga_parse_nav_menu' ), $text );
}
return $text;
}
function comment_text( $text ) {
if ( !yoast_ga_do_tracking() )
return $text;
if ( !is_feed() ) {
static $anchorPattern = '/ (.*?)<\/a>/i';
$text = preg_replace_callback( $anchorPattern, array( 'GA_Filter', 'ga_parse_comment_link' ), $text );
}
return $text;
}
function comment_author_link( $text ) {
$options = get_option( 'Yoast_Google_Analytics' );
if ( !yoast_ga_do_tracking() )
return $text;
static $anchorPattern = '/(.*\s+.*?href\s*=\s*)["\'](.*?)["\'](.*)/';
preg_match( $anchorPattern, $text, $matches );
if ( !isset( $matches[2] ) || $matches[2] == "" ) return $text;
$trackBit = '';
$target = GA_Filter::ga_get_domain( $matches[2] );
$origin = GA_Filter::ga_get_domain( $_SERVER["HTTP_HOST"] );
if ( $target["domain"] != $origin["domain"] ) {
if ( isset( $options['domainorurl'] ) && $options['domainorurl'] == "domain" )
$url = $target["host"];
else
$url = $matches[2];
$trackBit = 'onclick="' . GA_Filter::ga_get_tracking_link( 'outbound-commentauthor', $url ) . '"';
}
return $matches[1] . "\"" . $matches[2] . "\" " . $trackBit . " " . $matches[3];
}
function bookmarks( $bookmarks ) {
if ( !yoast_ga_do_tracking() )
return $bookmarks;
$i = 0;
while ( $i < count( $bookmarks ) ) {
$target = GA_Filter::ga_get_domain( $bookmarks[$i]->link_url );
$sitedomain = GA_Filter::ga_get_domain( get_bloginfo( 'url' ) );
if ( $target['host'] == $sitedomain['host'] ) {
$i++;
continue;
}
if ( isset( $options['domainorurl'] ) && $options['domainorurl'] == "domain" )
$url = $target["host"];
else
$url = $bookmarks[$i]->link_url;
$trackBit = '" onclick="' . GA_Filter::ga_get_tracking_link( 'outbound-blogroll', $url );
$bookmarks[$i]->link_target .= $trackBit;
$i++;
}
return $bookmarks;
}
function rsslinktagger( $guid ) {
$options = get_option( 'Yoast_Google_Analytics' );
global $wp, $post;
if ( is_feed() ) {
if ( $options['allowanchor'] ) {
$delimiter = '#';
} else {
$delimiter = '?';
if ( strpos( $guid, $delimiter ) > 0 )
$delimiter = '&';
}
return $guid . $delimiter . 'utm_source=rss&utm_medium=rss&utm_campaign=' . urlencode( $post->post_name );
}
return $guid;
}
function wpec_transaction_tracking( $push ) {
global $wpdb, $purchlogs, $cart_log_id;
if ( !isset( $cart_log_id ) || empty( $cart_log_id ) )
return $push;
$city = $wpdb->get_var( "SELECT tf.value
FROM " . WPSC_TABLE_SUBMITED_FORM_DATA . " tf
LEFT JOIN " . WPSC_TABLE_CHECKOUT_FORMS . " cf
ON cf.id = tf.form_id
WHERE cf.type = 'city'
AND log_id = " . $cart_log_id );
$country = $wpdb->get_var( "SELECT tf.value
FROM " . WPSC_TABLE_SUBMITED_FORM_DATA . " tf
LEFT JOIN " . WPSC_TABLE_CHECKOUT_FORMS . " cf
ON cf.id = tf.form_id
WHERE cf.type = 'country'
AND log_id = " . $cart_log_id );
$cart_items = $wpdb->get_results( "SELECT * FROM " . WPSC_TABLE_CART_CONTENTS . " WHERE purchaseid = " . $cart_log_id, ARRAY_A );
$total_shipping = $purchlogs->allpurchaselogs[0]->base_shipping;
$total_tax = 0;
foreach ( $cart_items as $item ) {
$total_shipping += $item['pnp'];
$total_tax += $item['tax_charged'];
}
$push[] = "'_addTrans','" . $cart_log_id . "'," // Order ID
. "'" . GA_Filter::ga_str_clean( get_bloginfo( 'name' ) ) . "'," // Store name
. "'" . nzshpcrt_currency_display( $purchlogs->allpurchaselogs[0]->totalprice, 1, true, false, true ) . "'," // Total price
. "'" . nzshpcrt_currency_display( $total_tax, 1, true, false, true ) . "'," // Tax
. "'" . nzshpcrt_currency_display( $total_shipping, 1, true, false, true ) . "'," // Shipping
. "'" . $city . "'," // City
. "''," // State
. "'" . $country . "'"; // Country
foreach ( $cart_items as $item ) {
$item['sku'] = $wpdb->get_var( "SELECT meta_value FROM " . WPSC_TABLE_PRODUCTMETA . " WHERE meta_key = 'sku' AND product_id = '" . $item['prodid'] . "' LIMIT 1" );
$item['category'] = $wpdb->get_var( "SELECT pc.name FROM " . WPSC_TABLE_PRODUCT_CATEGORIES . " pc LEFT JOIN " . WPSC_TABLE_ITEM_CATEGORY_ASSOC . " ca ON pc.id = ca.category_id WHERE pc.group_id = '1' AND ca.product_id = '" . $item['prodid'] . "'" );
$push[] = "'_addItem',"
. "'" . $cart_log_id . "'," // Order ID
. "'" . $item['sku'] . "'," // Item SKU
. "'" . str_replace( "'", "", $item['name'] ) . "'," // Item Name
. "'" . $item['category'] . "'," // Item Category
. "'" . $item['price'] . "'," // Item Price
. "'" . $item['quantity'] . "'"; // Item Quantity
}
$push[] = "'_trackTrans'";
return $push;
}
function shopp_transaction_tracking( $push ) {
global $Shopp;
// Only process if we're in the checkout process (receipt page)
if ( version_compare( substr( SHOPP_VERSION, 0, 3 ), '1.1' ) >= 0 ) {
// Only process if we're in the checkout process (receipt page)
if ( function_exists( 'is_shopp_page' ) && !is_shopp_page( 'checkout' ) ) return $push;
if ( empty( $Shopp->Order->purchase ) ) return $push;
$Purchase = new Purchase( $Shopp->Order->purchase );
$Purchase->load_purchased();
} else {
// For 1.0.x
// Only process if we're in the checkout process (receipt page)
if ( function_exists( 'is_shopp_page' ) && !is_shopp_page( 'checkout' ) ) return $push;
// Only process if we have valid order data
if ( !isset( $Shopp->Cart->data->Purchase ) ) return $push;
if ( empty( $Shopp->Cart->data->Purchase->id ) ) return $push;
$Purchase = $Shopp->Cart->data->Purchase;
}
$push[] = "'_addTrans',"
. "'" . $Purchase->id . "'," // Order ID
. "'" . GA_Filter::ga_str_clean( get_bloginfo( 'name' ) ) . "'," // Store
. "'" . number_format( $Purchase->total, 2 ) . "'," // Total price
. "'" . number_format( $Purchase->tax, 2 ) . "'," // Tax
. "'" . number_format( $Purchase->shipping, 2 ) . "'," // Shipping
. "'" . $Purchase->city . "'," // City
. "'" . $Purchase->state . "'," // State
. "'.$Purchase->country.'"; // Country
foreach ( $Purchase->purchased as $item ) {
$sku = empty( $item->sku ) ? 'PID-' . $item->product . str_pad( $item->price, 4, '0', STR_PAD_LEFT ) : $item->sku;
$push[] = "'_addItem',"
. "'" . $Purchase->id . "',"
. "'" . $sku . "',"
. "'" . str_replace( "'", "", $item->name ) . "',"
. "'" . $item->optionlabel . "',"
. "'" . number_format( $item->unitprice, 2 ) . "',"
. "'" . $item->quantity . "'";
}
$push[] = "'_trackTrans'";
return $push;
}
} // class GA_Filter
} // endif
/**
* If setAllowAnchor is set to true, GA ignores all links tagged "normally", so we redirect all "normally" tagged URL's
* to one tagged with a hash.
*/
function ga_utm_hashtag_redirect() {
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
if ( strpos( $_SERVER['REQUEST_URI'], "utm_" ) !== false ) {
$url = 'http://';
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != "" ) {
$url = 'https://';
}
$url .= $_SERVER['SERVER_NAME'];
if ( strpos( $_SERVER['REQUEST_URI'], "?utm_" ) !== false ) {
$url .= str_replace( "?utm_", "#utm_", $_SERVER['REQUEST_URI'] );
} else if ( strpos( $_SERVER['REQUEST_URI'], "&utm_" ) !== false ) {
$url .= substr_replace( $_SERVER['REQUEST_URI'], "#utm_", strpos( $_SERVER['REQUEST_URI'], "&utm_" ), 5 );
}
wp_redirect( $url, 301 );
exit;
}
}
}
function yoast_ga_do_tracking() {
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID )
return true;
$yoast_ga_options = get_option( 'Yoast_Google_Analytics' );
if ( ( $current_user->user_level >= $yoast_ga_options["ignore_userlevel"] ) )
return false;
else
return true;
}
function track_comment_form_head() {
if ( is_singular() ) {
global $post;
$yoast_ga_options = get_option( 'Yoast_Google_Analytics' );
if ( yoast_ga_do_tracking()
&& isset( $yoast_ga_options["trackcommentform"] )
&& $yoast_ga_options["trackcommentform"]
&& ( 'open' == $post->comment_status )
)
wp_enqueue_script( 'jquery' );
}
}
add_action( 'wp_print_scripts', 'track_comment_form_head' );
$comment_form_id = 'commentform';
function yoast_get_comment_form_id( $args ) {
global $comment_form_id;
$comment_form_id = $args['id_form'];
return $args;
}
add_filter( 'comment_form_defaults', 'yoast_get_comment_form_id', 99, 1 );
function yoast_track_comment_form() {
global $comment_form_id, $post;
$yoast_ga_options = get_option( 'Yoast_Google_Analytics' );
if ( yoast_ga_do_tracking() && $yoast_ga_options["trackcommentform"] ) {
?>
]*) href=('|\")\/([^\"']*)('|\")/", " ]*) href=('|\")#([^\"']*)('|\")/", " ';
}
$options = get_option( 'Yoast_Google_Analytics' );
if ( !is_array( $options ) ) {
$options = get_option( 'GoogleAnalyticsPP' );
if ( !is_array( $options ) ) {
if ( !isset( $ga_admin ) )
$ga_admin = new GA_Admin();
$ga_admin->set_defaults();
} else {
delete_option( 'GoogleAnalyticsPP' );
if ( $options['admintracking'] ) {
$options["ignore_userlevel"] = '8';
unset( $options['admintracking'] );
} else {
$options["ignore_userlevel"] = '11';
}
update_option( 'Yoast_Google_Analytics', $options );
}
} else {
if ( !isset( $options['uastring'] ) || $options['uastring'] == '' )
return;
$gaf = new GA_Filter();
if ( isset( $options['allowanchor'] ) && $options['allowanchor'] ) {
add_action( 'init', 'ga_utm_hashtag_redirect', 1 );
}
if ( ( isset( $options['trackoutbound'] ) && $options['trackoutbound'] ) ||
( isset( $options['trackcrossdomain'] ) && $options['trackcrossdomain'] )
) {
// filters alter the existing content
add_filter( 'the_content', array( 'GA_Filter', 'the_content' ), 99 );
add_filter( 'widget_text', array( 'GA_Filter', 'widget_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_bookmarks', array( 'GA_Filter', 'bookmarks' ), 99 );
add_filter( 'get_comment_author_link', array( 'GA_Filter', 'comment_author_link' ), 99 );
add_filter( 'wp_nav_menu', array( 'GA_Filter', 'nav_menu' ), 99 );
}
if ( isset( $options['trackadsense'] ) && $options['trackadsense'] )
add_action( 'wp_head', array( 'GA_Filter', 'spool_adsense' ), 1 );
if ( !isset( $options['position'] ) )
$options['position'] = 'header';
switch ( $options['position'] ) {
case 'manual':
// No need to insert here, bail NOW.
break;
case 'header':
default:
add_action( 'wp_head', array( 'GA_Filter', 'spool_analytics' ), 2 );
break;
}
if ( isset( $options['trackregistration'] ) && $options['trackregistration'] )
add_action( 'login_head', array( 'GA_Filter', 'spool_analytics' ), 20 );
if ( isset( $options['rsslinktagging'] ) && $options['rsslinktagging'] )
add_filter( 'the_permalink_rss', array( 'GA_Filter', 'rsslinktagger' ), 99 );
}