/** * 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; }
/** * If a link is outgoing, add an onclick that runs some Google JS with a * generated URL * * @param array $m - A match from the preg_replace_callback in self::get_links * @return string - modified andchor tag */ function handle_link($m) { $code = wpGoogleAnalytics::get_options('code'); //get our site url...used to see if the link is outgoing. We can't use the wordpress setting, because wordpress might not be running at the document root. $site_url = ($_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']; $link = array_pop($m); //If the link is outgoing, we modify $m[0] (the anchor tag) if (preg_match("/^https?:\\/\\//i", $link) && strpos(strtolower($link), strtolower($site_url)) !== 0) { //get our custom link $track['data'] = $link; $track['code'] = 'outgoing'; $track['url'] = wpGoogleAnalytics::get_url($track); // Check which version of the code the user is using, and user proper function $function = strpos($code, 'ga.js') !== false ? 'pageTracker._trackPageview' : 'urchinTracker'; $onclick = "{$function}('{$track['url']}');"; //If there is already an onclick, add to the beginning of it (adding to the end will not work, because too many people leave off the ; from the last statement) if (preg_match("/onclick\\s*=\\s*(['\"])/iUx", $m[0], $match)) { //If the onclick uses single quotes, we use double...and vice versa if ($match[1] == "'") { $onclick = str_replace("'", '"', $onclick); } $m[0] = str_replace($match[0], $match[0] . $onclick, $m[0]); } else { $m[0] = str_replace('>', " onclick=\"{$onclick}\">", $m[0]); } } //return the anchor tag (modified or not) return $m[0]; }