hook) { wp_enqueue_script('jquery'); ?> optionname); return ''; } function textinput($id) { $options = get_option($this->optionname); return ''; } function config_page() { $options = get_option('AsyncGoogleAnalyticsPP'); if ( (isset($_POST['reset']) && $_POST['reset'] == "true") || !is_array($options) ) { $this->set_defaults(); echo "
Async Google Analytics settings reset to default.
Async Google Analytics settings updated.
Async Google Analytics is not active. You must enter your UA String for it to work.
"; } // end warning() } // end class AGA_Admin $aga_admin = new AGA_Admin(); } //endif /** * Code that actually inserts stuff into pages. */ if ( ! class_exists( 'AGA_Filter' ) ) { class AGA_Filter { /* * Insert the tracking code into the page */ function get_options_crossandsubdomains(){ $options = get_option('AsyncGoogleAnalyticsPP'); $result = ""; if( !$options["useasync"] ) return $result; $current_domain_set = ""; if ( isset($options['domain']) && $options['domain'] != "" ){ $current_domain_set = "".$options['domain']; $current_domain_set = strtolower(trim($current_domain_set)); if( $current_domain_set != "" && $current_domain_set != "none" && substr($current_domain_set,0,1) != "." ){ $current_domain_set = ".".$current_domain_set; } } $primary_domain_set = ""; if ( isset($options['primarydomain']) && $options['primarydomain'] != "" ){ $primary_domain_set = "".$options['primarydomain']; $primary_domain_set = strtolower(trim($primary_domain_set)); if( $primary_domain_set != "" && $primary_domain_set != "none" && substr($primary_domain_set,0,1) != "." ){ $primary_domain_set = ".".$primary_domain_set; } } if ( $options['crossdomainstracking'] ){ if( $current_domain_set == "" || $current_domain_set == "none" ){ //This is non primary domain $current_domain_set = "none"; }else{ //This is Primary Domain if ( $options['subdomainstracking'] ){ // }else{ $current_domain_set = $primary_domain_set; } } } if ( $options['crossdomainstracking'] ){ //This is Primary Domain if ( $options['crossdomainsprimary'] ){ if( $current_domain_set == "" || $current_domain_set == "none" ){ //Update with current server name or tell user to update it. THIS IS REQUIRED! } //This is non primary domain }else{ if( $current_domain_set == "" || $current_domain_set == "none" ){ //Tell user to update domain name with their primary domain name. //THIS IS REQUIRED AND MUST BE DIFFERENT WITH CURRENT DOMAIN NAME! } $current_domain_set = "none"; } } if ( $options['subdomainstracking'] ){ If( $options['crossdomainstracking'] ){ //DOMAIN NAME WILL BE IGNORED HERE }else{ if( $current_domain_set == "" || $current_domain_set == "none" ){ //Update with current server name or tell user to update it. THIS IS REQUIRED! } } } if( $current_domain_set != "" ) $result .= ",['_setDomainName','".$current_domain_set."']"; if ( $options['crossdomainstracking'] ) $result .= ",['_setAllowLinker',true]"; if ( $options['subdomainstracking'] || $options['crossdomainstracking'] ) $result .= ",['_setAllowHash',false]"; return $result; } function spool_analytics() { $options = get_option('AsyncGoogleAnalyticsPP'); if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) { //USE ASYNC METHOD if( $options["useasync"] ){ echo "\n".''; //USE TRADITIONAL METHOD }else{ echo ''."\n"; echo ''."\n"; } else { if ( $options["extrase"] ) { /** * We need to load another script, so we need to close the try / catch, load the script, and open it again. */ echo "\t".'} catch(err) {}'."\n"; echo ''."\n"; echo ''."\n"; echo ''."\n"; } }////END OF TRADITIONAL METHOD } else if ( $options["uastring"] != "" && current_user_can('edit_users') && !$options["admintracking"] ) { echo ""; } else if ( $options["uastring"] == "" && current_user_can('edit_users') ) { echo ""; } } function spool_analytics_async_head() { $options = get_option('AsyncGoogleAnalyticsPP'); if( $options["useasync"] ){ if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ){ echo "\n".''; } else if ( $options["uastring"] != "" && current_user_can('edit_users') && !$options["admintracking"] ) { echo ""; } else if ( $options["uastring"] == "" && current_user_can('edit_users') ) { echo ""; } } } function spool_analytics_async_foot() { $options = get_option('AsyncGoogleAnalyticsPP'); if( $options["useasync"] ){ if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ){ echo "\n".''; } else if ( $options["uastring"] != "" && current_user_can('edit_users') && !$options["admintracking"] ) { echo ""; } else if ( $options["uastring"] == "" && current_user_can('edit_users') ) { echo ""; } } } /* * Insert the AdSense parameter code into the page. This'll go into the header per Google's instructions. */ function spool_adsense() { $options = get_option('AsyncGoogleAnalyticsPP'); if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) { echo ''."\n"; } } /* Create an array which contians: * "domain" e.g. boakes.org * "host" e.g. store.boakes.org */ 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); } return array("domain"=>$matches[0],"host"=>$host); } function ga_parse_link($leaf, $matches){ $origin = AGA_Filter::ga_get_domain($_SERVER["HTTP_HOST"]); $options = get_option('AsyncGoogleAnalyticsPP'); // Break out immediately if the link is not an http or https link. if (strpos($matches[2],"http") !== 0) $target = false; else $target = AGA_Filter::ga_get_domain($matches[3]); $coolBit = ""; $extension = substr($matches[3],-3); $dlextensions = split(",",$options['dlextensions']); $async_used = FALSE; if( $options["useasync"] ) $async_used = TRUE; if ( $target ) { if ( $target["domain"] != $origin["domain"] ){ if ($options['domainorurl'] == "domain") { $coolBit .= ($async_used) ? "javascript:_gaq.push(['_trackPageview','".$leaf."/".$target["host"]."']);" : "javascript:pageTracker._trackPageview('".$leaf."/".$target["host"]."');"; } else if ($options['domainorurl'] == "url") { $coolBit .= ($async_used) ? "javascript:_gaq.push(['_trackPageview','".$leaf."/".$matches[2]."//".$matches[3]."']);" : "javascript:pageTracker._trackPageview('".$leaf."/".$matches[2]."//".$matches[3]."');"; } } else if ( in_array($extension, $dlextensions) && $target["domain"] == $origin["domain"] ) { $file = str_replace($origin["domain"],"",$matches[3]); $file = str_replace('www.',"",$file); $coolBit .= ($async_used) ? "javascript:_gaq.push(['_trackPageview','".$options['dlprefix'].$file."']);" : "javascript:pageTracker._trackPageview('".$options['dlprefix'].$file."');"; } } if ($coolBit != "") { if (preg_match('/onclick=[\'\"](.*?)[\'\"]/i', $matches[4]) > 0) { $matches[4] = preg_replace('/onclick=[\'\"](.*?)[\'\"]/i', 'onclick="' . $coolBit .' $1"', $matches[4]); } else { $matches[4] = 'onclick="' . $coolBit . '"' . $matches[4]; } } return '' . $matches[5] . ''; } function ga_parse_article_link($matches){ $options = get_option('AsyncGoogleAnalyticsPP'); return AGA_Filter::ga_parse_link($options['artprefix'],$matches); } function ga_parse_comment_link($matches){ $options = get_option('AsyncGoogleAnalyticsPP'); return AGA_Filter::ga_parse_link($options['comprefix'],$matches); } function the_content($text) { if (!is_feed()) { static $anchorPattern = '/(.*?)<\/a>/i'; $text = preg_replace_callback($anchorPattern,array('AGA_Filter','ga_parse_article_link'),$text); } return $text; } function comment_text($text) { if (!is_feed()) { static $anchorPattern = '/(.*?)<\/a>/i'; $text = preg_replace_callback($anchorPattern,array('AGA_Filter','ga_parse_comment_link'),$text); } return $text; } function comment_author_link($text) { $options = get_option('AsyncGoogleAnalyticsPP'); if (current_user_can('edit_users') && !$options["admintracking"]) { return $text; } static $anchorPattern = '/(.*\s+.*?href\s*=\s*)["\'](.*?)["\'](.*)/'; preg_match($anchorPattern, $text, $matches); if ($matches[2] == "") return $text; $target = AGA_Filter::ga_get_domain($matches[2]); $coolBit = ""; $origin = AGA_Filter::ga_get_domain($_SERVER["HTTP_HOST"]); $async_used = FALSE; if( $options["useasync"] ) $async_used = TRUE; if ( $target["domain"] != $origin["domain"] ){ if ($options['domainorurl'] == "domain") { $coolBit .= ($async_used) ? "onclick=\"javascript:_gaq.push(['_trackPageview','".$options['comautprefix']."/".$target["host"]."']);\"" : "onclick=\"javascript:pageTracker._trackPageview('".$options['comautprefix']."/".$target["host"]."');\""; } else if ($options['domainorurl'] == "url") { $coolBit .= ($async_used) ? "onclick=\"javascript:_gaq.push(['_trackPageview','".$options['comautprefix']."/".$matches[2]."']);\"" : "onclick=\"javascript:pageTracker._trackPageview('".$options['comautprefix']."/".$matches[2]."');\""; } } return $matches[1] . "\"" . $matches[2] . "\" " . $coolBit ." ". $matches[3]; } function bookmarks($bookmarks) { $options = get_option('AsyncGoogleAnalyticsPP'); $async_used = FALSE; if( $options["useasync"] ) $async_used = TRUE; if (!is_admin() && (!current_user_can('edit_users') || $options['admintracking'] ) ) { foreach ( (array) $bookmarks as $bookmark ) { $target = AGA_Filter::ga_get_domain($bookmark->link_url); $sitedomain = AGA_Filter::ga_get_domain(get_bloginfo('url')); if ($target['host'] == $sitedomain['host']) continue; if ($options['domainorurl'] == "domain") { if( $async_used ){ $bookmark->link_rel = $bookmark->link_rel."\" onclick=\"javascript:_gaq.push(['_trackPageview','".$options['blogrollprefix']."/".$target["host"]."']);"; }else{ $bookmark->link_rel = $bookmark->link_rel."\" onclick=\"javascript:pageTracker._trackPageview('".$options['blogrollprefix']."/".$target["host"]."');"; } } else if ($options['domainorurl'] == "url") { if( $async_used ){ $bookmark->link_rel = $bookmark->link_rel."\" onclick=\"javascript:_gaq.push(['_trackPageview','".$options['blogrollprefix']."/".$bookmark->link_url."']);"; }else{ $bookmark->link_rel = $bookmark->link_rel."\" onclick=\"javascript:pageTracker._trackPageview('".$options['blogrollprefix']."/".$bookmark->link_url."');"; } } } } return $bookmarks; } function rsslinktagger($guid) { $options = get_option('AsyncGoogleAnalyticsPP'); 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); } } } // class AGA_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. Needs some work as it also needs to do that when the first utm_ var is actually not the * first GET variable in the URL. */ function ga_utm_hashtag_redirect() { if (isset($_SERVER['REQUEST_URI'])) { if (strpos($_SERVER['REQUEST_URI'], "utm_") !== false) { $url = aga_current_protocol().$_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 aga_current_protocol($echo=FALSE){ $url = 'http://'; if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != ""){ $url = 'https://'; } if($echo){ echo $url; }else{ return $url; } } function get_aga_source_async($echo=FALSE){ $url = aga_current_protocol(); if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != ""){ $url .= 'ssl'; }else{ $url .= 'www'; } $url .= '.google-analytics.com/ga.js'; if($echo){ echo $url; }else{ return $url; } } $agaf = new AGA_Filter(); $options = get_option('AsyncGoogleAnalyticsPP'); if (!is_array($options)) $aga_admin->set_defaults(); if ( $options['allowanchor'] ) { add_action('init','ga_utm_hashtag_redirect',1); } if ($options['trackoutbound']) { // filters alter the existing content add_filter('the_content', array('AGA_Filter','the_content'), 99); add_filter('the_excerpt', array('AGA_Filter','the_content'), 99); add_filter('comment_text', array('AGA_Filter','comment_text'), 99); add_filter('get_bookmarks', array('AGA_Filter','bookmarks'), 99); add_filter('get_comment_author_link', array('AGA_Filter','comment_author_link'), 99); } if ($options['trackadsense']) add_action('wp_head', array('AGA_Filter','spool_adsense'),10); if ($options['position'] == 'footer' || $options['position'] == ""){ if( $options["useasync"] ){ add_action('wp_footer', array('AGA_Filter','spool_analytics_async_foot'),999); add_action('wp_head', array('AGA_Filter','spool_analytics_async_head'),1); }else{ add_action('wp_footer', array('AGA_Filter','spool_analytics')); } }else{ add_action('wp_head', array('AGA_Filter','spool_analytics'),20); } if ($options['trackregistration']) add_action('login_head', array('AGA_Filter','spool_analytics'),20); if ($options['rsslinktagging']) add_filter ( 'the_permalink_rss', array('AGA_Filter','rsslinktagger'), 99 ); ?>