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