PageRenderTime 34ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/google-analytics-for-wordpress/includes/frontend/tracking/class-tracking-analytics.php

https://bitbucket.org/carloskikea/helpet
PHP | 326 lines | 237 code | 28 blank | 61 comment | 45 complexity | 2ac7e602ad9e472cf022bcf042fdb892 MD5 | raw file
  1<?php
  2/**
  3 * Tracking analytics.js class.
  4 *
  5 * @since 6.0.0
  6 *
  7 * @package MonsterInsights
  8 * @author  Chris Christoff
  9 */
 10
 11// Exit if accessed directly
 12if ( ! defined( 'ABSPATH' ) ) {
 13	exit;
 14}
 15
 16class MonsterInsights_Tracking_Analytics extends MonsterInsights_Tracking_Abstract {
 17	
 18	/**
 19	 * Holds the name of the tracking type.
 20	 *
 21	 * @since 6.0.0
 22	 * @access public
 23	 *
 24	 * @var string $name Name of the tracking type.
 25	 */
 26	public $name = 'analytics';
 27
 28	/**
 29	 * Version of the tracking class.
 30	 *
 31	 * @since 6.0.0
 32	 * @access public
 33	 *
 34	 * @var string $version Version of the tracking class.
 35	 */
 36	public $version = '1.0.0';
 37
 38	/**
 39	 * Primary class constructor.
 40	 *
 41	 * @since 6.0.0
 42	 * @access public
 43	 */
 44	public function __construct() {
 45		
 46	}
 47
 48	/**
 49	 * Get frontend tracking options.
 50	 *
 51	 * This function is used to return an array of parameters
 52	 * for the frontend_output() function to output. These are 
 53	 * generally dimensions and turned on GA features.
 54	 *
 55	 * @since 6.0.0
 56	 * @access public
 57	 *
 58	 * @return array Array of the options to use.
 59	 */
 60	public function frontend_tracking_options( ) {
 61		global $wp_query;
 62		$options = array();
 63
 64		$ua_code = monsterinsights_get_ua_to_output();
 65		if ( empty( $ua_code ) ) {
 66			return $options;
 67		}
 68
 69		$track_user = monsterinsights_track_user();
 70
 71		if ( ! $track_user ) {
 72			$options['create'] = "'create', '" . esc_js( $ua_code ) . "', '" . esc_js( 'auto' ) . "'";
 73			$options['forceSSL'] = "'set', 'forceSSL', true";
 74			$options['send'] = "'send','pageview'";
 75			return $options;
 76		}
 77
 78		$domain = esc_attr( monsterinsights_get_option( 'subdomain_tracking', 'auto' ) );
 79
 80		$allow_linker = monsterinsights_get_option( 'add_allow_linker', false );
 81		$allow_anchor = monsterinsights_get_option( 'allow_anchor', false );
 82
 83
 84		$create = array();
 85		if ( $allow_anchor ) {
 86			$create['allowAnchor'] = true;
 87		}
 88
 89		if ( $allow_linker ) {
 90			$create['allowLinker'] = true;
 91		}
 92
 93		if ( class_exists( 'MonsterInsights_AMP' ) ) {
 94			$create['useAmpClientId'] = true;
 95		}
 96
 97		$create = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_create', $create );
 98
 99		if ( $create && ! empty( $create ) && is_array( $create ) ) {
100			$create = json_encode( $create );
101			$create = str_replace( '"', "'",  $create );
102			$options['create'] = "'create', '" . esc_js( $ua_code ). "', '" . esc_js( $domain ) . "', " . $create;
103		} else {
104			$options['create'] = "'create', '" . esc_js( $ua_code ) . "', '" . esc_js( $domain ) . "'";
105		}
106
107		$options['forceSSL'] = "'set', 'forceSSL', true";
108
109		if ( monsterinsights_get_option( 'custom_code', false ) ) {
110			// Add custom code to the view
111			$options['custom_code'] = array(
112				'type'  => 'custom_code',
113				'value' => stripslashes( monsterinsights_get_option( 'custom_code', '' ) ),
114			);
115		}
116
117		// Anonymous data
118		if ( monsterinsights_get_option( 'anonymize_ips', false ) ) {
119			$options['anonymize_ips'] = "'set', 'anonymizeIp', true";
120		}
121
122		$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_scripts', $options );
123
124		// add demographics
125		if ( monsterinsights_get_option( 'demographics', false ) ) {
126			$options['demographics'] = "'require', 'displayfeatures'";
127		}
128
129		// Check for Enhanced link attribution
130		if ( monsterinsights_get_option( 'enhanced_link_attribution', false ) ) {
131			$options['enhanced_link_attribution'] = "'require', 'linkid', 'linkid.js'";
132		}
133
134		$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_pageview', $options );
135		$options = apply_filters( 'monsterinsights_frontend_tracking_options_before_pageview', $options, $this->name, $this->version );
136
137		if ( is_404() ) {
138			if ( monsterinsights_get_option( 'hash_tracking', false ) ) {
139				$options['send'] = "'send','pageview','/404.html?page=' + document.location.pathname + document.location.search + location.hash + '&from=' + document.referrer";
140			} else {
141				$options['send'] = "'send','pageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer";
142			}
143		} else if ( $wp_query->is_search ) {
144			$pushstr = "'send','pageview','/?s=";
145			if ( (int) $wp_query->found_posts === 0 ) {
146				$options['send'] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'";
147			} else if ( (int) $wp_query->found_posts === 1 ) {
148				$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'";
149			} else if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) {
150				$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'";
151			} else {
152				$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'";
153			}
154		} else if ( monsterinsights_get_option( 'hash_tracking', false ) ) {
155			$options['send'] = "'send','pageview', location.pathname + location.search + location.hash";
156		} else {
157			$options['send'] = "'send','pageview'";
158		}
159
160		$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_end', $options );
161		return $options;
162	}
163
164	/**
165	 * Get frontend output.
166	 *
167	 * This function is used to return the Javascript
168	 * to output in the head of the page for the given
169	 * tracking method.
170	 *
171	 * @since 6.0.0
172	 * @access public
173	 *
174	 * @return string Javascript to output.
175	 */
176	public function frontend_output( ) {
177		$options        = $this->frontend_tracking_options();
178		$is_debug_mode  =  monsterinsights_is_debug_mode();
179		$src     	    = apply_filters( 'monsterinsights_frontend_output_analytics_src', '//www.google-analytics.com/analytics.js' );
180		if ( current_user_can( 'manage_options' ) && $is_debug_mode ) {
181			$src       = apply_filters( 'monsterinsights_frontend_output_analytics_src', '//www.google-analytics.com/analytics_debug.js' );
182		}
183		$compat     	= monsterinsights_get_option( 'gatracker_compatibility_mode', false );
184		$compat    	 	= $compat ? 'window.ga = __gaTracker;' : '';
185		$track_user 	= monsterinsights_track_user();
186		$ua         	= monsterinsights_get_ua();
187		$output     	= '';
188		$reason     	= '';
189		$cookie_notice  = class_exists( 'Cookie_Notice' ) && monsterinsights_get_option( 'cookie_notice', false );
190		ob_start();
191		?>
192<!-- This site uses the Google Analytics by MonsterInsights plugin v<?php echo MONSTERINSIGHTS_VERSION; ?> - Using Analytics tracking - https://www.monsterinsights.com/ -->
193<?php if ( ! $track_user ) {
194	if ( empty( $ua ) ) {
195		$reason = __( 'Note: MonsterInsights is not currently configured on this site. The site owner needs to authenticate with Google Analytics in the MonsterInsights settings panel.', 'google-analytics-for-wordpress' );
196	    $output .=  '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL;
197	} else if ( current_user_can( 'monsterinsights_save_settings' ) ) {
198		$reason = __( 'Note: MonsterInsights does not track you as a logged in site administrator to prevent site owners from accidentally skewing their own Google Analytics data.'. PHP_EOL . 'If you are testing Google Analytics code, please do so either logged out or in the private browsing/incognito mode of your web browser.', 'google-analytics-for-wordpress' );
199	    $output .=  '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL;
200	} else if ( class_exists( 'Cookie_Notice' ) ) {
201		$reason = __( 'Note: You have not accepted the Cookie Notice.', 'google-analytics-for-wordpress' );
202	    $output .=  '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL;
203	} else {
204		$reason = __( 'Note: The site owner has disabled Google Analytics tracking for your user role.', 'google-analytics-for-wordpress' );
205	    $output .=  '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL;
206	}
207	echo $output;
208} ?>
209<?php if ( $ua ) { ?>
210<script type="text/javascript" data-cfasync="false">
211	var mi_track_user = <?php echo ( $track_user ? 'true' : 'false' ); ?>;
212	<?php if ( $cookie_notice ) { ?>
213	/* Compatibility with Cookie Notice */
214	if ( typeof setCookieNotice === 'function' ) {
215	  /* See if body has cookies-not-accepted */
216	    var mi_body_el = document.getElementsByTagName("BODY")[0];
217	   if ( (' ' + mi_body_el.className + ' ').indexOf(' ' + 'cookies-not-accepted' + ' ') > -1 ) {
218	        mi_track_user = false;
219	  }
220	}
221	<?php } ?>
222
223<?php if ( $this->should_do_optout() ) { ?>
224	var disableStr = 'ga-disable-<?php echo monsterinsights_get_ua(); ?>';
225
226	/* Function to detect opted out users */
227	function __gaTrackerIsOptedOut() {
228		return document.cookie.indexOf(disableStr + '=true') > -1;
229	}
230
231	/* Disable tracking if the opt-out cookie exists. */
232	if ( __gaTrackerIsOptedOut() ) {
233		window[disableStr] = true;
234	}
235
236	/* Opt-out function */
237	function __gaTrackerOptout() {
238	  document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
239	  window[disableStr] = true;
240	}
241	<?php } ?>
242
243	if ( mi_track_user ) {
244		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
245			(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
246			m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
247		})(window,document,'script','<?php echo $src; ?>','__gaTracker');
248
249<?php
250	if ( current_user_can( 'manage_options' ) && $is_debug_mode ) {
251		echo 'window.ga_debug = {trace: true};';
252	}
253
254	echo $compat;
255
256	if ( count( $options ) >= 1 ) {
257		foreach ( $options as $item ) {
258			if ( ! is_array( $item ) ) {
259				echo '		__gaTracker(' . $item . ");\n";
260			} else if ( ! empty ( $item['value'] ) ) {
261				echo '	' . $item['value'] . "\n";
262			}
263		}
264	}
265	?>
266	} else {
267<?php if ( $this->should_do_optout() ) { ?>
268		console.log( "<?php echo esc_js( $reason );?>" );
269		(function() {
270			/* https://developers.google.com/analytics/devguides/collection/analyticsjs/ */
271			var noopfn = function() {
272				return null;
273			};
274			var noopnullfn = function() {
275				return null;
276			};
277			var Tracker = function() {
278				return null;
279			};
280			var p = Tracker.prototype;
281			p.get = noopfn;
282			p.set = noopfn;
283			p.send = noopfn;
284			var __gaTracker = function() {
285				var len = arguments.length;
286				if ( len === 0 ) {
287					return;
288				}
289				var f = arguments[len-1];
290				if ( typeof f !== 'object' || f === null || typeof f.hitCallback !== 'function' ) {
291					console.log( '<?php echo esc_js( __('Not running function', 'google-analytics-for-wordpress' ) );?> __gaTracker(' + arguments[0] + " ....) <?php echo esc_js( sprintf( __( "because you're not being tracked. %s", 'google-analytics-for-wordpress' ), $reason ) );?>");
292					return;
293				}
294				try {
295					f.hitCallback();
296				} catch (ex) {
297
298				}
299			};
300			__gaTracker.create = function() {
301				return new Tracker();
302			};
303			__gaTracker.getByName = noopnullfn;
304			__gaTracker.getAll = function() {
305				return [];
306			};
307			__gaTracker.remove = noopfn;
308			window['__gaTracker'] = __gaTracker;
309		})();
310	<?php } ?>
311	}
312</script>
313<?php } else {  ?>
314<!-- No UA code set -->
315<?php } ?>
316<!-- / Google Analytics by MonsterInsights -->
317<?php
318		$output = ob_get_contents();
319		ob_end_clean();
320		return $output;
321	}
322
323	public function should_do_optout() {
324		return ! ( defined( 'MI_NO_TRACKING_OPTOUT' ) && MI_NO_TRACKING_OPTOUT );
325	}
326}