MonsterInsights_Events_JS

The Google Analytics for WordPress by MonsterInsights MonsterInsights Events JS class.

Defined (1)

The class is defined in the following location(s).

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