asga_options = get_option($this->option_name); //get action's priority $js_priority = absint($this->asga_options['js_priority']); //decide where to print code if ($this->asga_options['js_location'] == 1) add_action('wp_head', array($this, 'print_js_code'), $js_priority); else add_action('wp_footer', array($this, 'print_js_code'), $js_priority); } /** * 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; } /** * Prepare and print javascript code to front end */ function print_js_code() { //check if to proceed if (!$this->is_tracking_possible()) return; $options = $this->asga_options; //get tracking id $ga_id = $options['ga_id']; //decide sub-domain $domain = $options['ga_domain']; if (empty($domain)) $domain = 'auto'; $gaq = array(); global $wp_query; if ($options['ua_enabled'] == 1) { //if universal is enabled $gaq[] = "'create', '" . esc_attr($ga_id) . "', '" . esc_attr($domain) . "'"; $gaq[] = "'set', 'forceSSL', true"; if ($options['anonymise_ip'] == 1) { $gaq[] = "'set', 'anonymizeIp', true"; } if ($options['displayfeatures'] == 1) { $gaq[] = "'require', 'displayfeatures'"; } if ($options['ga_ela'] == 1) { $gaq[] = "'require', 'linkid', 'linkid.js'"; } if (is_404() && $options['log_404'] == 1) { $gaq[] = "'send','event','404',document.location.href,document.referrer"; } elseif ($wp_query->is_search && $options['log_search'] == 1) { $gaq[] = "'send','pageview','/?s=" . rawurlencode($wp_query->query_vars['s']) . "'"; } else { $gaq[] = "'send','pageview'"; } require('views/universal_script.php'); } else { //classic ga is enabled $ga_src = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'"; if ($options['displayfeatures'] == 1) { $ga_src = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'"; } $gaq[] = "'_setAccount', '" . esc_attr($ga_id) . "'"; if ($domain !== 'auto') { $gaq[] = "'_setDomainName', '" . esc_attr($domain) . "'"; } // enable SSL data $gaq[] = "'_gat._forceSSL'"; // Anonymous data if ($options['anonymise_ip'] == 1) { $gaq[] = "'_gat._anonymizeIp'"; } $plugin_url = ''; if ($options['ga_ela'] == 1) { $plugin_url = "var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';\n"; $gaq[] = "['_require', 'inpage_linkid', pluginUrl]"; } if (is_404() && $options['log_404'] == 1) { $gaq[] = "'_trackEvent','404',document.location.href,document.referrer"; } elseif ($wp_query->is_search && $options['log_search'] == 1) { $gaq[] = "'_trackPageview','/?s=" . rawurlencode($wp_query->query_vars['s']) . "'"; } else { $gaq[] = "'_trackPageview'"; } require('views/classic_script.php'); } } /** * Function determines whether to print tracking code or not * @return bool */ private function is_tracking_possible() { if (is_preview()) { echo ''; return false; } $options = $this->asga_options; $ga_id = $options['ga_id']; //if GA id is not set return early with a message if (empty($ga_id)) { echo ''; return false; } //if a user is logged in if (is_user_logged_in()) { if (is_super_admin()) { //if a network admin is logged in if (isset($options['ignore_role_networkAdmin']) && ($options['ignore_role_networkAdmin'] == 1)) { echo ''; return false; } } else { //If a normal user is logged in $role = array_shift(wp_get_current_user()->roles); if (isset($options['ignore_role_' . $role]) && ($options['ignore_role_' . $role] == 1)) { echo ''; return false; } } } return true; } } //end class if (is_admin()) { /* Load admin part only if we are inside wp-admin */ require(trailingslashit(dirname(__FILE__)) . "asga_admin.php"); //init admin class global $ASGA_Admin_Class; $ASGA_Admin_Class = ASGA_Admin_Class::get_instance(); } else { /*Init front end part*/ global $ank_simplified_ga; $ank_simplified_ga = Ank_Simplified_GA::get_instance(); }