<?php // Google Analytics - Core Functions

if (!defined('ABSPATH')) die();

function ga_google_analytics_pro_default_options($option) {
	
	global $GA_Google_Analytics_Pro;
	
	return $GA_Google_Analytics_Pro->default_options($option);
	
}

function ga_google_analytics_pro_get_options($option) {
	
	$default = ga_google_analytics_pro_default_options($option);
	
	return get_option('gapro_'. $option, $default);
	
}

function ga_google_analytics_pro_tracking_optout() {
	
	return isset($_COOKIE['gapro-optout']) ? intval($_COOKIE['gapro-optout']) : 0;
	
}

function ga_google_analytics_pro_tracking_optin() {
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$optin_require = isset($options_optout['optin_require']) ? $options_optout['optin_require'] : 0;
	
	$optin = isset($_COOKIE['gapro-optin']) ? intval($_COOKIE['gapro-optin']) : 0;
	
	return ($optin_require && !$optin) ? 1 : 0;
	
}

function ga_google_analytics_pro_tracking_primary_head() {
	
	add_action('admin_head', 'ga_google_analytics_pro_tracking_code');
	
	add_action('wp_head', 'ga_google_analytics_pro_tracking_code');
	
}

function ga_google_analytics_pro_tracking_primary_footer() {
	
	add_action('admin_footer', 'ga_google_analytics_pro_tracking_code');
	
	add_action('wp_footer', 'ga_google_analytics_pro_tracking_code');
	
}

function ga_google_analytics_pro_tracking_primary_custom() {
	
	add_action('gapro_after_body_open_tag', 'ga_google_analytics_pro_tracking_code');
	
}

function ga_google_analytics_pro_tracking_custom_head($var) {
	
	extract(ga_google_analytics_pro_options());
	
	if (${'custom'. $var .'_admin'}) add_action('admin_head', 'ga_google_analytics_pro_custom_code_'. $var);
	
	if (${'custom'. $var .'_frontend'} || !empty(${'custom'. $var .'_postids'})) add_action('wp_head', 'ga_google_analytics_pro_custom_code_'. $var);
	
}

function ga_google_analytics_pro_tracking_custom_footer($var) {
	
	extract(ga_google_analytics_pro_options());
	
	if (${'custom'. $var .'_admin'}) add_action('admin_footer', 'ga_google_analytics_pro_custom_code_'. $var);
	
	if (${'custom'. $var .'_frontend'} || !empty(${'custom'. $var .'_postids'})) add_action('wp_footer', 'ga_google_analytics_pro_custom_code_'. $var);
	
}

function ga_google_analytics_pro_tracking_login() {
	
	$login_hook = apply_filters('gapro_login_hook', 'login_head'); // e.g., login_footer
	
	add_action($login_hook, 'ga_google_analytics_pro_tracking_code');
	
}

function ga_google_analytics_pro_init() {
	
	extract(ga_google_analytics_pro_options());
	
	if (!empty($custom1_code) && $custom1_position == 0) ga_google_analytics_pro_tracking_custom_head('1');
	if (!empty($custom2_code) && $custom2_position == 0) ga_google_analytics_pro_tracking_custom_head('2');
	if (!empty($custom3_code) && $custom3_position == 0) ga_google_analytics_pro_tracking_custom_head('3');
	if (!empty($custom4_code) && $custom4_position == 0) ga_google_analytics_pro_tracking_custom_head('4');
	if (!empty($custom5_code) && $custom5_position == 0) ga_google_analytics_pro_tracking_custom_head('5');
	if (!empty($custom6_code) && $custom6_position == 0) ga_google_analytics_pro_tracking_custom_head('6');
	if (!empty($custom7_code) && $custom7_position == 0) ga_google_analytics_pro_tracking_custom_head('7');
	if (!empty($custom8_code) && $custom8_position == 0) ga_google_analytics_pro_tracking_custom_head('8');
	
	if ($gapro_location == 0) {
		
		ga_google_analytics_pro_tracking_primary_head();
		
	}
	
	if (!empty($custom1_code) && $custom1_position == 1) ga_google_analytics_pro_tracking_custom_head('1');
	if (!empty($custom2_code) && $custom2_position == 1) ga_google_analytics_pro_tracking_custom_head('2');
	if (!empty($custom3_code) && $custom3_position == 1) ga_google_analytics_pro_tracking_custom_head('3');
	if (!empty($custom4_code) && $custom4_position == 1) ga_google_analytics_pro_tracking_custom_head('4');
	if (!empty($custom5_code) && $custom5_position == 1) ga_google_analytics_pro_tracking_custom_head('5');
	if (!empty($custom6_code) && $custom6_position == 1) ga_google_analytics_pro_tracking_custom_head('6');
	if (!empty($custom7_code) && $custom7_position == 1) ga_google_analytics_pro_tracking_custom_head('7');
	if (!empty($custom8_code) && $custom8_position == 1) ga_google_analytics_pro_tracking_custom_head('8');
	
	//
	
	if (!empty($custom1_code) && $custom1_position == 2) ga_google_analytics_pro_tracking_custom_footer('1');
	if (!empty($custom2_code) && $custom2_position == 2) ga_google_analytics_pro_tracking_custom_footer('2');
	if (!empty($custom3_code) && $custom3_position == 2) ga_google_analytics_pro_tracking_custom_footer('3');
	if (!empty($custom4_code) && $custom4_position == 2) ga_google_analytics_pro_tracking_custom_footer('4');
	if (!empty($custom5_code) && $custom5_position == 2) ga_google_analytics_pro_tracking_custom_footer('5');
	if (!empty($custom6_code) && $custom6_position == 2) ga_google_analytics_pro_tracking_custom_footer('6');
	if (!empty($custom7_code) && $custom7_position == 2) ga_google_analytics_pro_tracking_custom_footer('7');
	if (!empty($custom8_code) && $custom8_position == 2) ga_google_analytics_pro_tracking_custom_footer('8');
	
	if ($gapro_location == 1) {
		
		ga_google_analytics_pro_tracking_primary_footer();
		
	}
	
	if (!empty($custom1_code) && $custom1_position == 3) ga_google_analytics_pro_tracking_custom_footer('1');
	if (!empty($custom2_code) && $custom2_position == 3) ga_google_analytics_pro_tracking_custom_footer('2');
	if (!empty($custom3_code) && $custom3_position == 3) ga_google_analytics_pro_tracking_custom_footer('3');
	if (!empty($custom4_code) && $custom4_position == 3) ga_google_analytics_pro_tracking_custom_footer('4');
	if (!empty($custom5_code) && $custom5_position == 3) ga_google_analytics_pro_tracking_custom_footer('5');
	if (!empty($custom6_code) && $custom6_position == 3) ga_google_analytics_pro_tracking_custom_footer('6');
	if (!empty($custom7_code) && $custom7_position == 3) ga_google_analytics_pro_tracking_custom_footer('7');
	if (!empty($custom8_code) && $custom8_position == 3) ga_google_analytics_pro_tracking_custom_footer('8');
	
	//
	
	if ($gapro_location == 2) {
		
		ga_google_analytics_pro_tracking_primary_custom();
		
	}
	
	//
	
	if ($login_page) {
		
		ga_google_analytics_pro_tracking_login();
		
	}
	
}

function ga_google_analytics_pro_tracking_code($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	if (!$override && ga_google_analytics_pro_disable()) return;
	
	if ($gapro_library == 3) {
		
		ga_google_analytics_pro_legacy();
		
	} elseif ($gapro_library == 2) {
		
		ga_google_analytics_pro_global();
		
	} else {
		
		ga_google_analytics_pro_universal();
		
	}
	
}

function ga_google_analytics_pro_custom_code_1($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom1 && $optin_require && !$optin) return;
	
	if ($disable_custom1 && $optout) return;
	
	if (empty($custom1_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom1_postids) && is_singular()) {
		
		$custom1_ids = array_map('trim', explode(',', $custom1_postids));
		
		foreach ($custom1_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom1_exclude) && is_singular()) {
		
		$custom1_ids = array_map('trim', explode(',', $custom1_exclude));
		
		foreach ($custom1_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom1_admin || $custom1_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom1_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_1', $output);
	
}

function ga_google_analytics_pro_custom_code_2($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom2 && $optin_require && !$optin) return;
	
	if ($disable_custom2 && $optout) return;
	
	if (empty($custom2_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom2_postids) && is_singular()) {
		
		$custom2_ids = array_map('trim', explode(',', $custom2_postids));
		
		foreach ($custom2_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom2_exclude) && is_singular()) {
		
		$custom2_ids = array_map('trim', explode(',', $custom2_exclude));
		
		foreach ($custom2_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom2_admin || $custom2_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom2_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_2', $output);
	
}

function ga_google_analytics_pro_custom_code_3($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom3 && $optin_require && !$optin) return;
	
	if ($disable_custom3 && $optout) return;
	
	if (empty($custom3_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom3_postids) && is_singular()) {
		
		$custom3_ids = array_map('trim', explode(',', $custom3_postids));
		
		foreach ($custom3_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom3_exclude) && is_singular()) {
		
		$custom3_ids = array_map('trim', explode(',', $custom3_exclude));
		
		foreach ($custom3_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom3_admin || $custom3_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom3_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_3', $output);
	
}

function ga_google_analytics_pro_custom_code_4($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom4 && $optin_require && !$optin) return;
	
	if ($disable_custom4 && $optout) return;
	
	if (empty($custom4_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom4_postids) && is_singular()) {
		
		$custom4_ids = array_map('trim', explode(',', $custom4_postids));
		
		foreach ($custom4_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom4_exclude) && is_singular()) {
		
		$custom4_ids = array_map('trim', explode(',', $custom4_exclude));
		
		foreach ($custom4_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom4_admin || $custom4_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom4_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_4', $output);
	
}

function ga_google_analytics_pro_custom_code_5($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom5 && $optin_require && !$optin) return;
	
	if ($disable_custom5 && $optout) return;
	
	if (empty($custom5_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom5_postids) && is_singular()) {
		
		$custom5_ids = array_map('trim', explode(',', $custom5_postids));
		
		foreach ($custom5_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom5_exclude) && is_singular()) {
		
		$custom5_ids = array_map('trim', explode(',', $custom5_exclude));
		
		foreach ($custom5_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom5_admin || $custom5_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom5_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_5', $output);
	
}

function ga_google_analytics_pro_custom_code_6($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom6 && $optin_require && !$optin) return;
	
	if ($disable_custom6 && $optout) return;
	
	if (empty($custom6_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom6_postids) && is_singular()) {
		
		$custom6_ids = array_map('trim', explode(',', $custom6_postids));
		
		foreach ($custom6_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom6_exclude) && is_singular()) {
		
		$custom6_ids = array_map('trim', explode(',', $custom6_exclude));
		
		foreach ($custom6_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom6_admin || $custom6_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom6_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_6', $output);
	
}

function ga_google_analytics_pro_custom_code_7($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom7 && $optin_require && !$optin) return;
	
	if ($disable_custom7 && $optout) return;
	
	if (empty($custom7_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom7_postids) && is_singular()) {
		
		$custom7_ids = array_map('trim', explode(',', $custom7_postids));
		
		foreach ($custom7_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom7_exclude) && is_singular()) {
		
		$custom7_ids = array_map('trim', explode(',', $custom7_exclude));
		
		foreach ($custom7_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom7_admin || $custom7_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom7_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_7', $output);
	
}

function ga_google_analytics_pro_custom_code_8($override = false) {
	
	extract(ga_google_analytics_pro_options());
	
	$optin  = isset($_COOKIE['gapro-optin'])  ? $_COOKIE['gapro-optin']  : 0;
	
	$optout = isset($_COOKIE['gapro-optout']) ? $_COOKIE['gapro-optout'] : 0;
	
	if ($disable_custom8 && $optin_require && !$optin) return;
	
	if ($disable_custom8 && $optout) return;
	
	if (empty($custom8_code)) return;
	
	$output = '';
	
	$enable = false;
	
	if (!empty($custom8_postids) && is_singular()) {
		
		$custom8_ids = array_map('trim', explode(',', $custom8_postids));
		
		foreach ($custom8_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$enable = true;
				
				break;
				
			}
			
		}
		
	}
	
	$disable = false;
	
	if (!empty($custom8_exclude) && is_singular()) {
		
		$custom8_ids = array_map('trim', explode(',', $custom8_exclude));
		
		foreach ($custom8_ids as $id) {
			
			if (intval($id) === get_the_ID()) {
				
				$disable = true;
				
				break;
				
			}
			
		}
		
	}
	
	if ($enable || $override || $custom8_admin || $custom8_frontend) {
		
		$output .= $disable ? '' : ga_google_analytics_pro_custom_filter($custom8_code) . "\n";
		
	}
	
	echo apply_filters('gapro_custom_code_8', $output);
	
}

function ga_google_analytics_pro_disable() {
	
	if (ga_google_analytics_pro_tracking_optin()) return true;
	
	if (ga_google_analytics_pro_tracking_optout()) return true;
	
	$options = ga_google_analytics_pro_options();
	
	$disable = apply_filters('ga_google_analytics_pro_disable_custom', false, $options);
	
	if ($disable) return true;
	
	extract($options);
	
	if (empty($gapro_id) || empty($gapro_library)) return true;
	
	if (current_user_can('administrator') && $disable_admin) return true;
	
	if (is_user_logged_in() && $disable_logged) return true;
	
	if (is_archive() && $disable_archives) return true;
	
	if (is_search() && $disable_search) return true;
	
	if (is_admin() && !$admin_area) return true;
	
	//
	
	if (get_the_ID() && !empty($disable_post_ids)) {
		
		$disable_ids = array_map('trim', explode(',', $disable_post_ids));
		
		foreach ($disable_ids as $id) {
			
			if ((int) $id === get_the_ID()) return true;
			
		}
		
	}
	
	//
	
	if (is_user_logged_in() && !empty($disable_user_roles)) {
		
		$current_user = wp_get_current_user();
		
		if ($current_user->exists()) {
			
			$disable_roles = array_map('trim', explode(',', $disable_user_roles));
			
			foreach ($disable_roles as $role) {
				
				if (in_array($role, $current_user->roles)) return true;
				
			}
			
		}
		
	}
	
	//
	
	if (get_post_type() && !empty($disable_post_types)) {
		
		$disable_types = array_map('trim', explode(',', $disable_post_types));
		
		foreach ($disable_types as $type) {
			
			if ($type === get_post_type()) return true;
			
		}
		
	}
	
	return false;
	
}

function ga_google_analytics_pro_custom_filter($code) {
	
	$current_user = wp_get_current_user();
	
	$current_id   = $current_user ? $current_user->ID : '';
	
	$current_id   = (empty($current_id)) ? '' : $current_id;
	
	$current_name = $current_user ? $current_user->user_login : '';
	
	$patterns = array();
	
	$patterns[0] = "/%%userid%%/";
	$patterns[1] = "/%%username%%/";
	$patterns[2] = "/{{{/";
	$patterns[3] = "/}}}/";
	
	$replaces = array();
	
	$replaces[0] = $current_id;
	$replaces[1] = $current_name;
	$replaces[2] = '<';
	$replaces[3] = '>';
	
	$code = preg_replace($patterns, $replaces, $code);
	
	return apply_filters('gapro_custom_filter', $code);
	
}

function ga_google_analytics_credit() {
	
	return '<!-- GA Pro @ https://m0n.co/gapro -->' ."\n";
	
}

function ga_google_analytics_pro_universal() {
	
	extract(ga_google_analytics_pro_options());
	
	$custom = ga_google_analytics_pro_custom_filter($custom_ga_code);
	$custom = explode(PHP_EOL, $custom);
	
	$tracker_object = apply_filters('gapro_tracker_object_universal', $tracker_object);
	
	$auto = apply_filters('ga_google_analytics_enable_auto', true) ? ", 'auto'" : "";
	
	$display = "ga('require', 'displayfeatures');";
	$link    = "ga('require', 'linkid');";
	$anon    = "ga('set', 'anonymizeIp', true);";
	$ssl     = "ga('set', 'forceSSL', true);";
	
	$script_atts = apply_filters('gapro_script_atts', '');
	
	?>

		<script<?php echo esc_attr($script_atts); ?>>
			(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
			(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
			m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
			})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
			ga('create', '<?php echo $gapro_id; ?>'<?php echo $auto; if ($tracker_object) echo ', '. stripslashes(rtrim($tracker_object)); ?>);
<?php 
			if ($custom_ga_code) {
				foreach ($custom as $c) echo "\t\t\t" . stripslashes(rtrim($c)) . "\n";
			}
?>
			<?php 
				if ($gapro_display_ads) echo $display . "\n\t\t\t";
				if ($gapro_link_attr)   echo $link    . "\n\t\t\t";
				if ($gapro_anonymize)   echo $anon    . "\n\t\t\t";
				if ($gapro_force_ssl)   echo $ssl     . "\n\t\t\t";
			?>ga('send', 'pageview');
		</script>

	<?php
	
}

function ga_google_analytics_pro_global() {
	
	extract(ga_google_analytics_pro_options());
	
	$custom = ga_google_analytics_pro_custom_filter($custom_ga_code);
	$custom = explode(PHP_EOL, $custom);
	
	$tracker_object = apply_filters('gapro_tracker_object_global', $tracker_object);
	
	$script_atts_ext = apply_filters('gapro_script_atts_ext', ' async');
	$script_atts     = apply_filters('gapro_script_atts', '');
	
	?>

		<script<?php echo esc_attr($script_atts_ext); ?> src="https://www.googletagmanager.com/gtag/js?id=<?php echo $gapro_id; ?>"></script>
		<script<?php echo esc_attr($script_atts); ?>>
			window.dataLayer = window.dataLayer || [];
			function gtag(){dataLayer.push(arguments);}
			gtag('js', new Date());
<?php 
			if ($custom_ga_code) {
				foreach ($custom as $c) echo "\t\t\t" . stripslashes(rtrim($c)) . "\n";
			}
?>
			gtag('config', '<?php echo $gapro_id; ?>'<?php if ($tracker_object) echo ', '. stripslashes(rtrim($tracker_object)); ?>);
		</script>

	<?php
	
}

function ga_google_analytics_pro_legacy() {
	
	extract(ga_google_analytics_pro_options());
	
	$custom = ga_google_analytics_pro_custom_filter($custom_ga_code);
	
	$ga_alt  = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';";
	$ga_src  = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';";
	$ga_link = "var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';\n\t\t\t_gaq.push(['_require', 'inpage_linkid', pluginUrl]);";
	$ga_anon = "_gaq.push(['_gat._anonymizeIp']);";
	$ga_ssl  = "_gaq.push(['_gat._forceSSL']);";
	
	if ($gapro_display_ads) $ga_src = $ga_alt;
	
	$script_atts = apply_filters('gapro_script_atts', '');
	
	?>

		<script<?php echo esc_attr($script_atts); ?> type="text/javascript">
			var _gaq = _gaq || [];
			<?php 
				if ($gapro_link_attr) echo $ga_link . "\n\t\t\t";
				if ($gapro_anonymize) echo $ga_anon . "\n\t\t\t";
				if ($gapro_force_ssl) echo $ga_ssl  . "\n\t\t\t";
				if ($custom_ga_code)  echo stripslashes(rtrim($custom)) . "\n\t\t\t";
			?>_gaq.push(['_setAccount', '<?php echo $gapro_id; ?>']);
			_gaq.push(['_trackPageview']);
			(function() {
				var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
				ga.src = <?php echo $ga_src . "\n"; ?>
				var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
		</script>

	<?php
	
}

function ga_google_analytics_pro_options() {
	
	$options_configure = get_option('gapro_configure', ga_google_analytics_pro_get_options('configure'));
	
	$gapro_id = isset($options_configure['gapro_id']) ? $options_configure['gapro_id'] : '';
	
	$gapro_library = isset($options_configure['gapro_library']) ? $options_configure['gapro_library'] : 1;
	
	$gapro_display_ads = isset($options_configure['gapro_display_ads']) ? $options_configure['gapro_display_ads'] : 0;
	
	$gapro_link_attr = isset($options_configure['gapro_link_attr']) ? $options_configure['gapro_link_attr'] : 0;
	
	$gapro_anonymize = isset($options_configure['gapro_anonymize']) ? $options_configure['gapro_anonymize'] : 0;
	
	$gapro_force_ssl = isset($options_configure['gapro_force_ssl']) ? $options_configure['gapro_force_ssl'] : 0;
	
	$gapro_location = isset($options_configure['gapro_location']) ? $options_configure['gapro_location'] : 0;
	
	$tracker_object = isset($options_configure['tracker_object']) ? $options_configure['tracker_object'] : '';
	
	$custom_ga_code = isset($options_configure['custom_ga_code']) ? $options_configure['custom_ga_code'] : '';
	
	//
	
	$options_customize = get_option('gapro_customize', ga_google_analytics_pro_get_options('customize'));
	
	$custom1_code = isset($options_customize['custom1_code']) ? $options_customize['custom1_code'] : '';
	
	$custom1_postids = isset($options_customize['custom1_postids']) ? $options_customize['custom1_postids'] : '';
	
	$custom1_exclude = isset($options_customize['custom1_exclude']) ? $options_customize['custom1_exclude'] : '';
	
	$custom1_admin = isset($options_customize['custom1_admin']) ? $options_customize['custom1_admin'] : '';
	
	$custom1_frontend = isset($options_customize['custom1_frontend']) ? $options_customize['custom1_frontend'] : '';
	
	$custom1_position = isset($options_customize['custom1_position']) ? $options_customize['custom1_position'] : 1;
	
	
	$custom2_code = isset($options_customize['custom2_code']) ? $options_customize['custom2_code'] : '';
	
	$custom2_postids = isset($options_customize['custom2_postids']) ? $options_customize['custom2_postids'] : '';
	
	$custom2_exclude = isset($options_customize['custom2_exclude']) ? $options_customize['custom2_exclude'] : '';
	
	$custom2_admin = isset($options_customize['custom2_admin']) ? $options_customize['custom2_admin'] : '';
	
	$custom2_frontend = isset($options_customize['custom2_frontend']) ? $options_customize['custom2_frontend'] : '';
	
	$custom2_position = isset($options_customize['custom2_position']) ? $options_customize['custom2_position'] : 1;
	
	
	$custom3_code = isset($options_customize['custom3_code']) ? $options_customize['custom3_code'] : '';
	
	$custom3_postids = isset($options_customize['custom3_postids']) ? $options_customize['custom3_postids'] : '';
	
	$custom3_exclude = isset($options_customize['custom3_exclude']) ? $options_customize['custom3_exclude'] : '';
	
	$custom3_admin = isset($options_customize['custom3_admin']) ? $options_customize['custom3_admin'] : '';
	
	$custom3_frontend = isset($options_customize['custom3_frontend']) ? $options_customize['custom3_frontend'] : '';
	
	$custom3_position = isset($options_customize['custom3_position']) ? $options_customize['custom3_position'] : 1;
	
	
	$custom4_code = isset($options_customize['custom4_code']) ? $options_customize['custom4_code'] : '';
	
	$custom4_postids = isset($options_customize['custom4_postids']) ? $options_customize['custom4_postids'] : '';
	
	$custom4_exclude = isset($options_customize['custom4_exclude']) ? $options_customize['custom4_exclude'] : '';
	
	$custom4_admin = isset($options_customize['custom4_admin']) ? $options_customize['custom4_admin'] : '';
	
	$custom4_frontend = isset($options_customize['custom4_frontend']) ? $options_customize['custom4_frontend'] : '';
	
	$custom4_position = isset($options_customize['custom4_position']) ? $options_customize['custom4_position'] : 1;
	
	
	$custom5_code = isset($options_customize['custom5_code']) ? $options_customize['custom5_code'] : '';
	
	$custom5_postids = isset($options_customize['custom5_postids']) ? $options_customize['custom5_postids'] : '';
	
	$custom5_exclude = isset($options_customize['custom5_exclude']) ? $options_customize['custom5_exclude'] : '';
	
	$custom5_admin = isset($options_customize['custom5_admin']) ? $options_customize['custom5_admin'] : '';
	
	$custom5_frontend = isset($options_customize['custom5_frontend']) ? $options_customize['custom5_frontend'] : '';
	
	$custom5_position = isset($options_customize['custom5_position']) ? $options_customize['custom5_position'] : 1;
	
	
	$custom6_code = isset($options_customize['custom6_code']) ? $options_customize['custom6_code'] : '';
	
	$custom6_postids = isset($options_customize['custom6_postids']) ? $options_customize['custom6_postids'] : '';
	
	$custom6_exclude = isset($options_customize['custom6_exclude']) ? $options_customize['custom6_exclude'] : '';
	
	$custom6_admin = isset($options_customize['custom6_admin']) ? $options_customize['custom6_admin'] : '';
	
	$custom6_frontend = isset($options_customize['custom6_frontend']) ? $options_customize['custom6_frontend'] : '';
	
	$custom6_position = isset($options_customize['custom6_position']) ? $options_customize['custom6_position'] : 1;
	
	
	$custom7_code = isset($options_customize['custom7_code']) ? $options_customize['custom7_code'] : '';
	
	$custom7_postids = isset($options_customize['custom7_postids']) ? $options_customize['custom7_postids'] : '';
	
	$custom7_exclude = isset($options_customize['custom7_exclude']) ? $options_customize['custom7_exclude'] : '';
	
	$custom7_admin = isset($options_customize['custom7_admin']) ? $options_customize['custom7_admin'] : '';
	
	$custom7_frontend = isset($options_customize['custom7_frontend']) ? $options_customize['custom7_frontend'] : '';
	
	$custom7_position = isset($options_customize['custom7_position']) ? $options_customize['custom7_position'] : 1;
	
	
	$custom8_code = isset($options_customize['custom8_code']) ? $options_customize['custom8_code'] : '';
	
	$custom8_postids = isset($options_customize['custom8_postids']) ? $options_customize['custom8_postids'] : '';
	
	$custom8_exclude = isset($options_customize['custom8_exclude']) ? $options_customize['custom8_exclude'] : '';
	
	$custom8_admin = isset($options_customize['custom8_admin']) ? $options_customize['custom8_admin'] : '';
	
	$custom8_frontend = isset($options_customize['custom8_frontend']) ? $options_customize['custom8_frontend'] : '';
	
	$custom8_position = isset($options_customize['custom8_position']) ? $options_customize['custom8_position'] : 1;
	
	//
	
	$options_advanced = get_option('gapro_advanced', ga_google_analytics_pro_get_options('advanced'));
	
	$login_page = isset($options_advanced['login_page']) ? $options_advanced['login_page'] : 0;
	
	$admin_area = isset($options_advanced['admin_area']) ? $options_advanced['admin_area'] : 0;
	
	$disable_admin = isset($options_advanced['disable_admin']) ? $options_advanced['disable_admin'] : 0;
	
	$disable_logged = isset($options_advanced['disable_logged']) ? $options_advanced['disable_logged'] : 0;
	
	$disable_post_ids = isset($options_advanced['disable_post_ids']) ? $options_advanced['disable_post_ids'] : '';
	
	$disable_user_roles = isset($options_advanced['disable_user_roles']) ? $options_advanced['disable_user_roles'] : '';
	
	$disable_post_types = isset($options_advanced['disable_post_types']) ? $options_advanced['disable_post_types'] : '';
	
	$disable_archives = isset($options_advanced['disable_archives']) ? $options_advanced['disable_archives'] : 0;
	
	$disable_search = isset($options_advanced['disable_search']) ? $options_advanced['disable_search'] : 0;
	
	//
	
	$options_optout = get_option('gapro_optout', ga_google_analytics_pro_get_options('optout'));
	
	$optin_require = isset($options_optout['optin_require']) ? $options_optout['optin_require'] : 0;
	
	$disable_custom1 = isset($options_optout['disable_custom1']) ? $options_optout['disable_custom1'] : 0;
	
	$disable_custom2 = isset($options_optout['disable_custom2']) ? $options_optout['disable_custom2'] : 0;
	
	$disable_custom3 = isset($options_optout['disable_custom3']) ? $options_optout['disable_custom3'] : 0;
	
	$disable_custom4 = isset($options_optout['disable_custom4']) ? $options_optout['disable_custom4'] : 0;
	
	$disable_custom5 = isset($options_optout['disable_custom5']) ? $options_optout['disable_custom5'] : 0;
	
	$disable_custom6 = isset($options_optout['disable_custom6']) ? $options_optout['disable_custom6'] : 0;
	
	$disable_custom7 = isset($options_optout['disable_custom7']) ? $options_optout['disable_custom7'] : 0;
	
	$disable_custom8 = isset($options_optout['disable_custom8']) ? $options_optout['disable_custom8'] : 0;
	
	/*
	
	Configure
	$gapro_id, $gapro_library, 
	$gapro_display_ads, $gapro_link_attr, $gapro_anonymize, $gapro_force_ssl, $gapro_location, 
	$tracker_object, $custom_ga_code
	
	Customize
	$custom1_code, $custom1_postids, $custom1_exclude, $custom1_admin, $custom1_frontend, $custom1_position, 
	$custom2_code, $custom2_postids, $custom2_exclude, $custom2_admin, $custom2_frontend, $custom2_position, 
	$custom3_code, $custom3_postids, $custom3_exclude, $custom3_admin, $custom3_frontend, $custom3_position
	...
	
	Advanced
	$login_page, $admin_area, $disable_admin, $disable_logged, 
	$disable_post_ids, $disable_user_roles, $disable_post_types, $disable_archives, $disable_search
	
	Opt-Out
	$optin_require,
	$disable_custom1 - $disable_custom8
	
	*/
	
	$options_array = array(
		
		'gapro_id'           => $gapro_id,
		'gapro_library'      => $gapro_library,
		'gapro_display_ads'  => $gapro_display_ads,
		'gapro_link_attr'    => $gapro_link_attr,
		'gapro_anonymize'    => $gapro_anonymize,
		'gapro_force_ssl'    => $gapro_force_ssl,
		'gapro_location'     => $gapro_location,
		'tracker_object'     => $tracker_object,
		'custom_ga_code'     => $custom_ga_code,
		
		'custom1_code'       => $custom1_code,
		'custom1_postids'    => $custom1_postids,
		'custom1_exclude'    => $custom1_exclude,
		'custom1_admin'      => $custom1_admin,
		'custom1_frontend'   => $custom1_frontend,
		'custom1_position'   => $custom1_position,
		
		'custom2_code'       => $custom2_code,
		'custom2_postids'    => $custom2_postids,
		'custom2_exclude'    => $custom2_exclude,
		'custom2_admin'      => $custom2_admin,
		'custom2_frontend'   => $custom2_frontend,
		'custom2_position'   => $custom2_position,
		
		'custom3_code'       => $custom3_code,
		'custom3_postids'    => $custom3_postids,
		'custom3_exclude'    => $custom3_exclude,
		'custom3_admin'      => $custom3_admin,
		'custom3_frontend'   => $custom3_frontend,
		'custom3_position'   => $custom3_position,
		
		'custom4_code'       => $custom4_code,
		'custom4_postids'    => $custom4_postids,
		'custom4_exclude'    => $custom4_exclude,
		'custom4_admin'      => $custom4_admin,
		'custom4_frontend'   => $custom4_frontend,
		'custom4_position'   => $custom4_position,
		
		'custom5_code'       => $custom5_code,
		'custom5_postids'    => $custom5_postids,
		'custom5_exclude'    => $custom5_exclude,
		'custom5_admin'      => $custom5_admin,
		'custom5_frontend'   => $custom5_frontend,
		'custom5_position'   => $custom5_position,
		
		'custom6_code'       => $custom6_code,
		'custom6_postids'    => $custom6_postids,
		'custom6_exclude'    => $custom6_exclude,
		'custom6_admin'      => $custom6_admin,
		'custom6_frontend'   => $custom6_frontend,
		'custom6_position'   => $custom6_position,
		
		'custom7_code'       => $custom7_code,
		'custom7_postids'    => $custom7_postids,
		'custom7_exclude'    => $custom7_exclude,
		'custom7_admin'      => $custom7_admin,
		'custom7_frontend'   => $custom7_frontend,
		'custom7_position'   => $custom7_position,
		
		'custom8_code'       => $custom8_code,
		'custom8_postids'    => $custom8_postids,
		'custom8_exclude'    => $custom8_exclude,
		'custom8_admin'      => $custom8_admin,
		'custom8_frontend'   => $custom8_frontend,
		'custom8_position'   => $custom8_position,
		
		'login_page'         => $login_page,
		'admin_area'         => $admin_area,
		'disable_admin'      => $disable_admin,
		'disable_logged'     => $disable_logged,
		'disable_post_ids'   => $disable_post_ids,
		'disable_user_roles' => $disable_user_roles,
		'disable_post_types' => $disable_post_types,
		'disable_archives'   => $disable_archives,
		'disable_search'     => $disable_search,
		
		'optin_require'      => $optin_require,
		'disable_custom1'    => $disable_custom1,
		'disable_custom2'    => $disable_custom2,
		'disable_custom3'    => $disable_custom3,
		'disable_custom4'    => $disable_custom4,
		'disable_custom5'    => $disable_custom5,
		'disable_custom6'    => $disable_custom6,
		'disable_custom7'    => $disable_custom7,
		'disable_custom8'    => $disable_custom8,
		
	);
	
	return apply_filters('ga_google_analytics_options_array', $options_array);
	
}