<commit_before><?php
/**
 * Google Universal Analytics Tracker
 * ==================================
 *
 * Extension to add Google Universal Analytics.js tracking code to SilverStripe 3
 *
 * License: MIT-style license http://opensource.org/licenses/MIT
 * Authors: Techno Joy development team (www.technojoy.co.nz)
 */

class AnalyticsJS extends Extension {

	/* Name of the Google Analytics object */
	public static $global_name = 'ga';

	/* Whether to track outbound links & assets downloads */
	public static $track_links = true;

	/* Ignore tracking pages with ?flush=[?] */
	public static $ignore_flushes = true;

	/* Whether to compress the JavaScript */
	public static $compress_js = true;

	protected static $tracker_config = array();
	protected static $ga_trackers = false;
	protected static $tracker_names = array();
	protected static $tracker_counter = 1;

	/*
	 * Statically add Universal Analytics configs
	 * @param array (see README.md)
	 * @return null
	 */
	public static function add_ga() {
		$arg_list = func_get_args();
		if (count($arg_list) < 2) {
			trigger_error('GaTracker::add_ga() requires at least two arguments', E_USER_ERROR);
			return false;
		}
		array_push(self::$tracker_config, $arg_list);
	}

	/*
	 * Automatically initiate the code
	 */
	public function onAfterInit() {
		$this->injectGoogleUniversalAnalyticsCode();
	}

	/*
	 * Injects GA tracking code into <head>
	 * Optional: inline code to track downloads & outgoing links
	 */
	protected function injectGoogleUniversalAnalyticsCode() {
			/* Parse static configs */
			$this->parseGoogleUniversalAnalyticsConfigs();
			/* Generate header code with configs */
			$this->genUniversalAnalyticsCode();
			/* Add link tracking code */
			$this->generateUniversalAnalyticsLinkCode();
	}


	/*
	 * Parse static config array
	 * Sets static self::$tracker_names array
	 * Sets static self::$ga_trackers code
	 * @param null
	 * @return null
	 */
	protected function parseGoogleUniversalAnalyticsConfigs() {

		if (count(self::$tracker_config) == 0) {
			return false;
		}

		$skip_tracking = (!Director::isLive() || (self::$ignore_flushes && isset($_GET['flush']))) ? true : false;

		foreach (self::$tracker_config as $conf) {

			$args = array();

			if ($conf[0] == 'create') {
				$tname = false;
				if ($skip_tracking) {
					$conf[1] = preg_replace('/[0-9]{4,}-[0-9]+/', 'DEV-' . self::$tracker_counter++, $conf[1]);
				}
				foreach ($conf as $i) {
					if (is_array($i) && isset($i['name'])) {
						$tname = $i['name'] . '.';
						break;
					}
				}
				if (in_array($tname, self::$tracker_names)) {
					$ufname = ($tname === false) ? 'Default' : $tname;
					trigger_error('GaTracker::add_ga(): ' . $ufname .' Tracker already set, please use name', E_USER_WARNING);
					return false;
				}
				else {
					array_push(self::$tracker_names, $tname);
				}
			}

			foreach ($conf as $i) {
				array_push($args, json_encode($i));
			}

			self::$ga_trackers .= self::$global_name . '(' . implode(',', $args) .');'."\n";
		}

	}


	/*
	 * Generates <head> and inject insertHeadTags() JavaScript code
	 * for tracking if at least one tracking config has been specified
	 * @param null
	 * @return null
	 */
	protected function genUniversalAnalyticsCode() {

		if (count(self::$tracker_names) == 0) {
			return false;
		}
<commit_msg>Ignore add_ga() when same ID added twice<commit_after><?php
/**
 * Google Universal Analytics Tracker
 * ==================================
 *
 * Extension to add Google Universal Analytics.js tracking code to SilverStripe 3
 *
 * License: MIT-style license http://opensource.org/licenses/MIT
 * Authors: Techno Joy development team (www.technojoy.co.nz)
 */

class AnalyticsJS extends Extension {

	/* Name of the Google Analytics object */
	public static $global_name = 'ga';

	/* Whether to track outbound links & assets downloads */
	public static $track_links = true;

	/* Ignore tracking pages with ?flush=[?] */
	public static $ignore_flushes = true;

	/* Whether to compress the JavaScript */
	public static $compress_js = true;

	protected static $tracker_config = array();
	protected static $ga_trackers = false;
	protected static $tracker_names = array();
	protected static $ga_configs = array();
	protected static $tracker_counter = 1;

	/*
	 * Statically add Universal Analytics configs
	 * @param array (see README.md)
	 * @return null
	 */
	public static function add_ga() {
		$arg_list = func_get_args();
		if (count($arg_list) < 2) {
			trigger_error('GaTracker::add_ga() requires at least two arguments', E_USER_ERROR);
			return false;
		}
		array_push(self::$tracker_config, $arg_list);
	}

	/*
	 * Automatically initiate the code
	 */
	public function onAfterInit() {
		$this->injectGoogleUniversalAnalyticsCode();
	}

	/*
	 * Injects GA tracking code into <head>
	 * Optional: inline code to track downloads & outgoing links
	 */
	protected function injectGoogleUniversalAnalyticsCode() {
			/* Parse static configs */
			$this->parseGoogleUniversalAnalyticsConfigs();
			/* Generate header code with configs */
			$this->genUniversalAnalyticsCode();
			/* Add link tracking code */
			$this->generateUniversalAnalyticsLinkCode();
	}


	/*
	 * Parse static config array
	 * Sets static self::$tracker_names array
	 * Sets static self::$ga_trackers code
	 * @param null
	 * @return null
	 */
	protected function parseGoogleUniversalAnalyticsConfigs() {

		if (count(self::$tracker_config) == 0) {
			return false;
		}

		$skip_tracking = (!Director::isLive() || (self::$ignore_flushes && isset($_GET['flush']))) ? true : false;

		foreach (self::$tracker_config as $conf) {

			$args = array();

			if ($conf[0] == 'create') {
				$tname = false;

				foreach ($conf as $i) {
					if (is_array($i) && isset($i['name'])) {
						$tname = $i['name'] . '.';
						break;
					}
				}

				$ufname = ($tname === false) ? 'Default' : $tname;

				/* check if config has already been set (or _config.php has been run a second time) */
				if (isset(self::$ga_configs[$ufname])) {
					/* no unique name has been specified for additional tracker */
					if (self::$ga_configs[$ufname] != $conf[1]) {
						trigger_error(
							'GaTracker::add_ga(): ' . $ufname .' Tracker already set, please use unique name eg: ' .
							'AnalyticsJS::add_ga("create", "UA-12345679-1", "auto", array("name" => "hehe"));',
							E_USER_WARNING
						);
					}
					return false;
				}
				else {
					self::$ga_configs[$ufname] = $conf[1];
				}

				/* Replace Tracker IDs with fake ones if not in LIVE mode */
				if ($skip_tracking) {
					$conf[1] = preg_replace('/[0-9]{4,}-[0-9]+/', 'DEV-' . self::$tracker_counter++, $conf[1]);
				}

				array_push(self::$tracker_names, $tname);

			}

			foreach ($conf as $i) {
				array_push($args, json_encode($i));
			}

			self::$ga_trackers .= self::$global_name . '(' . implode(',', $args) .');'."\n";
		}

	}


	/*
	 * Generates <head> and inject insertHeadTags() JavaScript code
	 * for tracking if at least one tracking config has been specified
	 * @param null
	 * @return null
	 */
	protected function genUniversalAnalyticsCode() {

		if (count(self::$tracker_names) == 0) {
			return false;
		}

{'fraction_non_alphanumeric': 0.15441381230854914, 'fraction_numerical': 0.005151768309663046, 'mean_word_length': 3.592710997442455, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '72459', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2226, 'n_words': 611}