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