PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.6
Jetpack – WP Security, Backup, Speed, & Growth v8.6
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / google-analytics / wp-google-analytics.php

wp-google-analytics.php in Jetpack – WP Security, Backup, Speed, & Growth 8.6, at modules/google-analytics/wp-google-analytics.php

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