-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclass-wc-google-analytics.php
315 lines (281 loc) · 14.1 KB
/
class-wc-google-analytics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
/**
* Google Analytics for WooCommerce
*
* Allows tracking code to be inserted into store pages.
*
* @class WC_Google_Analytics
* @extends WC_Integration
*/
class WC_Google_Analytics extends WC_Integration {
/**
* Returns the proper class based on Gtag settings.
*
* @return WC_Abstract_Google_Analytics_JS
*/
protected function get_tracking_instance() {
return WC_Google_Gtag_JS::get_instance( $this->settings );
}
/**
* Constructor
* Init and hook in the integration.
*/
public function __construct() {
$this->id = 'google_analytics';
$this->method_title = __( 'Google Analytics', 'woocommerce-google-analytics-integration' );
$this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce-google-analytics-integration' );
// Load the settings
$this->init_form_fields();
$this->init_settings();
add_action( 'admin_notices', array( $this, 'universal_analytics_upgrade_notice' ) );
include_once 'class-wc-abstract-google-analytics-js.php';
include_once 'class-wc-google-gtag-js.php';
if ( ! $this->disable_tracking( 'all' ) ) {
$this->get_tracking_instance();
}
// Display a task on "Things to do next section"
add_action( 'init', array( $this, 'add_wc_setup_task' ), 20 );
// Admin Options
add_filter( 'woocommerce_tracker_data', array( $this, 'track_settings' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'show_options_info' ) );
add_action( 'admin_init', array( $this, 'privacy_policy' ) );
// utm_nooverride parameter for Google AdWords
add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );
// Dequeue the WooCommerce Blocks Google Analytics integration,
// not to let it register its `gtag` function so that we could provide a more detailed configuration.
add_action(
'wp_enqueue_scripts',
function () {
wp_dequeue_script( 'wc-blocks-google-analytics' );
}
);
}
/**
* Conditionally display an error notice to the merchant if the stored property ID starts with "UA"
*
* @return void
*/
public function universal_analytics_upgrade_notice() {
if ( 'ua' === substr( strtolower( $this->get_option( 'ga_id' ) ), 0, 2 ) ) {
printf(
'<div class="%1$s"><p>%2$s</p></div>',
'notice notice-error',
sprintf(
/* translators: 1) URL for Google documentation on upgrading from UA to GA4 2) URL to WooCommerce Google Analytics settings page */
esc_html__( 'Your website is configured to use Universal Analytics which Google retired in July of 2023. Update your account using the %1$ssetup assistant%2$s and then update your %3$sWooCommerce settings%4$s.', 'woocommerce-google-analytics-integration' ),
'<a href="https://support.google.com/analytics/answer/9744165" target="_blank">',
'</a>',
'<a href="/wp-admin/admin.php?page=wc-settings&tab=integration§ion=google_analytics">',
'</a>'
)
);
}
}
/**
* Tells WooCommerce which settings to display under the "integration" tab
*/
public function init_form_fields() {
$this->form_fields = array(
'ga_product_identifier' => array(
'title' => __( 'Product Identification', 'woocommerce-google-analytics-integration' ),
'description' => __( 'Specify how your products will be identified to Google Analytics. Changing this setting may cause issues with historical data if a product was previously identified using a different structure.', 'woocommerce-google-analytics-integration' ),
'type' => 'select',
'options' => array(
'product_id' => __( 'Product ID', 'woocommerce-google-analytics-integration' ),
'product_sku' => __( 'Product SKU with prefixed (#) ID as fallback', 'woocommerce-google-analytics-integration' ),
),
// If the option is not set then the product SKU is used as default for existing installations
'default' => 'product_sku',
),
'ga_id' => array(
'title' => __( 'Google Analytics Tracking ID', 'woocommerce-google-analytics-integration' ),
'description' => __( 'Log into your Google Analytics account to find your ID. e.g. <code>GT-XXXXX</code> or <code>G-XXXXX</code>', 'woocommerce-google-analytics-integration' ),
'type' => 'text',
'placeholder' => 'GT-XXXXX',
'default' => get_option( 'woocommerce_ga_id' ), // Backwards compat
),
'ga_support_display_advertising' => array(
'label' => __( '"Display Advertising" Support', 'woocommerce-google-analytics-integration' ),
/* translators: Read more link */
'description' => sprintf( __( 'Set the Google Analytics code to support Display Advertising. %1$sRead more about Display Advertising%2$s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/2700409" target="_blank">', '</a>' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => get_option( 'woocommerce_ga_support_display_advertising' ) ? get_option( 'woocommerce_ga_support_display_advertising' ) : 'yes', // Backwards compat
),
'ga_404_tracking_enabled' => array(
'label' => __( 'Track 404 (Not found) Errors', 'woocommerce-google-analytics-integration' ),
/* translators: Read more link */
'description' => sprintf( __( 'Enable this to find broken or dead links. An "Event" with category "Error" and action "404 Not Found" will be created in Google Analytics for each incoming pageview to a non-existing page. By setting up a "Custom Goal" for these events within Google Analytics you can find out where broken links originated from (the referrer). %1$sRead how to set up a goal%2$s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/1032415" target="_blank">', '</a>' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_linker_allow_incoming_enabled' => array(
'label' => __( 'Accept Incoming Linker Parameters', 'woocommerce-google-analytics-integration' ),
'description' => __( 'Enabling this option will allow incoming linker parameters from other websites.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'no',
),
'ga_ecommerce_tracking_enabled' => array(
'title' => __( 'Event Tracking', 'woocommerce-google-analytics-integration' ),
'label' => __( 'Purchase Transactions', 'woocommerce-google-analytics-integration' ),
'description' => __( 'This requires a payment gateway that redirects to the thank you/order received page after payment. Orders paid with gateways which do not do this will not be tracked.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => 'start',
'default' => get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'yes', // Backwards compat
),
'ga_event_tracking_enabled' => array(
'label' => __( 'Add to Cart Events', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_enhanced_remove_from_cart_enabled' => array(
'label' => __( 'Remove from Cart Events', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_enhanced_product_impression_enabled' => array(
'label' => __( 'Product Impressions from Listing Pages', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_enhanced_product_click_enabled' => array(
'label' => __( 'Product Clicks from Listing Pages', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_enhanced_product_detail_view_enabled' => array(
'label' => __( 'Product Detail Views', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_enhanced_checkout_process_enabled' => array(
'label' => __( 'Checkout Process Initiated', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_linker_cross_domains' => array(
'title' => __( 'Cross Domain Tracking', 'woocommerce-google-analytics-integration' ),
/* translators: Read more link */
'description' => sprintf( __( 'Add a comma separated list of domains for automatic linking. %1$sRead more about Cross Domain Measurement%2$s', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/7476333" target="_blank">', '</a>' ),
'type' => 'text',
'placeholder' => 'example.com, example.net',
'default' => '',
),
);
}
/**
* Shows some additional help text after saving the Google Analytics settings
*/
public function show_options_info() {
$this->method_description .= "<div class='notice notice-info'><p>" . __( 'Please allow Google Analytics 24 hours to start displaying results.', 'woocommerce-google-analytics-integration' ) . '</p></div>';
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['woocommerce_google_analytics_ga_ecommerce_tracking_enabled'] ) && true === (bool) $_REQUEST['woocommerce_google_analytics_ga_ecommerce_tracking_enabled'] ) {
$this->method_description .= "<div class='notice notice-info'><p>" . __( 'Please note, for transaction tracking to work properly, you will need to use a payment gateway that redirects the customer back to a WooCommerce order received/thank you page.', 'woocommerce-google-analytics-integration' ) . '</div>';
}
}
/**
* Hooks into woocommerce_tracker_data and tracks some of the analytic settings (just enabled|disabled status)
* only if you have opted into WooCommerce tracking
* https://woo.com/usage-tracking/
*
* @param array $data Current WC tracker data.
* @return array Updated WC Tracker data.
*/
public function track_settings( $data ) {
$settings = $this->settings;
$data['wc-google-analytics'] = array(
'support_display_advertising' => $settings['ga_support_display_advertising'],
'ga_404_tracking_enabled' => $settings['ga_404_tracking_enabled'],
'ecommerce_tracking_enabled' => $settings['ga_ecommerce_tracking_enabled'],
'event_tracking_enabled' => $settings['ga_event_tracking_enabled'],
'plugin_version' => WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION,
'linker_allow_incoming_enabled' => empty( $settings['ga_linker_allow_incoming_enabled'] ) ? 'no' : 'yes',
'linker_cross_domains' => $settings['ga_linker_cross_domains'],
);
// ID prefix, blank, or X for unknown
$prefix = strstr( strtoupper( $settings['ga_id'] ), '-', true );
if ( in_array( $prefix, array( 'UA', 'G', 'GT' ), true ) || empty( $prefix ) ) {
$data['wc-google-analytics']['ga_id'] = $prefix;
} else {
$data['wc-google-analytics']['ga_id'] = 'X';
}
return $data;
}
/**
* Add suggested privacy policy content
*
* @return void
*/
public function privacy_policy() {
$policy_text = sprintf(
/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
esc_html__( 'By using this extension, you may be storing personal data or sharing data with an external service. %1$sLearn more about what data is collected by Google and what you may want to include in your privacy policy%2$s.', 'woocommerce-google-analytics-integration' ),
'<a href="https://support.google.com/analytics/answer/7318509" target="_blank">',
'</a>'
);
// As the extension doesn't offer suggested privacy policy text, the button to copy it is hidden.
$content = '
<p class="privacy-policy-tutorial">' . $policy_text . '</p>
<style>#privacy-settings-accordion-block-woocommerce-google-analytics-integration .privacy-settings-accordion-actions { display: none }</style>';
wp_add_privacy_policy_content( 'Google Analytics for WooCommerce', wpautop( $content, false ) );
}
/**
* Check if tracking is disabled
*
* @param string $type The setting to check
* @return bool True if tracking for a certain setting is disabled
*/
private function disable_tracking( $type ) {
return is_admin() || current_user_can( 'manage_options' ) || empty( $this->settings['ga_id'] ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type );
}
/**
* Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
*
* @param string $return_url WooCommerce Return URL
* @return string URL
*/
public function utm_nooverride( $return_url ) {
// We don't know if the URL already has the parameter so we should remove it just in case
$return_url = remove_query_arg( 'utm_nooverride', $return_url );
// Now add the utm_nooverride query arg to the URL
$return_url = add_query_arg( 'utm_nooverride', '1', $return_url );
return esc_url( $return_url, null, 'db' );
}
/**
* Check if the Google Analytics Tracking ID has been set up.
*
* @since 1.5.17
*
* @return bool Whether the Google Analytics setup is completed.
*/
public function is_setup_complete() {
return (bool) $this->get_option( 'ga_id' );
}
/**
* Adds the setup task to the Tasklists.
*
* @since 1.5.17
*/
public function add_wc_setup_task() {
require_once 'class-wc-google-analytics-task.php';
TaskLists::add_task(
'extended',
new WC_Google_Analytics_Task(
TaskLists::get_list( 'extended' )
)
);
}
}