Sin descripción

wp-google-analytics.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. Copyright 2006 Aaron D. Campbell (email : wp_plugins@xavisys.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /**
  17. * Jetpack_Google_Analytics is the class that handles ALL of the plugin functionality.
  18. * It helps us avoid name collisions
  19. * https://codex.wordpress.org/Writing_a_Plugin#Avoiding_Function_Name_Collisions
  20. */
  21. if ( ! defined( 'ABSPATH' ) ) {
  22. exit;
  23. }
  24. require_once( plugin_basename( 'classes/wp-google-analytics-utils.php' ) );
  25. require_once( plugin_basename( 'classes/wp-google-analytics-options.php' ) );
  26. require_once( plugin_basename( 'classes/wp-google-analytics-legacy.php' ) );
  27. require_once( plugin_basename( 'classes/wp-google-analytics-universal.php' ) );
  28. require_once plugin_basename( 'classes/class-jetpack-google-amp-analytics.php' );
  29. class Jetpack_Google_Analytics {
  30. /**
  31. * @var Jetpack_Google_Analytics - Static property to hold our singleton instance
  32. */
  33. static $instance = false;
  34. /**
  35. * @var Static property to hold concrete analytics impl that does the work (universal or legacy)
  36. */
  37. static $analytics = false;
  38. /**
  39. * This is our constructor, which is private to force the use of get_instance()
  40. *
  41. * @return void
  42. */
  43. private function __construct() {
  44. // At this time, we only leverage universal analytics when enhanced ecommerce is selected and WooCommerce is active.
  45. // Otherwise, don't bother emitting the tracking ID or fetching analytics.js
  46. if ( class_exists( 'WooCommerce' ) && Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) {
  47. self::$analytics = new Jetpack_Google_Analytics_Universal();
  48. new Jetpack_Google_AMP_Analytics();
  49. } else {
  50. self::$analytics = new Jetpack_Google_Analytics_Legacy();
  51. }
  52. }
  53. /**
  54. * Function to instantiate our class and make it a singleton
  55. */
  56. public static function get_instance() {
  57. if ( ! self::$instance ) {
  58. self::$instance = new self;
  59. }
  60. return self::$instance;
  61. }
  62. /**
  63. * Add amp-analytics tags.
  64. *
  65. * @param array $analytics_entries An associative array of the analytics entries.
  66. *
  67. * @return array
  68. */
  69. public static function amp_analytics_entries( $analytics_entries ) {
  70. if ( ! is_array( $analytics_entries ) ) {
  71. $analytics_entries = array();
  72. }
  73. $amp_tracking_codes = static::get_amp_tracking_codes( $analytics_entries );
  74. $jetpack_account = Jetpack_Google_Analytics_Options::get_tracking_code();
  75. // Bypass tracking codes already set on AMP plugin.
  76. if ( in_array( $jetpack_account, $amp_tracking_codes, true ) ) {
  77. return $analytics_entries;
  78. }
  79. $config_data = wp_json_encode(
  80. array(
  81. 'vars' => array(
  82. 'account' => Jetpack_Google_Analytics_Options::get_tracking_code(),
  83. ),
  84. 'triggers' => array(
  85. 'trackPageview' => array(
  86. 'on' => 'visible',
  87. 'request' => 'pageview',
  88. ),
  89. ),
  90. )
  91. );
  92. // Generate a hash string to uniquely identify this entry.
  93. $entry_id = substr( md5( 'googleanalytics' . $config_data ), 0, 12 );
  94. $analytics_entries[ $entry_id ] = array(
  95. 'type' => 'googleanalytics',
  96. 'config' => $config_data,
  97. );
  98. return $analytics_entries;
  99. }
  100. /**
  101. * Get AMP tracking codes.
  102. *
  103. * @param array $analytics_entries The codes available for AMP.
  104. *
  105. * @return array
  106. */
  107. protected static function get_amp_tracking_codes( $analytics_entries ) {
  108. $entries = array_column( $analytics_entries, 'config' );
  109. $accounts = array();
  110. foreach ( $entries as $entry ) {
  111. $entry = json_decode( $entry );
  112. if ( ! empty( $entry->vars->account ) ) {
  113. $accounts[] = $entry->vars->account;
  114. }
  115. }
  116. return $accounts;
  117. }
  118. }
  119. global $jetpack_google_analytics;
  120. $jetpack_google_analytics = Jetpack_Google_Analytics::get_instance();