<?php

/**
 * Returns the Google Analytics JavaScript code
 *
 * This function returns Google Analytics JavaScript code. If the customer has 
 * set their code, it will include this as well as the IRIS OpenWebsite code
 * that is created during the build. If the customer doesn't have a code, only 
 * the IRIS OpenWebsite code will be used. If one or more codes are missing, a 
 * message will be pushed to the developer console.
 *
 * @function google_analytics()
 *
 * @uses ow_global_var(), developer_console_message()
 *
 * @param N/A
 *
 * @return (string) The Google Analytics JavaScript code
 *
 * @since 1.0.0 
 *
 */
function google_analytics() {
	
	$script = "";
	$customerGACode = ow_global_var('googleA');
	$irisGACode = ow_global_var('IRISgoogleA');
	
	if ( $irisGACode != NULL ) {
		
		$script .= "<script defer>(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','//www.google-analytics.com/analytics.js','ga');";
		
		if ( $customerGACode != NULL ) {
			
			$script .= "ga('create','$customerGACode','auto');ga('send','pageview');";
		}
		else {
			
			developer_console_message( 'Customer Google Analytics tracking code not supplied', 'warning' );
		}
		$script .= "ga('create','$irisGACode','auto','irisOpenWebsite');ga('irisOpenWebsite.send','pageview');</script>";
		echo $script;
	}
	else {
		
		developer_console_message( 'IRIS OpenWebsite Google Analytics tracking code not supplied', 'alert' );
	}
}
add_action( 'wp_head', 'google_analytics', 120 );