PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/carloskikea/helpet
PHP | 134 lines | 72 code | 16 blank | 46 comment | 13 complexity | c4319f9a70833e4dcd0a3ed4816d4ac9 MD5 | raw file
  1<?php
  2/**
  3 * Events JS class.
  4 *
  5 * @since 6.0.0
  6 *
  7 * @package MonsterInsights
  8 * @subpackage  Events
  9 * @author  Chris Christoff
 10 */
 11
 12// Exit if accessed directly
 13if ( ! defined( 'ABSPATH' ) ) {
 14	exit;
 15}
 16
 17class MonsterInsights_Analytics_Events {
 18	
 19	/**
 20	 * Holds the name of the events type.
 21	 *
 22	 * @since 6.0.0
 23	 * @access public
 24	 *
 25	 * @var string $name Name of the events type.
 26	 */
 27	public $name = 'js';
 28
 29	/**
 30	 * Version of the events class.
 31	 *
 32	 * @since 6.0.0
 33	 * @access public
 34	 *
 35	 * @var string $version Version of the events class.
 36	 */
 37	public $version = '1.0.0';
 38
 39	/**
 40	 * Primary class constructor.
 41	 *
 42	 * @since 6.0.0
 43	 * @access public
 44	 */
 45	public function __construct() {
 46		add_action( 'wp_enqueue_scripts', array( $this, 'output_javascript' ), 9 ); 
 47		//add_action( 'login_head', array( $this, 'output_javascript' ), 9 );
 48	}
 49
 50	/**
 51	 * Outputs the Javascript for JS tracking on the page.
 52	 *
 53	 * @since 6.0.0
 54	 * @access public
 55	 * 
 56	 * @return string
 57	 */
 58	public function output_javascript() {
 59		// What should we track downloads as?
 60		$track_download_as = monsterinsights_get_option( 'track_download_as', '' );
 61		$track_download_as = $track_download_as === 'pageview' ? 'pageview' : 'event';
 62
 63		// What label should be used for internal links?
 64		$internal_label = monsterinsights_get_option( 'track_internal_as_label', 'int' );
 65		if ( ! empty( $internal_label ) && is_string( $internal_label ) ) {
 66			$internal_label = trim( $internal_label, ',' );
 67			$internal_label = trim( $internal_label );
 68		}
 69
 70		// If the label is empty, set a default value
 71		if ( empty( $internal_label ) ) {
 72			$internal_label = 'int';
 73		}
 74
 75		$internal_label = esc_js( $internal_label );
 76
 77		// Get inbound as outbound to track
 78		$inbound_paths = monsterinsights_get_option( 'track_internal_as_outbound','' );
 79		$inbound_paths = explode( ',', $inbound_paths );
 80		if ( ! is_array( $inbound_paths ) ) {
 81			$inbound_paths = array( $inbound_paths );
 82		}
 83		$i = 0;
 84		foreach ( $inbound_paths as $path ){
 85			$inbound_paths[ $i ] = esc_js( trim( $path ) );
 86			$i++;
 87		}
 88
 89		$inbound_paths = implode( ",", $inbound_paths );
 90
 91		// Get download extensions to track
 92		$download_extensions = monsterinsights_get_option( 'extensions_of_files', '' );
 93		$download_extensions = explode( ',', str_replace( '.', '', $download_extensions ) );
 94		if ( ! is_array( $download_extensions ) ) {
 95			$download_extensions = array( $download_extensions );
 96		}
 97		$i = 0;
 98		foreach( $download_extensions as $extension ){
 99			$download_extensions[ $i ] = esc_js( trim( $extension ) );
100			$i++;
101		}
102
103		$download_extensions = implode( ",", $download_extensions );
104
105		$is_debug_mode     =  monsterinsights_is_debug_mode();
106		if ( current_user_can( 'manage_options' ) && $is_debug_mode ) {
107			$is_debug_mode = 'true';
108		} else {
109			$is_debug_mode = 'false';
110		}
111
112		$hash_tracking = monsterinsights_get_option( 'hash_tracking', false ) ? 'true' : 'false';
113
114		$suffix = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
115		if ( ! file_exists( MONSTERINSIGHTS_PLUGIN_DIR . 'assets/js/frontend.min.js' ) ) {
116			$suffix = '';
117		}
118		wp_enqueue_script( 'monsterinsights-frontend-script', plugins_url( 'assets/js/frontend' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), false );
119		wp_localize_script(
120			'monsterinsights-frontend-script',
121			'monsterinsights_frontend',
122			array(
123				'js_events_tracking'  => 'true',
124				'is_debug_mode' 	  => $is_debug_mode,
125				'download_extensions' => $download_extensions, /* Let's get the extensions to track */
126				'inbound_paths'       => $inbound_paths, /* Let's get the internal paths to track */
127				'home_url'            => home_url(), /* Let's get the url to compare for external/internal use */
128				'track_download_as'   => $track_download_as, /* should downloads be tracked as events or pageviews */
129				'internal_label'      => $internal_label, /* What is the prefix for internal-as-external links */
130				'hash_tracking'       => $hash_tracking, /* Should hash track */
131			)
132		);
133	}
134}