Google Analytics - Version 2.4.0

Version Description

  • Add GDPR compliance tool integration.
  • Add Demographic data chart option.
  • Fix ST terms agreement.
Download this release

Release Info

Developer ShareThis
Plugin Icon wp plugin Google Analytics
Version 2.4.0
Comparing to
See all releases

Code changes from version 2.3.8 to 2.4.0

assets/images/demo-ad.png ADDED
Binary file
assets/images/gdpr-ex.png ADDED
Binary file
class/Ga_Admin.php CHANGED
@@ -2,780 +2,968 @@
2
 
3
  class Ga_Admin {
4
 
5
- const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
- const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
- const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
- const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
- const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
- const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
- const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
- const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
- const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
- const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
- const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
- const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
- const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
- const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
- const MIN_WP_VERSION = '3.8';
20
- const GA_SHARETHIS_API_ALIAS = 'sharethis';
21
- const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
22
- const GA_HEARTBEAT_API_CACHE_UPDATE = false;
23
-
24
- /**
25
- * Instantiate API client.
26
- *
27
- * @return Ga_Lib_Google_Api_Client|null
28
- */
29
- public static function api_client( $type = '' ) {
30
- if ( self::GA_SHARETHIS_API_ALIAS === $type ) {
31
- $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
32
- } else {
33
- $instance = Ga_Lib_Google_Api_Client::get_instance();
34
- $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
35
- try {
36
- if ( ! empty( $token ) ) {
37
- $token = json_decode( $token, true );
38
- $instance->set_access_token( $token );
39
- }
40
- } catch ( Exception $e ) {
41
- Ga_Helper::ga_oauth_notice( $e->getMessage() );
42
- }
43
- }
44
-
45
- return $instance;
46
- }
47
-
48
- /**
49
- * Initializes plugin's options during plugin activation process.
50
- */
51
- public static function activate_googleanalytics() {
52
- add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
53
- add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
54
- add_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, false );
55
- add_option( self::GA_HIDE_TERMS_OPTION_NAME, false );
56
- add_option( self::GA_VERSION_OPTION_NAME );
57
- add_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
58
- add_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
59
- add_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
60
- add_option( self::GA_SELECTED_ACCOUNT );
61
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
62
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
63
- add_option( self::GA_DISABLE_ALL_FEATURES );
64
- Ga_Cache::add_cache_options();
65
- }
66
-
67
- /**
68
- * Deletes plugin's options during plugin activation process.
69
- */
70
- public static function deactivate_googleanalytics() {
71
- delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
72
- delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
73
- delete_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
74
- delete_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
75
- delete_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
76
- delete_option( self::GA_SELECTED_ACCOUNT );
77
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
78
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
79
- delete_option( self::GA_DISABLE_ALL_FEATURES );
80
- delete_option( Ga_SupportLogger::LOG_OPTION );
81
- Ga_Cache::delete_cache_options();
82
- }
83
-
84
- /**
85
- * Deletes plugin's options during plugin uninstallation process.
86
- */
87
- public static function uninstall_googleanalytics() {
88
- delete_option( self::GA_SHARETHIS_TERMS_OPTION_NAME );
89
- delete_option( self::GA_HIDE_TERMS_OPTION_NAME );
90
- delete_option( self::GA_VERSION_OPTION_NAME );
91
- delete_option( self::GA_SHARETHIS_PROPERTY_ID );
92
- delete_option( self::GA_SHARETHIS_PROPERTY_SECRET );
93
- delete_option( self::GA_SHARETHIS_VERIFICATION_RESULT );
94
- }
95
-
96
- /**
97
- * Do actions during plugin load.
98
- */
99
- public static function loaded_googleanalytics() {
100
- self::update_googleanalytics();
101
- }
102
-
103
- /**
104
- * Update hook fires when plugin is being loaded.
105
- */
106
- public static function update_googleanalytics() {
107
- $version = get_option( self::GA_VERSION_OPTION_NAME );
108
- $installed_version = get_option( self::GA_VERSION_OPTION_NAME, '2.3.8' );
109
- $old_property_value = Ga_Helper::get_option( 'web_property_id' );
110
-
111
- if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'lt' ) ) {
112
-
113
- if ( ! empty( $old_property_value ) ) {
114
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME, $old_property_value );
115
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, 1 );
116
- delete_option( 'web_property_id' );
117
- }
118
- }
119
-
120
- update_option( self::GA_VERSION_OPTION_NAME, GOOGLEANALYTICS_VERSION );
121
- }
122
-
123
- public static function preupdate_exclude_roles( $new_value, $old_value ) {
124
- if ( ! Ga_Helper::are_features_enabled() ) {
125
- return '';
126
- }
127
-
128
- return wp_json_encode( $new_value );
129
- }
130
-
131
- /**
132
- * Pre-update hook for preparing JSON structure.
133
- *
134
- * @param $new_value
135
- * @param $old_value
136
- *
137
- * @return mixed
138
- */
139
- public static function preupdate_selected_account( $new_value, $old_value ) {
140
- $data = null;
141
- if ( ! empty( $new_value ) ) {
142
- $data = explode( '_', $new_value );
143
-
144
- if ( ! empty( $data[1] ) ) {
145
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
146
- }
147
- }
148
-
149
- return wp_json_encode( $data );
150
- }
151
-
152
- public static function preupdate_disable_all_features( $new_value, $old_value ) {
153
- if ( 'on' === $old_value ) {
154
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
155
- }
156
-
157
- return $new_value;
158
- }
159
-
160
- public static function preupdate_optimize_code( $new_value, $old_value ) {
161
- if ( ! empty( $new_value ) ) {
162
- $new_value = sanitize_text_field( wp_unslash( $new_value ) );
163
- }
164
-
165
- return $new_value;
166
- }
167
-
168
- public static function preupdate_ip_anonymization( $new_value, $old_value ) {
169
- return $new_value;
170
- }
171
-
172
- /**
173
- * Registers plugin's settings.
174
- */
175
- public static function admin_init_googleanalytics() {
176
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME );
177
- register_setting( GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME );
178
- register_setting( GA_NAME, self::GA_SELECTED_ACCOUNT );
179
- register_setting( GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
180
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
181
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
182
- register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
183
- register_setting( GA_NAME, 'googleanalytics_optimize_code' );
184
- register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
185
- add_filter( 'pre_update_option_' . self::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
186
- add_filter( 'pre_update_option_' . self::GA_SELECTED_ACCOUNT, 'GA_Admin::preupdate_selected_account', 1, 2 );
187
- add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
188
- add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
189
- }
190
-
191
- /**
192
- * Builds plugin's menu structure.
193
- */
194
- public static function admin_menu_googleanalytics() {
195
- if ( current_user_can( 'manage_options' ) ) {
196
- add_menu_page( 'Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000 );
197
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Dashboard' ), 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics' );
198
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Settings' ), 'manage_options', 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics' );
199
- }
200
- }
201
-
202
- /**
203
- * Prepares and displays plugin's stats page.
204
- */
205
- public static function statistics_page_googleanalytics() {
206
-
207
- if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
208
- return false;
209
- }
210
-
211
- $data = self::get_stats_page();
212
- Ga_View_Core::load(
213
- 'statistics',
214
- array(
215
- 'data' => $data,
216
- )
217
- );
218
-
219
- if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
220
- self::api_client()->add_own_error( '1', __( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
221
- }
222
-
223
- self::display_api_errors();
224
- }
225
-
226
- /**
227
- * Prepares and displays plugin's settings page.
228
- */
229
- public static function options_page_googleanalytics() {
230
-
231
- if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
232
- return false;
233
- }
234
- if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
235
- echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
236
- }
237
- /**
238
- * Keeps data to be extracted as variables in the view.
239
- *
240
- * @var array $data
241
- */
242
- $data = array();
243
-
244
- $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
245
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
246
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
247
- $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
248
-
249
- $roles = Ga_Helper::get_user_roles();
250
- $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
251
-
252
- $tmp = array();
253
- if ( ! empty( $roles ) ) {
254
- foreach ( $roles as $role ) {
255
- $role_id = Ga_Helper::prepare_role_id( $role );
256
- $tmp[] = array(
257
- 'name' => $role,
258
- 'id' => $role_id,
259
- 'checked' => ( ! empty( $saved[ $role_id ] ) && 'on' === $saved[ $role_id ] ),
260
- );
261
- }
262
- }
263
- $data['roles'] = $tmp;
264
-
265
- if ( Ga_Helper::is_authorized() ) {
266
- $data['ga_accounts_selector'] = self::get_accounts_selector();
267
- $data['auth_button'] = self::get_auth_button( 'reauth' );
268
- } else {
269
- $data['popup_url'] = self::get_auth_popup_url();
270
- $data['auth_button'] = self::get_auth_button( 'auth' );
271
- }
272
- $data['debug_modal'] = self::get_debug_modal();
273
- $data['debug_info'] = Ga_SupportLogger::$debug_info;
274
-
275
- if ( ! empty( $_GET['err'] ) ) { // WPCS: CSRF ok.
276
- switch ( $_GET['err'] ) { // WPCS: CSRF ok.
277
- case 1:
278
- $data['error_message'] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
279
- break;
280
- case 2:
281
- $data['error_message'] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
282
- break;
283
- }
284
- }
285
- Ga_View_Core::load(
286
- 'page',
287
- array(
288
- 'data' => $data,
289
- 'tooltip' => Ga_Helper::get_tooltip(),
290
- )
291
- );
292
-
293
- self::display_api_errors();
294
- }
295
-
296
- /**
297
- * Prepares and returns a plugin's URL to be opened in a popup window
298
- * during Google authentication process.
299
- *
300
- * @return mixed
301
- */
302
- public static function get_auth_popup_url() {
303
- return admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_auth' ) ) );
304
- }
305
-
306
- /**
307
- * Prepares and returns Google Account's dropdown code.
308
- *
309
- * @return string
310
- */
311
- public static function get_accounts_selector() {
312
- $selected = Ga_Helper::get_selected_account_data();
313
- $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
314
- if ( ! Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
315
- echo wp_kses_post( Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", 'warning' ) );
316
- }
317
-
318
- return Ga_View_Core::load(
319
- 'ga_accounts_selector',
320
- array(
321
- 'selector' => $selector,
322
- 'selected' => $selected ? implode( '_', $selected ) : null,
323
- 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled(),
324
- ),
325
- true
326
- );
327
- }
328
-
329
- /**
330
- * Adds JS scripts for the settings page.
331
- */
332
- public static function enqueue_ga_scripts() {
333
- wp_register_script(
334
- GA_NAME . '-page-js',
335
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js',
336
- [ 'jquery' ],
337
- GOOGLEANALYTICS_VERSION,
338
- false
339
- );
340
- wp_enqueue_script( GA_NAME . '-page-js' );
341
- }
342
-
343
- /**
344
- * Adds CSS plugin's scripts.
345
- */
346
- public static function enqueue_ga_css() {
347
- wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, GOOGLEANALYTICS_VERSION, 'all' );
348
- wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
349
- wp_enqueue_style( GA_NAME . '-css' );
350
- wp_enqueue_style( GA_NAME . '-additional-css' );
351
- if ( Ga_Helper::is_wp_old() ) {
352
- wp_register_style( GA_NAME . '-old-wp-support-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_old_wp_support.css', false, GOOGLEANALYTICS_VERSION, 'all' );
353
- wp_enqueue_style( GA_NAME . '-old-wp-support-css' );
354
- }
355
- wp_register_style( GA_NAME . '-modal-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_modal.css', false, GOOGLEANALYTICS_VERSION, 'all' );
356
- wp_enqueue_style( GA_NAME . '-modal-css' );
357
- }
358
-
359
- /**
360
- * Enqueues dashboard JS scripts.
361
- */
362
- private static function enqueue_dashboard_scripts() {
363
- wp_register_script(
364
- GA_NAME . '-dashboard-js',
365
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js',
366
- [ 'jquery' ],
367
- GOOGLEANALYTICS_VERSION,
368
- false
369
- );
370
- wp_enqueue_script( GA_NAME . '-dashboard-js' );
371
- }
372
-
373
- /**
374
- * Enqueues plugin's JS and CSS scripts.
375
- */
376
- public static function enqueue_scripts() {
377
- $domain = str_replace('http://','', str_replace('https://', '', str_replace( '/wp-admin/', '', admin_url() )));
378
- $st_prop = get_option(self::GA_SHARETHIS_PROPERTY_ID);
379
- $st_secret = get_option(self::GA_SHARETHIS_PROPERTY_SECRET);
380
-
381
- if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
382
- wp_register_script(
383
- GA_NAME . '-js',
384
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js',
385
- [ 'jquery' ],
386
- GOOGLEANALYTICS_VERSION,
387
- false
388
- );
389
- wp_enqueue_script( GA_NAME . '-js' );
390
-
391
- wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, 1, false );
392
- wp_enqueue_script( 'googlecharts' );
393
-
394
- if ( empty($st_prop) || empty($st_secret) ) {
395
- wp_register_script( 'googlecreateprop', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/googleanalytics_createprop.js', ['jquery', 'wp-util'], time(), false );
396
- wp_enqueue_script('googlecreateprop');
397
- wp_add_inline_script('googlecreateprop', '
398
- var gaNonce = "' . wp_create_nonce('googleanalyticsnonce') . '";
399
- var gasiteURL = "' . $domain . '";
400
- var gaAdminEmail = "' . get_option('admin_email') . '";'
401
- );
402
- }
403
-
404
- self::enqueue_ga_css();
405
- }
406
-
407
- if ( Ga_Helper::is_dashboard_page() ) {
408
- self::enqueue_dashboard_scripts();
409
- }
410
-
411
- if ( Ga_Helper::is_plugin_page() ) {
412
- self::enqueue_ga_scripts();
413
- }
414
- }
415
-
416
- /**
417
- * Prepares plugin's statistics page and return HTML code.
418
- *
419
- * @return string HTML code
420
- */
421
- public static function get_stats_page() {
422
- $chart = null;
423
- $boxes = null;
424
- $labels = null;
425
- $sources = null;
426
- if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && ! Ga_Helper::is_all_feature_disabled() ) {
427
- list( $chart, $boxes, $labels, $sources ) = self::generate_stats_data();
428
- }
429
-
430
- return Ga_Helper::get_chart_page(
431
- 'stats',
432
- array(
433
- 'chart' => $chart,
434
- 'boxes' => $boxes,
435
- 'labels' => $labels,
436
- 'sources' => $sources,
437
- )
438
- );
439
- }
440
-
441
- /**
442
- * Shows plugin's notice on the admin area.
443
- */
444
- public static function admin_notice_googleanalytics() {
445
- if ( ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && Ga_Helper::is_plugin_page() ) || ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && ! get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) ) {
446
- $current_url = Ga_Helper::get_current_url();
447
- $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
448
- Ga_View_Core::load( 'ga_notice', [ 'url' => $url ] );
449
- }
450
-
451
- if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
452
- echo wp_kses_post(
453
- Ga_Helper::ga_wp_notice(
454
- __( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ),
455
- 'warning',
456
- false,
457
- array(
458
- 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
459
- 'label' => __( 'Enable' ),
460
- )
461
- )
462
- );
463
- }
464
- }
465
-
466
- /**
467
- * Prepare required PHP version warning.
468
- * @return string
469
- */
470
- public static function admin_notice_googleanalytics_php_version() {
471
- echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED, 'error' ) );
472
- }
473
-
474
- /**
475
- * Prepare required WP version warning
476
- * @return string
477
- */
478
- public static function admin_notice_googleanalytics_wp_version() {
479
- echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION, 'error' ) );
480
- }
481
-
482
- /**
483
- * Hides plugin's notice
484
- */
485
- public static function admin_notice_hide_googleanalytics() {
486
- update_option( self::GA_HIDE_TERMS_OPTION_NAME, true );
487
- }
488
-
489
- /**
490
- * Adds GA dashboard widget only for administrators.
491
- */
492
- public static function add_dashboard_device_widget() {
493
- if ( Ga_Helper::is_administrator() ) {
494
- wp_add_dashboard_widget( 'ga_dashboard_widget', __( 'Google Analytics Dashboard' ), 'Ga_Helper::add_ga_dashboard_widget' );
495
- }
496
- }
497
-
498
- /**
499
- * Adds plugin's actions
500
- */
501
- public static function add_actions() {
502
- add_action( 'admin_init', 'Ga_Admin::admin_init_googleanalytics' );
503
- add_action( 'admin_menu', 'Ga_Admin::admin_menu_googleanalytics' );
504
- add_action( 'admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts' );
505
- add_action( 'wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget' );
506
- add_action( 'wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change' );
507
- add_action( 'wp_ajax_ga_ajax_hide_review', 'Ga_Admin::ga_ajax_hide_review' );
508
- add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
509
- add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
510
- add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
511
- add_action( 'wp_ajax_set_ga_credentials', 'Ga_Admin::createGAProperty' );
512
- }
513
-
514
- /**
515
- * Runs jobs
516
- *
517
- * @param $response
518
- * @param $screen_id
519
- */
520
- public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
521
-
522
- if ( self::GA_HEARTBEAT_API_CACHE_UPDATE ) {
523
- // Disable cache for ajax request
524
- self::api_client()->set_disable_cache( true );
525
-
526
- // Try to regenerate cache if needed
527
- self::generate_stats_data();
528
- }
529
- }
530
-
531
- /**
532
- * Adds plugin's filters
533
- */
534
- public static function add_filters() {
535
- add_filter( 'plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5 );
536
- }
537
-
538
- /**
539
- * Adds new action links on the plugin list.
540
- *
541
- * @param $actions
542
- * @param $plugin_file
543
- *
544
- * @return mixed
545
- */
546
- public static function ga_action_links( $actions, $plugin_file ) {
547
-
548
- if ( basename( $plugin_file ) === GA_NAME . '.php' ) {
549
- array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . __( 'Settings' ) . '</a>' );
550
- }
551
-
552
- return $actions;
553
- }
554
-
555
- public static function init_oauth() {
556
-
557
- $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
558
-
559
- if ( ! empty( $code ) ) {
560
- Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, '' );
561
-
562
- // Get access token
563
- $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
564
- if ( empty( $response ) ) {
565
- return false;
566
- }
567
- $param = '';
568
- if ( ! self::save_access_token( $response ) ) {
569
- $param = '&err=1';
570
- $errors = self::api_client()->get_errors();
571
- if ( ! empty( $errors ) ) {
572
- foreach ( $errors as $error ) {
573
- if ( 'invalid_grant' === $error['message'] ) {
574
- $param = '&err=2';
575
- }
576
- }
577
- }
578
- } else {
579
- self::api_client()->set_access_token( $response->getData() );
580
- // Get accounts data
581
- $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
582
- self::save_ga_account_summaries( $account_summaries->getData() );
583
- update_option( self::GA_SELECTED_ACCOUNT, '' );
584
- }
585
-
586
- wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
587
- }
588
- }
589
-
590
- /**
591
- * Save access token.
592
- *
593
- * @param Ga_Lib_Api_Response $response
594
- *
595
- * @return boolean
596
- */
597
- public static function save_access_token( $response, $refresh_token = '' ) {
598
- $access_token = $response->getData();
599
- if ( ! empty( $access_token ) ) {
600
- $access_token['created'] = time();
601
- } else {
602
- return false;
603
- }
604
-
605
- if ( ! empty( $refresh_token ) ) {
606
- $access_token['refresh_token'] = $refresh_token;
607
- }
608
-
609
- return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
610
- }
611
-
612
- /**
613
- * Saves Google Analytics account data.
614
- *
615
- * @param $data
616
- *
617
- * @return array
618
- */
619
- public static function save_ga_account_summaries( $data ) {
620
- $return = array();
621
- if ( ! empty( $data['items'] ) ) {
622
- foreach ( $data['items'] as $item ) {
623
- $tmp = array();
624
- $tmp['id'] = $item['id'];
625
- $tmp['name'] = $item['name'];
626
- if ( is_array( $item['webProperties'] ) ) {
627
- foreach ( $item['webProperties'] as $property ) {
628
- $profiles = array();
629
- if ( is_array( $property['profiles'] ) ) {
630
- foreach ( $property['profiles'] as $profile ) {
631
- $profiles[] = array(
632
- 'id' => $profile['id'],
633
- 'name' => $profile['name'],
634
- );
635
- }
636
- }
637
-
638
- $tmp['webProperties'][] = array(
639
- 'internalWebPropertyId' => $property['internalWebPropertyId'],
640
- 'webPropertyId' => $property['id'],
641
- 'name' => $property['name'],
642
- 'profiles' => $profiles,
643
- );
644
- }
645
- }
646
-
647
- $return[] = $tmp;
648
- }
649
-
650
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
651
- } else {
652
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, '' );
653
- }
654
- update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, '' );
655
-
656
- return $return;
657
- }
658
-
659
- /**
660
- * Handle AJAX data for the GA dashboard widget.
661
- */
662
- public static function ga_ajax_data_change() {
663
- if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
664
- $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null; // WPCS: CSRF ok.
665
- $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null; // WPCS: CSRF ok.
666
- echo wp_kses_post( Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true ) );
667
- } else {
668
- echo wp_json_encode( array( 'error' => __( 'Invalid request.' ) ) );
669
- }
670
-
671
- wp_die();
672
- }
673
-
674
- /**
675
- * Displays API error messages.
676
- */
677
- public static function display_api_errors( $alias = '' ) {
678
- $errors = self::api_client( $alias )->get_errors();
679
- if ( ! empty( $errors ) ) {
680
- foreach ( $errors as $error ) {
681
- echo wp_kses_post( Ga_Notice::get_message( $error ) );
682
- }
683
- }
684
- }
685
-
686
- /**
687
- * Gets dashboard data.
688
- *
689
- * @return array
690
- */
691
- public static function generate_stats_data() {
692
- $selected = Ga_Helper::get_selected_account_data( true );
693
-
694
- $query_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'main_chart', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
695
-
696
- $stats_data = self::api_client()->call(
697
- 'ga_api_data',
698
- [ $query_params ]
699
- );
700
-
701
- $boxes_data = self::api_client()->call(
702
- 'ga_api_data',
703
- [ Ga_Stats::get_query( 'boxes', $selected['view_id'] ) ]
704
- );
705
- $sources_data = self::api_client()->call(
706
- 'ga_api_data',
707
- [ Ga_Stats::get_query( 'sources', $selected['view_id'] ) ]
708
- );
709
- $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
710
- $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
711
- $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
712
- unset( $chart['date'] );
713
- $labels = array(
714
- 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
715
- 'thisMonth' => date( 'M d, Y', strtotime( '-29 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
716
- );
717
- $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
718
-
719
- return array( $chart, $boxes, $labels, $sources );
720
- }
721
-
722
- /**
723
- * Returns auth or re-auth button
724
- *
725
- * @return string
726
- */
727
- public static function get_auth_button( $type ) {
728
-
729
- return Ga_View_Core::load(
730
- 'ga_auth_button',
731
- [
732
- 'label' => 'auth' === $type ? 'Authenticate with Google' : 'Re-authenticate with Google',
733
- 'type' => $type,
734
- 'url' => self::get_auth_popup_url(),
735
- 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ),
736
- ],
737
- true
738
- );
739
- }
740
-
741
- /**
742
- * Returns debug modal
743
- *
744
- * @return string
745
- */
746
- public static function get_debug_modal() {
747
-
748
- return Ga_View_Core::load(
749
- 'ga_debug_modal',
750
- [ 'debug_info' => Ga_SupportLogger::$debug_info, 'debug_help_message' => Ga_SupportLogger::$debug_help_message ],
751
- true
752
- );
753
- }
754
-
755
- public static function ga_ajax_hide_review( $post ) {
756
- $error = 0;
757
-
758
- if ( Ga_Controller_Core::verify_nonce( 'ga_ajax_data_change' ) ) {
759
- update_option('googleanalytics-hide-review', true);
760
- }
761
-
762
- wp_send_json_success('hidden');
763
- }
764
-
765
- /**
766
- * New property creation method.
767
- */
768
- public static function createGAProperty() {
769
- check_ajax_referer( 'googleanalyticsnonce', 'nonce' );
770
-
771
- if (! isset($_POST['propid'], $_POST['secret'])) { // WPCS: input var ok.
772
- wp_send_json_error( 'Set credentials failed.' );
773
- }
774
-
775
- $secret = sanitize_text_field( wp_unslash( $_POST['secret'] ) ); // WPCS: input var ok.
776
- $propid = sanitize_text_field( wp_unslash( $_POST['propid'] ) ); // WPCS: input var ok.
777
-
778
- update_option(self::GA_SHARETHIS_PROPERTY_ID, $propid);
779
- update_option(self::GA_SHARETHIS_PROPERTY_SECRET, $secret);
780
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
781
  }
2
 
3
  class Ga_Admin {
4
 
5
+ const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
+ const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
+ const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
+ const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
+ const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
+ const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
+ const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
+ const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
+ const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
+ const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
+ const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
+ const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
+ const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
+ const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
+ const MIN_WP_VERSION = '3.8';
20
+ const GA_SHARETHIS_API_ALIAS = 'sharethis';
21
+ const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
22
+ const GA_HEARTBEAT_API_CACHE_UPDATE = false;
23
+ const NOTICE_SUCCESS = 'success';
24
+ const NOTICE_WARNING = 'warning';
25
+ const NOTICE_ERROR = 'error';
26
+
27
+ /**
28
+ * Instantiate API client.
29
+ *
30
+ * @return Ga_Lib_Google_Api_Client|null
31
+ */
32
+ public static function api_client( $type = '' ) {
33
+ if ( self::GA_SHARETHIS_API_ALIAS === $type ) {
34
+ $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
35
+ } else {
36
+ $instance = Ga_Lib_Google_Api_Client::get_instance();
37
+ $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
38
+ try {
39
+ if ( ! empty( $token ) ) {
40
+ $token = json_decode( $token, true );
41
+ $instance->set_access_token( $token );
42
+ }
43
+ } catch ( Exception $e ) {
44
+ Ga_Helper::ga_oauth_notice( $e->getMessage() );
45
+ }
46
+ }
47
+
48
+ return $instance;
49
+ }
50
+
51
+ /**
52
+ * Initializes plugin's options during plugin activation process.
53
+ */
54
+ public static function activate_googleanalytics() {
55
+ add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
56
+ add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
57
+ add_option( self::GA_VERSION_OPTION_NAME );
58
+ add_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
59
+ add_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
60
+ add_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
61
+ add_option( self::GA_SELECTED_ACCOUNT );
62
+ add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
63
+ add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
64
+ add_option( self::GA_DISABLE_ALL_FEATURES );
65
+ Ga_Cache::add_cache_options();
66
+ }
67
+
68
+ /**
69
+ * Deletes plugin's options during plugin activation process.
70
+ */
71
+ public static function deactivate_googleanalytics() {
72
+ delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
73
+ delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
74
+ delete_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
75
+ delete_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
76
+ delete_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
77
+ delete_option( self::GA_SELECTED_ACCOUNT );
78
+ delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
79
+ delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
80
+ delete_option( self::GA_DISABLE_ALL_FEATURES );
81
+ delete_option( Ga_SupportLogger::LOG_OPTION );
82
+ delete_option('googleanalytics_gdpr_config');
83
+ delete_option('googleanalytics_demographic');
84
+ delete_option('googleanalytics_demo_data');
85
+ delete_option('googleanalytics_demo_date');
86
+ delete_option('googleanalytics_send_data');
87
+ delete_option('googleanalytics_hide_terms');
88
+ delete_option('googleanalytics_sharethis_terms');
89
+ delete_option('googleanalytics_sherethis_property_id');
90
+ delete_option('googleanalytics_sherethis_property_secret');
91
+ delete_option(self::GA_SHARETHIS_TERMS_OPTION_NAME );
92
+ Ga_Cache::delete_cache_options();
93
+ }
94
+
95
+ /**
96
+ * Deletes plugin's options during plugin uninstallation process.
97
+ */
98
+ public static function uninstall_googleanalytics() {
99
+ delete_option( self::GA_SHARETHIS_TERMS_OPTION_NAME );
100
+ delete_option( self::GA_HIDE_TERMS_OPTION_NAME );
101
+ delete_option( self::GA_VERSION_OPTION_NAME );
102
+ delete_option( self::GA_SHARETHIS_PROPERTY_ID );
103
+ delete_option( self::GA_SHARETHIS_PROPERTY_SECRET );
104
+ delete_option( self::GA_SHARETHIS_VERIFICATION_RESULT );
105
+ }
106
+
107
+ /**
108
+ * Do actions during plugin load.
109
+ */
110
+ public static function loaded_googleanalytics() {
111
+ self::update_googleanalytics();
112
+ }
113
+
114
+ /**
115
+ * Update hook fires when plugin is being loaded.
116
+ */
117
+ public static function update_googleanalytics() {
118
+ $version = get_option( self::GA_VERSION_OPTION_NAME );
119
+ $installed_version = get_option( self::GA_VERSION_OPTION_NAME, '2.4.0' );
120
+ $old_property_value = Ga_Helper::get_option( 'web_property_id' );
121
+
122
+ if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'eq' ) ) {
123
+ return;
124
+ }
125
+
126
+ if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'lt' ) ) {
127
+
128
+ if ( ! empty( $old_property_value ) ) {
129
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME, $old_property_value );
130
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, 1 );
131
+ delete_option( 'web_property_id' );
132
+ }
133
+ }
134
+
135
+ update_option( self::GA_VERSION_OPTION_NAME, GOOGLEANALYTICS_VERSION );
136
+ }
137
+
138
+ public static function preupdate_exclude_roles( $new_value, $old_value ) {
139
+ if ( ! Ga_Helper::are_features_enabled() ) {
140
+ return '';
141
+ }
142
+
143
+ return wp_json_encode( $new_value );
144
+ }
145
+
146
+ /**
147
+ * Pre-update hook for preparing JSON structure.
148
+ *
149
+ * @param $new_value
150
+ * @param $old_value
151
+ *
152
+ * @return mixed
153
+ */
154
+ public static function preupdate_selected_account( $new_value, $old_value ) {
155
+ $data = null;
156
+ if ( ! empty( $new_value ) ) {
157
+ $data = explode( '_', $new_value );
158
+
159
+ if ( ! empty( $data[1] ) ) {
160
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
161
+ }
162
+ }
163
+
164
+ return wp_json_encode( $data );
165
+ }
166
+
167
+ public static function preupdate_disable_all_features( $new_value, $old_value ) {
168
+ if ( 'on' === $old_value ) {
169
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
170
+ }
171
+
172
+ return $new_value;
173
+ }
174
+
175
+ public static function preupdate_optimize_code( $new_value, $old_value ) {
176
+ if ( ! empty( $new_value ) ) {
177
+ $new_value = sanitize_text_field( wp_unslash( $new_value ) );
178
+ }
179
+
180
+ return $new_value;
181
+ }
182
+
183
+ public static function preupdate_ip_anonymization( $new_value, $old_value ) {
184
+ return $new_value;
185
+ }
186
+
187
+ /**
188
+ * Registers plugin's settings.
189
+ */
190
+ public static function admin_init_googleanalytics() {
191
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME );
192
+ register_setting( GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME );
193
+ register_setting( GA_NAME, self::GA_SELECTED_ACCOUNT );
194
+ register_setting( GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
195
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
196
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
197
+ register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
198
+ register_setting( GA_NAME, 'googleanalytics_optimize_code' );
199
+ register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
200
+ add_filter( 'pre_update_option_' . self::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
201
+ add_filter( 'pre_update_option_' . self::GA_SELECTED_ACCOUNT, 'GA_Admin::preupdate_selected_account', 1, 2 );
202
+ add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
203
+ add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
204
+ }
205
+
206
+ /**
207
+ * Builds plugin's menu structure.
208
+ */
209
+ public static function admin_menu_googleanalytics() {
210
+ $gdpr = get_option('googleanalytics_gdpr_config');
211
+
212
+ if ( current_user_can( 'manage_options' ) ) {
213
+ add_menu_page( 'Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000 );
214
+ add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Dashboard' ), 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics' );
215
+ add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Settings' ), 'manage_options', 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics' );
216
+
217
+ if (!empty($gdpr)) {
218
+ add_submenu_page('googleanalytics', 'Google Analytics', __('GDPR'), 'manage_options',
219
+ 'googleanalytics/gdpr', 'Ga_Admin::gdpr_page_googleanalytics');
220
+ }
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Prepares and displays plugin's stats page.
226
+ */
227
+ public static function statistics_page_googleanalytics() {
228
+
229
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
230
+ return false;
231
+ }
232
+
233
+ $data = self::get_stats_page();
234
+ Ga_View_Core::load(
235
+ 'statistics',
236
+ array(
237
+ 'data' => $data,
238
+ )
239
+ );
240
+
241
+ if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
242
+ self::api_client()->add_own_error( '1', __( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
243
+ }
244
+
245
+ self::display_api_errors();
246
+ }
247
+
248
+ /**
249
+ * Prepares and displays plugin's settings page.
250
+ */
251
+ public static function options_page_googleanalytics() {
252
+
253
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
254
+ return false;
255
+ }
256
+ if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
257
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
258
+ }
259
+ /**
260
+ * Keeps data to be extracted as variables in the view.
261
+ *
262
+ * @var array $data
263
+ */
264
+ $data = array();
265
+
266
+ $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
267
+ $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
268
+ $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
269
+ $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
270
+
271
+ $roles = Ga_Helper::get_user_roles();
272
+ $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
273
+
274
+ $tmp = array();
275
+ if ( ! empty( $roles ) ) {
276
+ foreach ( $roles as $role ) {
277
+ $role_id = Ga_Helper::prepare_role_id( $role );
278
+ $tmp[] = array(
279
+ 'name' => $role,
280
+ 'id' => $role_id,
281
+ 'checked' => ( ! empty( $saved[ $role_id ] ) && 'on' === $saved[ $role_id ] ),
282
+ );
283
+ }
284
+ }
285
+ $data['roles'] = $tmp;
286
+
287
+ if ( Ga_Helper::is_authorized() ) {
288
+ $data['ga_accounts_selector'] = self::get_accounts_selector();
289
+ $data['auth_button'] = self::get_auth_button( 'reauth' );
290
+ } else {
291
+ $data['popup_url'] = self::get_auth_popup_url();
292
+ $data['auth_button'] = self::get_auth_button( 'auth' );
293
+ }
294
+ $data['debug_modal'] = self::get_debug_modal();
295
+ $data['debug_info'] = Ga_SupportLogger::$debug_info;
296
+
297
+ if ( ! empty( $_GET['err'] ) ) { // WPCS: CSRF ok.
298
+ switch ( $_GET['err'] ) { // WPCS: CSRF ok.
299
+ case 1:
300
+ $data['error_message'] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
301
+ break;
302
+ case 2:
303
+ $data['error_message'] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
304
+ break;
305
+ }
306
+ }
307
+ Ga_View_Core::load(
308
+ 'page',
309
+ array(
310
+ 'data' => $data,
311
+ 'tooltip' => Ga_Helper::get_tooltip(),
312
+ )
313
+ );
314
+
315
+ self::display_api_errors();
316
+ }
317
+
318
+ /**
319
+ * Prepares and displays plugin's gdpr page.
320
+ */
321
+ public static function gdpr_page_googleanalytics() {
322
+
323
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
324
+ return false;
325
+ }
326
+ if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
327
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
328
+ }
329
+
330
+ $vendor_data = self::getVendors();
331
+ $vendors = $vendor_data['vendors'];
332
+ $purposes = array_column($vendor_data['purposes'], 'name', 'id');
333
+
334
+ include plugin_dir_path(__FILE__) . '../view/templates/gdpr-config.php';
335
+ }
336
+
337
+ /**
338
+ * Prepares and returns a plugin's URL to be opened in a popup window
339
+ * during Google authentication process.
340
+ *
341
+ * @return mixed
342
+ */
343
+ public static function get_auth_popup_url() {
344
+ return admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_auth' ) ) );
345
+ }
346
+
347
+ /**
348
+ * Prepares and returns Google Account's dropdown code.
349
+ *
350
+ * @return string
351
+ */
352
+ public static function get_accounts_selector() {
353
+ $selected = Ga_Helper::get_selected_account_data();
354
+ $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
355
+ if ( ! Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
356
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", 'warning' ) );
357
+ }
358
+
359
+ return Ga_View_Core::load(
360
+ 'ga_accounts_selector',
361
+ array(
362
+ 'selector' => $selector,
363
+ 'selected' => $selected ? implode( '_', $selected ) : null,
364
+ 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled(),
365
+ ),
366
+ true
367
+ );
368
+ }
369
+
370
+ /**
371
+ * Adds JS scripts for the settings page.
372
+ */
373
+ public static function enqueue_ga_scripts() {
374
+ $property_id = get_option( 'googleanalytics_sherethis_property_id', true );
375
+ $secret = get_option( 'googleanalytics_sherethis_property_secret', true );
376
+ $config = wp_json_encode(get_option('googleanalytics_gdpr_config'));
377
+
378
+ wp_register_script(
379
+ GA_NAME . '-page-js',
380
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js',
381
+ [ 'jquery' ],
382
+ time(),
383
+ false
384
+ );
385
+
386
+ wp_enqueue_script(GA_NAME . '-page-js');
387
+ wp_add_inline_script(
388
+ GA_NAME . '-page-js',
389
+ 'var siteAdminUrl = \'' .
390
+ admin_url() .
391
+ '\'; var gaGdprConfig = \''.
392
+ $config .
393
+ '\'; var ga_demo_nonce = "' .
394
+ wp_create_nonce('ga_demo_nonce') .
395
+ '"; var ga_property_id = "' . $property_id .
396
+ '"; var ga_secret_id = "' .
397
+ $secret .
398
+ '";'
399
+ );
400
+ }
401
+
402
+ /**
403
+ * Adds CSS plugin's scripts.
404
+ */
405
+ public static function enqueue_ga_css() {
406
+ wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, time(), 'all' );
407
+ wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
408
+ wp_enqueue_style( GA_NAME . '-css');
409
+ wp_enqueue_style( GA_NAME . '-additional-css' );
410
+ if ( Ga_Helper::is_wp_old() ) {
411
+ wp_register_style( GA_NAME . '-old-wp-support-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_old_wp_support.css', false, GOOGLEANALYTICS_VERSION, 'all' );
412
+ wp_enqueue_style( GA_NAME . '-old-wp-support-css' );
413
+ }
414
+ wp_register_style( GA_NAME . '-modal-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_modal.css', false, GOOGLEANALYTICS_VERSION, 'all' );
415
+ wp_enqueue_style( GA_NAME . '-modal-css' );
416
+ }
417
+
418
+ /**
419
+ * Enqueues dashboard JS scripts.
420
+ */
421
+ private static function enqueue_dashboard_scripts() {
422
+ wp_register_script(
423
+ GA_NAME . '-dashboard-js',
424
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js',
425
+ [ 'jquery' ],
426
+ GOOGLEANALYTICS_VERSION,
427
+ false
428
+ );
429
+ wp_enqueue_script( GA_NAME . '-dashboard-js' );
430
+ }
431
+
432
+ /**
433
+ * Enqueues plugin's JS and CSS scripts.
434
+ */
435
+ public static function enqueue_scripts() {
436
+ $domain = str_replace('http://','', str_replace('https://', '', str_replace( '/wp-admin/', '', admin_url() )));
437
+ $st_prop = get_option(self::GA_SHARETHIS_PROPERTY_ID);
438
+ $st_secret = get_option(self::GA_SHARETHIS_PROPERTY_SECRET);
439
+
440
+ if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
441
+ wp_register_script(
442
+ GA_NAME . '-js',
443
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js',
444
+ [ 'jquery' ],
445
+ GOOGLEANALYTICS_VERSION,
446
+ false
447
+ );
448
+ wp_enqueue_script( GA_NAME . '-js' );
449
+
450
+ wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, 1, false );
451
+ wp_enqueue_script( 'googlecharts' );
452
+ wp_add_inline_script(GA_NAME . '-js', 'var ga_demo_nonce = "' . wp_create_nonce('ga_demo_nonce') . '";');
453
+
454
+ if ( empty($st_prop) || empty($st_secret) ) {
455
+ wp_register_script( 'googlecreateprop', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/googleanalytics_createprop.js', ['jquery', 'wp-util'], time(), false );
456
+ wp_enqueue_script('googlecreateprop');
457
+ wp_add_inline_script('googlecreateprop', '
458
+ var gaNonce = "' . wp_create_nonce('googleanalyticsnonce') . '";
459
+ var gasiteURL = "' . $domain . '";
460
+ var gaAdminEmail = "' . get_option('admin_email') . '";'
461
+ );
462
+ }
463
+
464
+ self::enqueue_ga_css();
465
+ }
466
+
467
+ if ( Ga_Helper::is_dashboard_page() ) {
468
+ self::enqueue_dashboard_scripts();
469
+ }
470
+
471
+ if ( Ga_Helper::is_plugin_page() ) {
472
+ self::enqueue_ga_scripts();
473
+ }
474
+ }
475
+
476
+ /**
477
+ * Prepares plugin's statistics page and return HTML code.
478
+ *
479
+ * @return string HTML code
480
+ */
481
+ public static function get_stats_page() {
482
+ $chart = null;
483
+ $age_chart = null;
484
+ $gender_chart = null;
485
+ $boxes = null;
486
+ $labels = null;
487
+ $sources = null;
488
+ if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && ! Ga_Helper::is_all_feature_disabled() ) {
489
+ list( $chart, $age_chart, $gender_chart, $boxes, $labels, $sources ) = self::generate_stats_data();
490
+ }
491
+
492
+ return Ga_Helper::get_chart_page(
493
+ 'stats',
494
+ array(
495
+ 'chart' => $chart,
496
+ 'gender_chart' => $gender_chart,
497
+ 'age_chart' => $age_chart,
498
+ 'boxes' => $boxes,
499
+ 'labels' => $labels,
500
+ 'sources' => $sources,
501
+ )
502
+ );
503
+ }
504
+
505
+ /**
506
+ * Shows plugin's notice on the admin area.
507
+ */
508
+ public static function admin_notice_googleanalytics() {
509
+ if ( (!get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && Ga_Helper::is_plugin_page() ) || (!get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && !get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) ) {
510
+ $current_url = Ga_Helper::get_current_url();
511
+ $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
512
+ Ga_View_Core::load( 'ga_notice', array(
513
+ 'url' => $url
514
+ ) );
515
+ }
516
+
517
+ if ( !empty( $_GET[ 'settings-updated' ] ) && Ga_Helper::is_plugin_page() ) {
518
+ echo Ga_Helper::ga_wp_notice( _( 'Settings saved' ), self::NOTICE_SUCCESS );
519
+ }
520
+
521
+ if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
522
+ echo wp_kses_post(
523
+ Ga_Helper::ga_wp_notice(
524
+ __( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ),
525
+ 'warning',
526
+ false,
527
+ array(
528
+ 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
529
+ 'label' => __( 'Enable' ),
530
+ )
531
+ )
532
+ );
533
+ }
534
+ }
535
+
536
+ /**
537
+ * Prepare required PHP version warning.
538
+ * @return string
539
+ */
540
+ public static function admin_notice_googleanalytics_php_version() {
541
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED, 'error' ) );
542
+ }
543
+
544
+ /**
545
+ * Prepare required WP version warning
546
+ * @return string
547
+ */
548
+ public static function admin_notice_googleanalytics_wp_version() {
549
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION, 'error' ) );
550
+ }
551
+
552
+ /**
553
+ * Hides plugin's notice
554
+ */
555
+ public static function admin_notice_hide_googleanalytics() {
556
+ update_option( self::GA_HIDE_TERMS_OPTION_NAME, true );
557
+ }
558
+
559
+ /**
560
+ * Adds GA dashboard widget only for administrators.
561
+ */
562
+ public static function add_dashboard_device_widget() {
563
+ if ( Ga_Helper::is_administrator() ) {
564
+ wp_add_dashboard_widget( 'ga_dashboard_widget', __( 'Google Analytics Dashboard' ), 'Ga_Helper::add_ga_dashboard_widget' );
565
+ }
566
+ }
567
+
568
+ /**
569
+ * Adds plugin's actions
570
+ */
571
+ public static function add_actions() {
572
+ add_action( 'admin_init', 'Ga_Admin::admin_init_googleanalytics' );
573
+ add_action( 'admin_menu', 'Ga_Admin::admin_menu_googleanalytics' );
574
+ add_action( 'admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts' );
575
+ add_action( 'wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget' );
576
+ add_action( 'wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change' );
577
+ add_action( 'wp_ajax_ga_ajax_hide_review', 'Ga_Admin::ga_ajax_hide_review' );
578
+ add_action( 'wp_ajax_ga_ajax_enable_gdpr', 'Ga_Admin::gaAjaxGdprEnable' );
579
+ add_action( 'wp_ajax_ga_ajax_enable_demographic', 'Ga_Admin::gaAjaxEnableDemo' );
580
+ add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
581
+ add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
582
+ add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
583
+ add_action( 'wp_ajax_set_ga_credentials', 'Ga_Admin::createGAProperty' );
584
+
585
+ if ( !get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && !get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) {
586
+ add_action( 'wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics' );
587
+ }
588
+ }
589
+
590
+ /**
591
+ * Runs jobs
592
+ *
593
+ * @param $response
594
+ * @param $screen_id
595
+ */
596
+ public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
597
+
598
+ if ( self::GA_HEARTBEAT_API_CACHE_UPDATE ) {
599
+ // Disable cache for ajax request
600
+ self::api_client()->set_disable_cache( true );
601
+
602
+ // Try to regenerate cache if needed
603
+ self::generate_stats_data();
604
+ }
605
+ }
606
+
607
+ /**
608
+ * Adds plugin's filters
609
+ */
610
+ public static function add_filters() {
611
+ add_filter( 'plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5 );
612
+ }
613
+
614
+ /**
615
+ * Adds new action links on the plugin list.
616
+ *
617
+ * @param $actions
618
+ * @param $plugin_file
619
+ *
620
+ * @return mixed
621
+ */
622
+ public static function ga_action_links( $actions, $plugin_file ) {
623
+
624
+ if ( basename( $plugin_file ) === GA_NAME . '.php' ) {
625
+ array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . __( 'Settings' ) . '</a>' );
626
+ }
627
+
628
+ return $actions;
629
+ }
630
+
631
+ public static function init_oauth() {
632
+
633
+ $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
634
+
635
+ if ( ! empty( $code ) ) {
636
+ Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, '' );
637
+
638
+ // Get access token
639
+ $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
640
+ if ( empty( $response ) ) {
641
+ return false;
642
+ }
643
+ $param = '';
644
+ if ( ! self::save_access_token( $response ) ) {
645
+ $param = '&err=1';
646
+ $errors = self::api_client()->get_errors();
647
+ if ( ! empty( $errors ) ) {
648
+ foreach ( $errors as $error ) {
649
+ if ( 'invalid_grant' === $error['message'] ) {
650
+ $param = '&err=2';
651
+ }
652
+ }
653
+ }
654
+ } else {
655
+ self::api_client()->set_access_token( $response->getData() );
656
+ // Get accounts data
657
+ $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
658
+ self::save_ga_account_summaries( $account_summaries->getData() );
659
+ update_option( self::GA_SELECTED_ACCOUNT, '' );
660
+ }
661
+
662
+ wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
663
+ }
664
+ }
665
+
666
+ /**
667
+ * Save access token.
668
+ *
669
+ * @param Ga_Lib_Api_Response $response
670
+ *
671
+ * @return boolean
672
+ */
673
+ public static function save_access_token( $response, $refresh_token = '' ) {
674
+ $access_token = $response->getData();
675
+ if ( ! empty( $access_token ) ) {
676
+ $access_token['created'] = time();
677
+ } else {
678
+ return false;
679
+ }
680
+
681
+ if ( ! empty( $refresh_token ) ) {
682
+ $access_token['refresh_token'] = $refresh_token;
683
+ }
684
+
685
+ return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
686
+ }
687
+
688
+ /**
689
+ * Saves Google Analytics account data.
690
+ *
691
+ * @param $data
692
+ *
693
+ * @return array
694
+ */
695
+ public static function save_ga_account_summaries( $data ) {
696
+ $return = array();
697
+ if ( ! empty( $data['items'] ) ) {
698
+ foreach ( $data['items'] as $item ) {
699
+ $tmp = array();
700
+ $tmp['id'] = $item['id'];
701
+ $tmp['name'] = $item['name'];
702
+ if ( is_array( $item['webProperties'] ) ) {
703
+ foreach ( $item['webProperties'] as $property ) {
704
+ $profiles = array();
705
+ if ( is_array( $property['profiles'] ) ) {
706
+ foreach ( $property['profiles'] as $profile ) {
707
+ $profiles[] = array(
708
+ 'id' => $profile['id'],
709
+ 'name' => $profile['name'],
710
+ );
711
+ }
712
+ }
713
+
714
+ $tmp['webProperties'][] = array(
715
+ 'internalWebPropertyId' => $property['internalWebPropertyId'],
716
+ 'webPropertyId' => $property['id'],
717
+ 'name' => $property['name'],
718
+ 'profiles' => $profiles,
719
+ );
720
+ }
721
+ }
722
+
723
+ $return[] = $tmp;
724
+ }
725
+
726
+ update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
727
+ } else {
728
+ update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, '' );
729
+ }
730
+ update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, '' );
731
+
732
+ return $return;
733
+ }
734
+
735
+ /**
736
+ * Handle AJAX data for the GA dashboard widget.
737
+ */
738
+ public static function ga_ajax_data_change() {
739
+ if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
740
+ $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null; // WPCS: CSRF ok.
741
+ $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null; // WPCS: CSRF ok.
742
+ echo wp_kses_post( Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true ) );
743
+ } else {
744
+ echo wp_json_encode( array( 'error' => __( 'Invalid request.' ) ) );
745
+ }
746
+
747
+ wp_die();
748
+ }
749
+
750
+ /**
751
+ * Displays API error messages.
752
+ */
753
+ public static function display_api_errors( $alias = '' ) {
754
+ $errors = self::api_client( $alias )->get_errors();
755
+ if ( ! empty( $errors ) ) {
756
+ foreach ( $errors as $error ) {
757
+ echo wp_kses_post( Ga_Notice::get_message( $error ) );
758
+ }
759
+ }
760
+ }
761
+
762
+ /**
763
+ * Gets dashboard data.
764
+ *
765
+ * @return array
766
+ */
767
+ public static function generate_stats_data() {
768
+ $selected = Ga_Helper::get_selected_account_data( true );
769
+ $update_data = self::checkDataDate();
770
+ $query_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'main_chart', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
771
+ $stats_data = self::api_client()->call(
772
+ 'ga_api_data',
773
+ [ $query_params ]
774
+ );
775
+ $gender_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'gender', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'gender', $selected['view_id'] );
776
+ $gender_data = self::api_client()->call(
777
+ 'ga_api_data',
778
+ [$gender_params]
779
+ );
780
+ $age_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'age', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'age', $selected['view_id'] );
781
+ $age_data = self::api_client()->call(
782
+ 'ga_api_data',
783
+ [$age_params]
784
+ );
785
+ $boxes_data = self::api_client()->call(
786
+ 'ga_api_data',
787
+ [ Ga_Stats::get_query( 'boxes', $selected['view_id'] ) ]
788
+ );
789
+ $sources_data = self::api_client()->call(
790
+ 'ga_api_data',
791
+ [ Ga_Stats::get_query( 'sources', $selected['view_id'] ) ]
792
+ );
793
+
794
+ $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
795
+ $gender_chart = ! empty($gender_data) ? Ga_Stats::get_gender_chart($gender_data->getData()) : [];
796
+ $age_chart = ! empty($age_data) ? Ga_Stats::get_age_chart($age_data->getData()) : [];
797
+ $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
798
+ $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
799
+
800
+ unset( $chart['date'] );
801
+ $labels = array(
802
+ 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
803
+ 'thisMonth' => date( 'M d, Y', strtotime( '-29 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
804
+ );
805
+ $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
806
+
807
+ // Add gender/age data for 30 days.
808
+ if ($update_data) {
809
+ $gender_params = Ga_Stats::get_query( 'gender', $selected['view_id'], '30daysAgo' );
810
+ $gender_data = self::api_client()->call(
811
+ 'ga_api_data',
812
+ [$gender_params]
813
+ );
814
+ $age_params = Ga_Stats::get_query( 'age', $selected['view_id'], '30daysAgo' );
815
+ $age_data = self::api_client()->call(
816
+ 'ga_api_data',
817
+ [$age_params]
818
+ );
819
+
820
+ $gender_chart = ! empty($gender_data) ? Ga_Stats::get_gender_chart($gender_data->getData()) : [];
821
+ $age_chart = ! empty($age_data) ? Ga_Stats::get_age_chart($age_data->getData()) : [];
822
+
823
+ self::updateDemoData(
824
+ $gender_chart,
825
+ $age_chart
826
+ );
827
+ }
828
+
829
+ return [$chart, $age_chart, $gender_chart, $boxes, $labels, $sources];
830
+ }
831
+
832
+ private static function updateDemoData($gender_response = false, $age_response = false)
833
+ {
834
+ $demoSendData = [];
835
+ if ($gender_response && $age_response) {
836
+ foreach ($age_response as $type => $amount) {
837
+ $demoSendData['age'][$type] = intval($amount);
838
+ $x++;
839
+ }
840
+ foreach ($gender_response as $type => $amount) {
841
+ $demoSendData['gender'][ucfirst($type)] = intval($amount);
842
+ $x++;
843
+ }
844
+ }
845
+
846
+ // Add data for send.
847
+ update_option('googleanalytics_demo_data', wp_json_encode($demoSendData));
848
+
849
+ // Trigger send.
850
+ update_option('googleanalytics_send_data', "true");
851
+ }
852
+
853
+ /**
854
+ * Check if we should send batch of demo data.
855
+ *
856
+ * @return bool
857
+ */
858
+ private static function checkDataDate()
859
+ {
860
+ $demo_enabled = get_option('googleanalytics_demographic');
861
+ $demo_date = !empty(get_option('googleanalytics_demo_date')) ? strtotime(get_option('googleanalytics_demo_date')) : '';
862
+ $thirty_date = '' !== $demo_date ? date("Y-m-d", strtotime("+1 month", $demo_date)) : '';
863
+
864
+ if (empty($demo_enabled) || !$demo_enabled) {
865
+ return false;
866
+ }
867
+
868
+ if ('' !== $demo_date && $thirty_date <= $current_date) {
869
+ return true;
870
+ } elseif ('' === $demo_date) {
871
+ return true;
872
+ }
873
+
874
+ return false;
875
+ }
876
+
877
+ /**
878
+ * Returns auth or re-auth button
879
+ *
880
+ * @return string
881
+ */
882
+ public static function get_auth_button( $type ) {
883
+
884
+ return Ga_View_Core::load(
885
+ 'ga_auth_button',
886
+ [
887
+ 'label' => 'auth' === $type ? 'Authenticate with Google' : 'Re-authenticate with Google',
888
+ 'type' => $type,
889
+ 'url' => self::get_auth_popup_url(),
890
+ 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ),
891
+ ],
892
+ true
893
+ );
894
+ }
895
+
896
+ /**
897
+ * Returns debug modal
898
+ *
899
+ * @return string
900
+ */
901
+ public static function get_debug_modal() {
902
+
903
+ return Ga_View_Core::load(
904
+ 'ga_debug_modal',
905
+ [ 'debug_info' => Ga_SupportLogger::$debug_info, 'debug_help_message' => Ga_SupportLogger::$debug_help_message ],
906
+ true
907
+ );
908
+ }
909
+
910
+ public static function ga_ajax_hide_review( $post ) {
911
+ $error = 0;
912
+
913
+ if ( Ga_Controller_Core::verify_nonce( 'ga_ajax_data_change' ) ) {
914
+ update_option('googleanalytics-hide-review', true);
915
+ }
916
+
917
+ wp_send_json_success('hidden');
918
+ }
919
+
920
+ public static function gaAjaxGdprEnable( $post ) {
921
+ if (!isset($_POST['config'])) {
922
+ wp_send_json_error('No config found.');
923
+ }
924
+
925
+ update_option('googleanalytics_gdpr_config', $_POST['config']);
926
+
927
+ wp_send_json_success('gdpr_on');
928
+ }
929
+
930
+ public static function gaAjaxEnableDemo( $post ) {
931
+ check_ajax_referer( 'ga_demo_nonce', 'nonce' );
932
+
933
+ $enabled = isset($_POST['enabled']) && $_POST['enabled'] === 'true' ? true : false;
934
+
935
+ update_option('googleanalytics_demographic', $enabled);
936
+
937
+ wp_send_json_success('demo_on');
938
+ }
939
+
940
+ /**
941
+ * New property creation method.
942
+ */
943
+ public static function createGAProperty() {
944
+ check_ajax_referer( 'googleanalyticsnonce', 'nonce' );
945
+
946
+ if (! isset($_POST['propid'], $_POST['secret'])) { // WPCS: input var ok.
947
+ wp_send_json_error( 'Set credentials failed.' );
948
+ }
949
+
950
+ $secret = sanitize_text_field( wp_unslash( $_POST['secret'] ) ); // WPCS: input var ok.
951
+ $propid = sanitize_text_field( wp_unslash( $_POST['propid'] ) ); // WPCS: input var ok.
952
+
953
+ update_option(self::GA_SHARETHIS_PROPERTY_ID, $propid);
954
+ update_option(self::GA_SHARETHIS_PROPERTY_SECRET, $secret);
955
+ }
956
+
957
+ /**
958
+ * Helper function get vendors.
959
+ *
960
+ * @param string $page
961
+ * @return array
962
+ */
963
+ private static function getVendors()
964
+ {
965
+ $response = wp_remote_get('https://vendorlist.consensu.org/v2/vendor-list.json');
966
+
967
+ return json_decode(wp_remote_retrieve_body($response), true);
968
+ }
969
  }
class/Ga_Helper.php CHANGED
@@ -436,7 +436,7 @@ class Ga_Helper {
436
  * @return bool
437
  */
438
  public static function are_terms_accepted() {
439
- return true;
440
  }
441
 
442
  /**
436
  * @return bool
437
  */
438
  public static function are_terms_accepted() {
439
+ return self::get_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME );
440
  }
441
 
442
  /**
class/Ga_Stats.php CHANGED
@@ -1,786 +1,928 @@
1
- <?php
2
-
3
- /**
4
- * Ga_Stats class
5
- *
6
- * Preparing request and parsing response from Google Analytics Reporting Api
7
- *
8
- * @author wle@adips.com
9
- * @version 1.0
10
- */
11
- class Ga_Stats {
12
-
13
- private $profile = array();
14
-
15
- /**
16
- * Primary class constructor.
17
- *
18
- * @access public
19
- * @since 7.0.0
20
- */
21
- public function __construct() {
22
- $this->profile = $this->get_analytics_profile();
23
- }
24
-
25
- /**
26
- * Preparing query to get Analytics data
27
- *
28
- * @param string $query query type
29
- * @param int $id_view The Analytics view ID from which to retrieve data.
30
- * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
31
- * @param string $metric A metric expression
32
- *
33
- * @return array Request query
34
- */
35
- public static function get_query( $query, $id_view, $date_range = null, $metric = null ) {
36
- if ( $query == 'main_chart' ) {
37
- return self::main_chart_query( $id_view, $date_range, $metric );
38
- } elseif ( $query == 'boxes' ) {
39
- return self::boxes_query( $id_view );
40
- } elseif ( $query == 'dashboard_boxes' ) {
41
- return self::dashboard_boxes_query( $id_view, $date_range );
42
- } elseif ( $query == 'sources' ) {
43
- return self::sources_query( $id_view );
44
- } else {
45
- return array();
46
- }
47
- }
48
-
49
- /**
50
- * Preparing query for top traffic sources table
51
- *
52
- * @param int $id_view The Analytics view ID from which to retrieve data.
53
- *
54
- * @return array Sources query
55
- */
56
- public static function sources_query( $id_view ) {
57
- $reports_requests = array();
58
- $daysAgo = isset($_GET['th']) ? '30daysAgo' : '7daysAgo';
59
-
60
- if ( isset( $_GET['ts'] ) ) {
61
- $reports_requests[] = array(
62
- 'viewId' => $id_view,
63
- 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
64
- 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
65
- 'includeEmptyRows' => true,
66
- 'pageSize' => 10,
67
- 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
68
- 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
69
- );
70
- } else {
71
- $reports_requests[] = array(
72
- 'viewId' => $id_view,
73
- 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
74
- 'metrics' => self::set_metrics( array(
75
- 'ga:pageviews',
76
- 'ga:uniquePageviews',
77
- 'ga:timeOnPage',
78
- 'ga:bounces',
79
- 'ga:entrances',
80
- 'ga:exits'
81
- ) ),
82
- 'includeEmptyRows' => true,
83
- 'pageSize' => 10,
84
- 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
85
- 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
86
- );
87
- }
88
-
89
- $query = array(
90
- 'reportRequests' => $reports_requests
91
- );
92
-
93
- return $query;
94
- }
95
-
96
- /**
97
- * Preparing query for dashbord boxes
98
- *
99
- * @param int $id_view The Analytics view ID from which to retrieve data.
100
- * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
101
- *
102
- * @return array Dashboard boxes query
103
- */
104
- public static function dashboard_boxes_query( $id_view, $date_range ) {
105
- $reports_requests = array();
106
- $daysAgo = isset($_GET['th']) ? '30daysAgo' : '7daysAgo';
107
-
108
- if ( isset( $_GET['ts'] ) ) {
109
- $reports_requests[] = array(
110
- 'viewId' => $id_view,
111
- 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
112
- 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
113
- 'includeEmptyRows' => true,
114
- 'pageSize' => 10,
115
- 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
116
- 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
117
- );
118
- } else {
119
- $reports_requests[] = array(
120
- 'viewId' => $id_view,
121
- 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
122
- 'metrics' => self::set_metrics( array(
123
- 'ga:pageviews',
124
- 'ga:uniquePageviews',
125
- 'ga:timeOnPage',
126
- 'ga:bounces',
127
- 'ga:entrances',
128
- 'ga:exits'
129
- ) ),
130
- 'includeEmptyRows' => true,
131
- 'pageSize' => 10,
132
- 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
133
- 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
134
- );
135
- }
136
- $query = array(
137
- 'reportRequests' => $reports_requests
138
- );
139
-
140
- return $query;
141
- }
142
-
143
- /**
144
- * Preparing query for stats boxes
145
- *
146
- * @param int $id_view The Analytics view ID from which to retrieve data.
147
- *
148
- * @return array Boxes query
149
- */
150
- public static function boxes_query( $id_view ) {
151
- $range = isset( $_GET['th'] ) ? '30daysAgo' : '7daysAgo';
152
- $range_s_prev = isset( $_GET['th'] ) ? '60daysAgo' : '14daysAgo';
153
- $range_e_prev = isset( $_GET['th'] ) ? '31daysAgo' : '8daysAgo';
154
- $reports_requests = array();
155
- $reports_requests[] = array(
156
- 'viewId' => $id_view,
157
- 'dateRanges' => self::set_date_ranges( $range, 'yesterday', $range_s_prev, $range_e_prev ),
158
- 'metrics' => self::set_metrics( array(
159
- 'ga:users',
160
- 'ga:pageviews',
161
- 'ga:pageviewsPerSession',
162
- 'ga:BounceRate'
163
- ) ),
164
- 'includeEmptyRows' => true,
165
- 'dimensions' => self::set_dimensions( 'ga:date' )
166
- );
167
- $query = array(
168
- 'reportRequests' => $reports_requests
169
- );
170
-
171
- return $query;
172
- }
173
-
174
- /**
175
- * Preparing query for chart
176
- *
177
- * @param int $id_view The Analytics view ID from which to retrieve data.
178
- * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
179
- * @param string $metric A metric expression
180
- *
181
- * @return array Chart query
182
- */
183
- public static function main_chart_query( $id_view, $date_range = null, $metric = null ) {
184
- if ( empty( $date_range ) ) {
185
- $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
186
- } else {
187
- $date_ranges = self::set_date_ranges( $date_range, 'yesterday', '14daysAgo', '8daysAgo' );
188
- }
189
-
190
- if ( empty( $metric ) ) {
191
- $metric = 'ga:pageviews';
192
- } else {
193
- $metric = 'ga:' . $metric;
194
- }
195
-
196
- $reports_requests = array();
197
- $reports_requests[] = array(
198
- 'viewId' => $id_view,
199
- 'dateRanges' => $date_ranges,
200
- 'metrics' => self::set_metrics( $metric ),
201
- 'includeEmptyRows' => true,
202
- 'dimensions' => self::set_dimensions( 'ga:date' )
203
- );
204
- $query = array(
205
- 'reportRequests' => $reports_requests
206
- );
207
-
208
- return $query;
209
- }
210
-
211
- /**
212
- * Setting order for requests
213
- *
214
- * @param string $name The field which to sort by. The default sort order is ascending. Example: ga:browser.
215
- * @param string $sort The sorting order for the field. 'ASCENDING' or 'DESCENDING'
216
- *
217
- * @return array OrderBys
218
- */
219
- public static function set_order_bys( $name, $sort ) {
220
- $order = array();
221
- $order[] = array(
222
- 'fieldName' => $name,
223
- 'sortOrder' => $sort,
224
- );
225
-
226
- return $order;
227
- }
228
-
229
- /**
230
- * Setting metrics for requests
231
- *
232
- * @param mixed $expression A metric expression or array of expressions
233
- *
234
- * @return array Metrics
235
- */
236
- public static function set_metrics( $expression ) {
237
- $metrics = array();
238
- if ( is_array( $expression ) ) {
239
- foreach ( $expression as $exp ) {
240
- $metrics[] = array(
241
- 'expression' => $exp
242
- );
243
- }
244
- } else {
245
- $metrics[] = array(
246
- 'expression' => $expression
247
- );
248
- }
249
-
250
- return $metrics;
251
- }
252
-
253
- /**
254
- * Setting dimensions for requests
255
- *
256
- * @param string $name Name of the dimension to fetch, for example ga:browser.
257
- *
258
- * @return array Dimensions
259
- */
260
- public static function set_dimensions( $name ) {
261
- $dimensions = array();
262
- $dimensions[] = array(
263
- 'name' => $name
264
- );
265
-
266
- return $dimensions;
267
- }
268
-
269
- /**
270
- * Setting date ranges for requests
271
- *
272
- * @param string $start_date The start date for the query in the format YYYY-MM-DD.
273
- * @param string $end_date The end date for the query in the format YYYY-MM-DD.
274
- * @param string $prev_start_date The start date (second range) for the query in the format YYYY-MM-DD.
275
- * @param string $prev_end_date The start date (second range) for the query in the format YYYY-MM-DD.
276
- *
277
- * @return array Date ranges
278
- */
279
- public static function set_date_ranges( $start_date, $end_date, $prev_start_date = '', $prev_end_date = '' ) {
280
- $date_danges = array();
281
- $date_danges[] = array(
282
- 'startDate' => $start_date,
283
- 'endDate' => $end_date
284
- );
285
- if ( !empty( $prev_start_date ) and ! empty( $prev_end_date ) ) {
286
- $date_danges[] = array(
287
- 'startDate' => $prev_start_date,
288
- 'endDate' => $prev_end_date
289
- );
290
- }
291
-
292
- return $date_danges;
293
- }
294
-
295
- /**
296
- * Preparing response for data received from analytics
297
- *
298
- * @param array $data Analytics response
299
- *
300
- * @return array Response rows
301
- */
302
- public static function prepare_response( $data ) {
303
- $data = self::get_reports_from_response( $data );
304
- self::handle_more_reports( $data );
305
- $report = self::get_single_report( $data );
306
- self::get_report_column_header( $report );
307
- $report_data = self::get_report_data( $report );
308
- self::get_totals( $report_data );
309
- self::get_row_count( $report_data );
310
- $rows = self::get_rows( $report_data );
311
-
312
- return $rows;
313
- }
314
-
315
- /**
316
- * Get dimensions from response row
317
- *
318
- * @param array $row Analytics response row
319
- *
320
- * @return array Dimensions
321
- */
322
- public static function get_dimensions( $row ) {
323
- if ( !empty( $row[ 'dimensions' ] ) ) {
324
- return $row[ 'dimensions' ];
325
- }
326
-
327
- return false;
328
- }
329
-
330
- /**
331
- * Get metrics from response row
332
- *
333
- * @param array $row Analytics response row
334
- *
335
- * @return array Metrics
336
- */
337
- public static function get_metrics( $row ) {
338
- if ( !empty( $row[ 'metrics' ] ) ) {
339
- return $row[ 'metrics' ];
340
- }
341
-
342
- return false;
343
- }
344
-
345
- /**
346
- * Get row from response report data
347
- *
348
- * @param array $report_data Analytics response report data
349
- *
350
- * @return array Rows
351
- */
352
- public static function get_rows( $report_data ) {
353
- if ( !empty( $report_data[ 'rows' ] ) ) {
354
- return $report_data[ 'rows' ];
355
- }
356
-
357
- return false;
358
- }
359
-
360
- /**
361
- * Get row count from response report data
362
- *
363
- * @param array $report_data Analytics response report data
364
- *
365
- * @return array Row count
366
- */
367
- public static function get_row_count( $report_data ) {
368
- if ( !empty( $report_data[ 'rowCount' ] ) ) {
369
- return $report_data[ 'rowCount' ];
370
- }
371
-
372
- return false;
373
- }
374
-
375
- /**
376
- * Get totals from response report data
377
- *
378
- * @param array $report_data Analytics response report data
379
- *
380
- * @return array Totals
381
- */
382
- public static function get_totals( $report_data ) {
383
- if ( !empty( $report_data[ 'totals' ] ) ) {
384
- return $report_data[ 'totals' ];
385
- }
386
-
387
- return false;
388
- }
389
-
390
- /**
391
- * Get reports from response data
392
- *
393
- * @param array $data Analytics response data
394
- *
395
- * @return array Reports
396
- */
397
- public static function get_reports_from_response( $data ) {
398
- if ( !empty( $data[ 'reports' ] ) ) {
399
- return $data[ 'reports' ];
400
- }
401
-
402
- return false;
403
- }
404
-
405
- /**
406
- * Show info for multiple data
407
- *
408
- * @param array $data Analytics response data
409
- *
410
- */
411
- public static function handle_more_reports( $data ) {
412
- if ( count( $data ) > 1 ) {
413
- echo 'more than one report';
414
- }
415
- }
416
-
417
- /**
418
- * Show info for multiple rows
419
- *
420
- * @param array $rows Analytics response rows
421
- *
422
- */
423
- public static function handle_more_rows( $rows ) {
424
- if ( count( $rows ) > 1 ) {
425
- echo 'more than one row';
426
- }
427
- }
428
-
429
- /**
430
- * Get single report from response data
431
- *
432
- * @param array $data Analytics response data
433
- *
434
- * @return array Report
435
- */
436
- public static function get_single_report( $data ) {
437
- if ( !empty( $data ) ) {
438
- foreach ( $data as $report ) {
439
- if ( !empty( $report ) ) {
440
- return $report;
441
- }
442
- }
443
- }
444
-
445
- return false;
446
- }
447
-
448
- /**
449
- * Get single row from response data rows
450
- *
451
- * @param array $rows Analytics response data rows
452
- *
453
- * @return array Row
454
- */
455
- public static function get_single_row( $rows ) {
456
- if ( !empty( $rows ) ) {
457
- foreach ( $rows as $row ) {
458
- if ( !empty( $row ) ) {
459
- return $row;
460
- }
461
- }
462
- }
463
-
464
- return false;
465
- }
466
-
467
- /**
468
- * Get column header from response data
469
- *
470
- * @param array $data Analytics response data
471
- *
472
- * @return array Column header
473
- */
474
- public static function get_report_column_header( $data ) {
475
- if ( !empty( $data[ 'columnHeader' ] ) ) {
476
- return $data[ 'columnHeader' ];
477
- }
478
-
479
- return false;
480
- }
481
-
482
- /**
483
- * Get report data from response data
484
- *
485
- * @param array $data Analytics response data
486
- *
487
- * @return array data
488
- */
489
- public static function get_report_data( $data ) {
490
- if ( !empty( $data[ 'data' ] ) ) {
491
- return $data[ 'data' ];
492
- }
493
-
494
- return false;
495
- }
496
-
497
- /**
498
- * Get chart from response data
499
- *
500
- * @param array $response_data Analytics response data
501
- *
502
- * @return array chart data
503
- */
504
- public static function get_chart( $response_data ) {
505
- $chart_data = array();
506
- if ( !empty( $response_data ) ) {
507
- $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
508
- $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
509
- if ( !empty( $rows ) ) {
510
- foreach ( $rows as $key => $row ) {
511
- if ( $key < 7 ) {
512
- $chart_data[ $key ][ 'previous' ] = !empty( $row[ 'metrics' ][ 1 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 1 ][ 'values' ][ 0 ] : 0;
513
- $chart_data[ $key ][ 'previous-day' ] = date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) );
514
- } else {
515
- $chart_data[ $key - 7 ][ 'day' ] = date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) );
516
- $chart_data[ $key - 7 ][ 'current' ] = !empty( $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] : 0;
517
- $chart_data[ 'date' ] = strtotime( $row[ 'dimensions' ][ 0 ] );
518
- }
519
- }
520
- }
521
- }
522
-
523
- return $chart_data;
524
- }
525
-
526
- /**
527
- * Get dasboard chart from response data
528
- *
529
- * @param array $response_data Analytics response data
530
- *
531
- * @return array dashboard chart data
532
- */
533
- public static function get_dashboard_chart( $response_data ) {
534
- $chart_data = array();
535
- if ( !empty( $response_data ) ) {
536
- $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
537
- $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
538
- if ( !empty( $rows ) ) {
539
- foreach ( $rows as $row ) {
540
- $chart_data[] = array(
541
- 'day' => date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) ),
542
- 'current' => !empty( $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] : 0
543
- );
544
- }
545
- }
546
- }
547
-
548
- return $chart_data;
549
- }
550
-
551
- /**
552
- * Get boxes from response data
553
- *
554
- * @param array $data Analytics response data
555
- *
556
- * @return array boxes data
557
- */
558
- public static function get_boxes( $data ) {
559
- if ( !empty( $data ) ) {
560
- $data = self::get_reports_from_response( $data );
561
- self::handle_more_reports( $data );
562
- $report = self::get_single_report( $data );
563
- self::get_report_column_header( $report );
564
- $report_data = self::get_report_data( $report );
565
- $totals = self::get_totals( $report_data );
566
-
567
- return self::get_boxes_from_totals( $totals );
568
- }
569
- }
570
-
571
- /**
572
- * Get boxes from totals
573
- *
574
- * @param array $totals Analytics response totals
575
- *
576
- * @return array boxes data
577
- */
578
- public static function get_boxes_from_totals( $totals ) {
579
- if ( !empty( $totals ) ) {
580
- $boxes_data = array();
581
- foreach ( $totals as $key => $total ) {
582
- if ( $key == 0 ) {
583
- $boxes_data[ 'Users' ][ 'current' ] = $total[ 'values' ][ 0 ];
584
- $boxes_data[ 'Pageviews' ][ 'current' ] = $total[ 'values' ][ 1 ];
585
- $boxes_data[ 'PageviewsPerSession' ][ 'current' ] = $total[ 'values' ][ 2 ];
586
- $boxes_data[ 'BounceRate' ][ 'current' ] = round( $total[ 'values' ][ 3 ], 2 );
587
- } else {
588
- $boxes_data[ 'Users' ][ 'previous' ] = $total[ 'values' ][ 0 ];
589
- $boxes_data[ 'Pageviews' ][ 'previous' ] = $total[ 'values' ][ 1 ];
590
- $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] = $total[ 'values' ][ 2 ];
591
- $boxes_data[ 'BounceRate' ][ 'previous' ] = round( $total[ 'values' ][ 3 ], 2 );
592
- }
593
- }
594
-
595
- return self::prepare_boxes( $boxes_data );
596
- }
597
-
598
- return false;
599
- }
600
-
601
- /**
602
- * Prepare boxes data
603
- *
604
- * @param array $boxes_data Boxes data
605
- *
606
- * @return array boxes data
607
- */
608
- public static function prepare_boxes( $boxes_data ) {
609
- $boxes_data[ 'Users' ][ 'diff' ] = ( $boxes_data[ 'Users' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'Users' ][ 'current' ] - $boxes_data[ 'Users' ][ 'previous' ] ) / $boxes_data[ 'Users' ][ 'previous' ] * 100, 2 ) : 100;
610
- $boxes_data[ 'Pageviews' ][ 'diff' ] = ( $boxes_data[ 'Pageviews' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'Pageviews' ][ 'current' ] - $boxes_data[ 'Pageviews' ][ 'previous' ] ) / $boxes_data[ 'Pageviews' ][ 'previous' ] * 100, 2 ) : 100;
611
- $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'PageviewsPerSession' ][ 'current' ] - $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] ) / $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] * 100, 2 ) : 100;
612
- $boxes_data[ 'BounceRate' ][ 'diff' ] = ( $boxes_data[ 'BounceRate' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'BounceRate' ][ 'current' ] - $boxes_data[ 'BounceRate' ][ 'previous' ] ) / $boxes_data[ 'BounceRate' ][ 'previous' ] * 100, 2 ) : 100;
613
- $boxes_data[ 'Users' ][ 'diff' ] = ( $boxes_data[ 'Users' ][ 'previous' ] == 0 && $boxes_data[ 'Users' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Users' ][ 'diff' ];
614
- $boxes_data[ 'Pageviews' ][ 'diff' ] = ( $boxes_data[ 'Pageviews' ][ 'previous' ] == 0 && $boxes_data[ 'Pageviews' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Pageviews' ][ 'diff' ];
615
- $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] == 0 && $boxes_data[ 'PageviewsPerSession' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'PageviewsPerSession' ][ 'diff' ];
616
- $boxes_data[ 'BounceRate' ][ 'diff' ] = ( $boxes_data[ 'BounceRate' ][ 'previous' ] == 0 && $boxes_data[ 'BounceRate' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'BounceRate' ][ 'diff' ];
617
- $boxes_data[ 'Users' ][ 'label' ] = 'Users';
618
- $boxes_data[ 'Pageviews' ][ 'label' ] = 'Pageviews';
619
- $boxes_data[ 'PageviewsPerSession' ][ 'label' ] = 'Pages / Session';
620
- $boxes_data[ 'BounceRate' ][ 'label' ] = 'Bounce Rate';
621
- $boxes_data[ 'Users' ][ 'comparison' ] = $boxes_data[ 'Users' ][ 'current' ] . ' vs ' . $boxes_data[ 'Users' ][ 'previous' ];
622
- $boxes_data[ 'Pageviews' ][ 'comparison' ] = $boxes_data[ 'Pageviews' ][ 'current' ] . ' vs ' . $boxes_data[ 'Pageviews' ][ 'previous' ];
623
- $boxes_data[ 'PageviewsPerSession' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'current' ], 2, '.', ',' ) . ' vs ' . self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ], 2, '.', ',' );
624
- $boxes_data[ 'BounceRate' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'current' ], 2, '.', ',' ) . '% vs ' . self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'previous' ], 2, '.', ',' ) . '%';
625
- $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] > 0 ) ? 'green' : 'red';
626
- $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] > 0 ) ? 'green' : 'red';
627
- $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] > 0 ) ? 'green' : 'red';
628
- $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] > 0 ) ? 'red' : 'green';
629
- $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Users' ][ 'color' ] : 'black';
630
- $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Pageviews' ][ 'color' ] : 'black';
631
- $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] != 0 ) ? $boxes_data[ 'PageviewsPerSession' ][ 'color' ] : 'black';
632
- $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] != 0 ) ? $boxes_data[ 'BounceRate' ][ 'color' ] : 'black';
633
-
634
- return $boxes_data;
635
- }
636
-
637
- /**
638
- * Number format for boxes
639
- *
640
- * @param float $number Number to format
641
- * @param int $precision Precision
642
- * @param string $dec_point Decimal point
643
- * @param string $thousands_sep Thousands Separator
644
- *
645
- * @return string clean number format
646
- */
647
- public static function number_format_clean( $number, $precision = 0, $dec_point = '.', $thousands_sep = ',' ) {
648
- if ( $number == 0 ) {
649
- return 0;
650
- } else {
651
- $format = number_format( $number, $precision, $dec_point, $thousands_sep );
652
- if ( substr( $format, 2 ) == '.00' ) {
653
- return substr( $format, 0, - 3 );
654
- }
655
-
656
- return $format;
657
- }
658
- }
659
-
660
- /**
661
- * Get sources from analytics response data
662
- *
663
- * @param array $data Analytics response data
664
- *
665
- * @return array sources data
666
- */
667
- public static function get_sources( $data ) {
668
- if ( !empty( $data ) ) {
669
- $data = self::get_reports_from_response( $data );
670
- self::handle_more_reports( $data );
671
- $report = self::get_single_report( $data );
672
- self::get_report_column_header( $report );
673
- $report_data = self::get_report_data( $report );
674
- $rows = self::get_rows( $report_data );
675
- $totals = self::get_totals( $report_data );
676
- $totalCount = array();
677
- if ( !empty( $totals ) ) {
678
- foreach ( $totals as $key => $total ) {
679
- $totalCount = $total[ 'values' ][ 0 ];
680
- }
681
- }
682
- $sources = array(
683
- 'total' => $totalCount,
684
- 'sum' => 0,
685
- 'rows' => array(),
686
- );
687
- if ( !empty( $rows ) ) {
688
- $i = 1;
689
- foreach ( $rows as $row ) {
690
- if ( !empty( $row ) ) {
691
- foreach ( $row as $key => $value ) {
692
- if ( $key == 'dimensions' ) {
693
- $sources[ 'rows' ][ $i ][ 'name' ] = $value[ 0 ];
694
- $sources[ 'rows' ][ $i ][ 'url' ] = $value[ 0 ];
695
- } elseif ( $key == 'metrics' ) {
696
- $sources[ 'rows' ][ $i ][ 'number' ] = $value[ 0 ][ 'values' ][ 0 ];
697
- $sources[ 'rows' ][ $i ][ 'percent' ] = (!empty( $totalCount ) ) ? round( $value[ 0 ][ 'values' ][ 0 ] / $totalCount * 100, 2 ) : 0;
698
- $sources[ 'sum' ] += $value[ 0 ][ 'values' ][ 0 ];
699
- }
700
- }
701
- $i ++;
702
- }
703
- }
704
- }
705
-
706
- return $sources;
707
- }
708
-
709
- return false;
710
- }
711
-
712
- /**
713
- * Get dashboard boxes data from analytics response data
714
- *
715
- * @param array $data Analytics response data
716
- *
717
- * @return array dashboard boxes data
718
- */
719
- public static function get_dashboard_boxes_data( $data ) {
720
- if ( !empty( $data ) ) {
721
- $data = self::get_reports_from_response( $data );
722
- self::handle_more_reports( $data );
723
- $report = self::get_single_report( $data );
724
- self::get_report_column_header( $report );
725
- $report_data = self::get_report_data( $report );
726
- $totals = self::get_totals( $report_data );
727
- $boxes_data = array();
728
- $boxes_data[ 'Sessions' ] = array(
729
- 'label' => 'Visits',
730
- 'value' => $totals[ 0 ][ 'values' ][ 0 ],
731
- );
732
- $boxes_data[ 'Pageviews' ] = array(
733
- 'label' => 'Pageviews',
734
- 'value' => $totals[ 0 ][ 'values' ][ 1 ],
735
- );
736
- $boxes_data[ 'pageviewsPerSession' ] = array(
737
- 'label' => 'Pages / Visit',
738
- 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 2 ], 2, '.', ',' ),
739
- );
740
- $boxes_data[ 'BounceRate' ] = array(
741
- 'label' => 'Bounce Rate',
742
- 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 3 ], 2, '.', ',' ) . '%',
743
- );
744
- $boxes_data[ 'avgTimeOnPage' ] = array(
745
- 'label' => 'Avg. Time on Site',
746
- 'value' => gmdate( "H:i:s", $totals[ 0 ][ 'values' ][ 4 ] ),
747
- );
748
- $boxes_data[ 'percentNewSessions' ] = array(
749
- 'label' => '% of New Visits',
750
- 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 5 ], 2, '.', ',' ),
751
- );
752
-
753
- return $boxes_data;
754
- }
755
- }
756
-
757
- public static function get_empty_boxes_structure() {
758
- $boxes_data = array();
759
- $boxes_data[ 'Sessions' ] = array(
760
- 'label' => 'Visits',
761
- 'value' => 0,
762
- );
763
- $boxes_data[ 'Pageviews' ] = array(
764
- 'label' => 'Pageviews',
765
- 'value' => 0,
766
- );
767
- $boxes_data[ 'pageviewsPerSession' ] = array(
768
- 'label' => 'Pages / Visit',
769
- 'value' => self::number_format_clean( 0, 2, '.', ',' ),
770
- );
771
- $boxes_data[ 'BounceRate' ] = array(
772
- 'label' => 'Bounce Rate',
773
- 'value' => self::number_format_clean( 0, 2, '.', ',' ) . '%',
774
- );
775
- $boxes_data[ 'avgTimeOnPage' ] = array(
776
- 'label' => 'Avg. Time on Site',
777
- 'value' => gmdate( "H:i:s", 0 ),
778
- );
779
- $boxes_data[ 'percentNewSessions' ] = array(
780
- 'label' => '% of New Visits',
781
- 'value' => self::number_format_clean( 0, 2, '.', ',' ),
782
- );
783
-
784
- return $boxes_data;
785
- }
786
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Ga_Stats class
5
+ *
6
+ * Preparing request and parsing response from Google Analytics Reporting Api
7
+ *
8
+ * @author wle@adips.com
9
+ * @version 1.0
10
+ */
11
+ class Ga_Stats {
12
+
13
+ private $profile = array();
14
+
15
+ /**
16
+ * Primary class constructor.
17
+ *
18
+ * @access public
19
+ * @since 7.0.0
20
+ */
21
+ public function __construct() {
22
+ $this->profile = $this->get_analytics_profile();
23
+ }
24
+
25
+ /**
26
+ * Preparing query to get Analytics data
27
+ *
28
+ * @param string $query query type
29
+ * @param int $id_view The Analytics view ID from which to retrieve data.
30
+ * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
31
+ * @param string $metric A metric expression
32
+ *
33
+ * @return array Request query
34
+ */
35
+ public static function get_query( $query, $id_view, $date_range = null, $metric = null ) {
36
+ if ( $query == 'main_chart' ) {
37
+ return self::main_chart_query( $id_view, $date_range, $metric );
38
+ } elseif ( $query == 'gender' ) {
39
+ return self::gender_chart_query($id_view, $date_range, $metric);
40
+ } elseif ( $query == 'age' ) {
41
+ return self::age_chart_query($id_view, $date_range, $metric);
42
+ } elseif ( $query == 'boxes' ) {
43
+ return self::boxes_query( $id_view );
44
+ } elseif ( $query == 'dashboard_boxes' ) {
45
+ return self::dashboard_boxes_query( $id_view, $date_range );
46
+ } elseif ( $query == 'sources' ) {
47
+ return self::sources_query( $id_view );
48
+ } else {
49
+ return array();
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Preparing query for top traffic sources table
55
+ *
56
+ * @param int $id_view The Analytics view ID from which to retrieve data.
57
+ *
58
+ * @return array Sources query
59
+ */
60
+ public static function sources_query( $id_view ) {
61
+ $reports_requests = array();
62
+ $daysAgo = isset($_GET['th']) ? '30daysAgo' : '7daysAgo';
63
+
64
+ if ( isset( $_GET['ts'] ) ) {
65
+ $reports_requests[] = array(
66
+ 'viewId' => $id_view,
67
+ 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
68
+ 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
69
+ 'includeEmptyRows' => true,
70
+ 'pageSize' => 10,
71
+ 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
72
+ 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
73
+ );
74
+ } else {
75
+ $reports_requests[] = array(
76
+ 'viewId' => $id_view,
77
+ 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
78
+ 'metrics' => self::set_metrics( array(
79
+ 'ga:pageviews',
80
+ 'ga:uniquePageviews',
81
+ 'ga:timeOnPage',
82
+ 'ga:bounces',
83
+ 'ga:entrances',
84
+ 'ga:exits'
85
+ ) ),
86
+ 'includeEmptyRows' => true,
87
+ 'pageSize' => 10,
88
+ 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
89
+ 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
90
+ );
91
+ }
92
+
93
+ $query = array(
94
+ 'reportRequests' => $reports_requests
95
+ );
96
+
97
+ return $query;
98
+ }
99
+
100
+ /**
101
+ * Preparing query for dashbord boxes
102
+ *
103
+ * @param int $id_view The Analytics view ID from which to retrieve data.
104
+ * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
105
+ *
106
+ * @return array Dashboard boxes query
107
+ */
108
+ public static function dashboard_boxes_query( $id_view, $date_range ) {
109
+ $reports_requests = array();
110
+ $daysAgo = isset($_GET['th']) ? '30daysAgo' : '7daysAgo';
111
+
112
+ if ( isset( $_GET['ts'] ) ) {
113
+ $reports_requests[] = array(
114
+ 'viewId' => $id_view,
115
+ 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
116
+ 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
117
+ 'includeEmptyRows' => true,
118
+ 'pageSize' => 10,
119
+ 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
120
+ 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
121
+ );
122
+ } else {
123
+ $reports_requests[] = array(
124
+ 'viewId' => $id_view,
125
+ 'dateRanges' => self::set_date_ranges( $daysAgo, 'yesterday' ),
126
+ 'metrics' => self::set_metrics( array(
127
+ 'ga:pageviews',
128
+ 'ga:uniquePageviews',
129
+ 'ga:timeOnPage',
130
+ 'ga:bounces',
131
+ 'ga:entrances',
132
+ 'ga:exits'
133
+ ) ),
134
+ 'includeEmptyRows' => true,
135
+ 'pageSize' => 10,
136
+ 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
137
+ 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
138
+ );
139
+ }
140
+ $query = array(
141
+ 'reportRequests' => $reports_requests
142
+ );
143
+
144
+ return $query;
145
+ }
146
+
147
+ /**
148
+ * Preparing query for stats boxes
149
+ *
150
+ * @param int $id_view The Analytics view ID from which to retrieve data.
151
+ *
152
+ * @return array Boxes query
153
+ */
154
+ public static function boxes_query( $id_view ) {
155
+ $range = isset( $_GET['th'] ) ? '30daysAgo' : '7daysAgo';
156
+ $range_s_prev = isset( $_GET['th'] ) ? '60daysAgo' : '14daysAgo';
157
+ $range_e_prev = isset( $_GET['th'] ) ? '31daysAgo' : '8daysAgo';
158
+ $reports_requests = array();
159
+ $reports_requests[] = array(
160
+ 'viewId' => $id_view,
161
+ 'dateRanges' => self::set_date_ranges( $range, 'yesterday', $range_s_prev, $range_e_prev ),
162
+ 'metrics' => self::set_metrics( array(
163
+ 'ga:users',
164
+ 'ga:pageviews',
165
+ 'ga:pageviewsPerSession',
166
+ 'ga:BounceRate'
167
+ ) ),
168
+ 'includeEmptyRows' => true,
169
+ 'dimensions' => self::set_dimensions( 'ga:date' )
170
+ );
171
+ $query = array(
172
+ 'reportRequests' => $reports_requests
173
+ );
174
+
175
+ return $query;
176
+ }
177
+
178
+ /**
179
+ * Preparing query for chart
180
+ *
181
+ * @param int $id_view The Analytics view ID from which to retrieve data.
182
+ * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
183
+ * @param string $metric A metric expression
184
+ *
185
+ * @return array Chart query
186
+ */
187
+ public static function main_chart_query( $id_view, $date_range = null, $metric = null ) {
188
+ if ( empty( $date_range ) ) {
189
+ $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
190
+ } else {
191
+ $date_ranges = self::set_date_ranges( $date_range, 'yesterday', '14daysAgo', '8daysAgo' );
192
+ }
193
+
194
+ if ( empty( $metric ) ) {
195
+ $metric = 'ga:pageviews';
196
+ } else {
197
+ $metric = 'ga:' . $metric;
198
+ }
199
+
200
+ $reports_requests = array();
201
+ $reports_requests[] = array(
202
+ 'viewId' => $id_view,
203
+ 'dateRanges' => $date_ranges,
204
+ 'metrics' => self::set_metrics( $metric ),
205
+ 'includeEmptyRows' => true,
206
+ 'dimensions' => self::set_dimensions( 'ga:date' )
207
+ );
208
+ $query = array(
209
+ 'reportRequests' => $reports_requests
210
+ );
211
+
212
+ return $query;
213
+ }
214
+
215
+ /**
216
+ * Preparing query for gender chart
217
+ *
218
+ * @param int $id_view The Analytics view ID from which to retrieve data.
219
+ * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
220
+ * @param string $metric A metric expression
221
+ *
222
+ * @return array Chart query
223
+ */
224
+ public static function gender_chart_query( $id_view, $date_range = null, $metric = null ) {
225
+ if ( empty( $date_range ) ) {
226
+ $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
227
+ } else {
228
+ $date_ranges = self::set_date_ranges( $date_range, 'yesterday', '14daysAgo', '8daysAgo' );
229
+ }
230
+
231
+ $reports_requests = array();
232
+ $reports_requests[] = array(
233
+ 'viewId' => $id_view,
234
+ 'dateRanges' => $date_ranges,
235
+ 'metrics' => self::set_metrics( 'ga:sessions' ),
236
+ 'includeEmptyRows' => true,
237
+ 'dimensions' => self::set_dimensions( 'ga:userGender' )
238
+ );
239
+ $query = array(
240
+ 'reportRequests' => $reports_requests
241
+ );
242
+
243
+ return $query;
244
+ }
245
+
246
+ /**
247
+ * Preparing query for age chart
248
+ *
249
+ * @param int $id_view The Analytics view ID from which to retrieve data.
250
+ * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'
251
+ * @param string $metric A metric expression
252
+ *
253
+ * @return array Chart query
254
+ */
255
+ public static function age_chart_query( $id_view, $date_range = null, $metric = null ) {
256
+ if ( empty( $date_range ) ) {
257
+ $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
258
+ } else {
259
+ $date_ranges = self::set_date_ranges( $date_range, 'yesterday', '14daysAgo', '8daysAgo' );
260
+ }
261
+
262
+ $reports_requests = array();
263
+ $reports_requests[] = array(
264
+ 'viewId' => $id_view,
265
+ 'dateRanges' => $date_ranges,
266
+ 'metrics' => self::set_metrics( 'ga:sessions' ),
267
+ 'includeEmptyRows' => true,
268
+ 'dimensions' => self::set_dimensions( 'ga:userAgeBracket' )
269
+ );
270
+ $query = array(
271
+ 'reportRequests' => $reports_requests
272
+ );
273
+
274
+ return $query;
275
+ }
276
+
277
+ /**
278
+ * Setting order for requests
279
+ *
280
+ * @param string $name The field which to sort by. The default sort order is ascending. Example: ga:browser.
281
+ * @param string $sort The sorting order for the field. 'ASCENDING' or 'DESCENDING'
282
+ *
283
+ * @return array OrderBys
284
+ */
285
+ public static function set_order_bys( $name, $sort ) {
286
+ $order = array();
287
+ $order[] = array(
288
+ 'fieldName' => $name,
289
+ 'sortOrder' => $sort,
290
+ );
291
+
292
+ return $order;
293
+ }
294
+
295
+ /**
296
+ * Setting metrics for requests
297
+ *
298
+ * @param mixed $expression A metric expression or array of expressions
299
+ *
300
+ * @return array Metrics
301
+ */
302
+ public static function set_metrics( $expression ) {
303
+ $metrics = array();
304
+ if ( is_array( $expression ) ) {
305
+ foreach ( $expression as $exp ) {
306
+ $metrics[] = array(
307
+ 'expression' => $exp
308
+ );
309
+ }
310
+ } else {
311
+ $metrics[] = array(
312
+ 'expression' => $expression
313
+ );
314
+ }
315
+
316
+ return $metrics;
317
+ }
318
+
319
+ /**
320
+ * Setting dimensions for requests
321
+ *
322
+ * @param string $name Name of the dimension to fetch, for example ga:browser.
323
+ *
324
+ * @return array Dimensions
325
+ */
326
+ public static function set_dimensions( $name ) {
327
+ $dimensions = array();
328
+ $dimensions[] = array(
329
+ 'name' => $name
330
+ );
331
+
332
+ return $dimensions;
333
+ }
334
+
335
+ /**
336
+ * Setting date ranges for requests
337
+ *
338
+ * @param string $start_date The start date for the query in the format YYYY-MM-DD.
339
+ * @param string $end_date The end date for the query in the format YYYY-MM-DD.
340
+ * @param string $prev_start_date The start date (second range) for the query in the format YYYY-MM-DD.
341
+ * @param string $prev_end_date The start date (second range) for the query in the format YYYY-MM-DD.
342
+ *
343
+ * @return array Date ranges
344
+ */
345
+ public static function set_date_ranges( $start_date, $end_date, $prev_start_date = '', $prev_end_date = '' ) {
346
+ $date_danges = array();
347
+ $date_danges[] = array(
348
+ 'startDate' => $start_date,
349
+ 'endDate' => $end_date
350
+ );
351
+ if ( !empty( $prev_start_date ) and ! empty( $prev_end_date ) ) {
352
+ $date_danges[] = array(
353
+ 'startDate' => $prev_start_date,
354
+ 'endDate' => $prev_end_date
355
+ );
356
+ }
357
+
358
+ return $date_danges;
359
+ }
360
+
361
+ /**
362
+ * Preparing response for data received from analytics
363
+ *
364
+ * @param array $data Analytics response
365
+ *
366
+ * @return array Response rows
367
+ */
368
+ public static function prepare_response( $data ) {
369
+ $data = self::get_reports_from_response( $data );
370
+ self::handle_more_reports( $data );
371
+ $report = self::get_single_report( $data );
372
+ self::get_report_column_header( $report );
373
+ $report_data = self::get_report_data( $report );
374
+ self::get_totals( $report_data );
375
+ self::get_row_count( $report_data );
376
+ $rows = self::get_rows( $report_data );
377
+
378
+ return $rows;
379
+ }
380
+
381
+ /**
382
+ * Get dimensions from response row
383
+ *
384
+ * @param array $row Analytics response row
385
+ *
386
+ * @return array Dimensions
387
+ */
388
+ public static function get_dimensions( $row ) {
389
+ if ( !empty( $row[ 'dimensions' ] ) ) {
390
+ return $row[ 'dimensions' ];
391
+ }
392
+
393
+ return false;
394
+ }
395
+
396
+ /**
397
+ * Get metrics from response row
398
+ *
399
+ * @param array $row Analytics response row
400
+ *
401
+ * @return array Metrics
402
+ */
403
+ public static function get_metrics( $row ) {
404
+ if ( !empty( $row[ 'metrics' ] ) ) {
405
+ return $row[ 'metrics' ];
406
+ }
407
+
408
+ return false;
409
+ }
410
+
411
+ /**
412
+ * Get row from response report data
413
+ *
414
+ * @param array $report_data Analytics response report data
415
+ *
416
+ * @return array Rows
417
+ */
418
+ public static function get_rows( $report_data ) {
419
+ if ( !empty( $report_data[ 'rows' ] ) ) {
420
+ return $report_data[ 'rows' ];
421
+ }
422
+
423
+ return false;
424
+ }
425
+
426
+ /**
427
+ * Get row count from response report data
428
+ *
429
+ * @param array $report_data Analytics response report data
430
+ *
431
+ * @return array Row count
432
+ */
433
+ public static function get_row_count( $report_data ) {
434
+ if ( !empty( $report_data[ 'rowCount' ] ) ) {
435
+ return $report_data[ 'rowCount' ];
436
+ }
437
+
438
+ return false;
439
+ }
440
+
441
+ /**
442
+ * Get totals from response report data
443
+ *
444
+ * @param array $report_data Analytics response report data
445
+ *
446
+ * @return array Totals
447
+ */
448
+ public static function get_totals( $report_data ) {
449
+ if ( !empty( $report_data[ 'totals' ] ) ) {
450
+ return $report_data[ 'totals' ];
451
+ }
452
+
453
+ return false;
454
+ }
455
+
456
+ /**
457
+ * Get reports from response data
458
+ *
459
+ * @param array $data Analytics response data
460
+ *
461
+ * @return array Reports
462
+ */
463
+ public static function get_reports_from_response( $data ) {
464
+ if ( !empty( $data[ 'reports' ] ) ) {
465
+ return $data[ 'reports' ];
466
+ }
467
+
468
+ return false;
469
+ }
470
+
471
+ /**
472
+ * Show info for multiple data
473
+ *
474
+ * @param array $data Analytics response data
475
+ *
476
+ */
477
+ public static function handle_more_reports( $data ) {
478
+ if ( count( $data ) > 1 ) {
479
+ echo 'more than one report';
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Show info for multiple rows
485
+ *
486
+ * @param array $rows Analytics response rows
487
+ *
488
+ */
489
+ public static function handle_more_rows( $rows ) {
490
+ if ( count( $rows ) > 1 ) {
491
+ echo 'more than one row';
492
+ }
493
+ }
494
+
495
+ /**
496
+ * Get single report from response data
497
+ *
498
+ * @param array $data Analytics response data
499
+ *
500
+ * @return array Report
501
+ */
502
+ public static function get_single_report( $data ) {
503
+ if ( !empty( $data ) ) {
504
+ foreach ( $data as $report ) {
505
+ if ( !empty( $report ) ) {
506
+ return $report;
507
+ }
508
+ }
509
+ }
510
+
511
+ return false;
512
+ }
513
+
514
+ /**
515
+ * Get single row from response data rows
516
+ *
517
+ * @param array $rows Analytics response data rows
518
+ *
519
+ * @return array Row
520
+ */
521
+ public static function get_single_row( $rows ) {
522
+ if ( !empty( $rows ) ) {
523
+ foreach ( $rows as $row ) {
524
+ if ( !empty( $row ) ) {
525
+ return $row;
526
+ }
527
+ }
528
+ }
529
+
530
+ return false;
531
+ }
532
+
533
+ /**
534
+ * Get column header from response data
535
+ *
536
+ * @param array $data Analytics response data
537
+ *
538
+ * @return array Column header
539
+ */
540
+ public static function get_report_column_header( $data ) {
541
+ if ( !empty( $data[ 'columnHeader' ] ) ) {
542
+ return $data[ 'columnHeader' ];
543
+ }
544
+
545
+ return false;
546
+ }
547
+
548
+ /**
549
+ * Get report data from response data
550
+ *
551
+ * @param array $data Analytics response data
552
+ *
553
+ * @return array data
554
+ */
555
+ public static function get_report_data( $data ) {
556
+ if ( !empty( $data[ 'data' ] ) ) {
557
+ return $data[ 'data' ];
558
+ }
559
+
560
+ return false;
561
+ }
562
+
563
+ /**
564
+ * Get chart from response data
565
+ *
566
+ * @param array $response_data Analytics response data
567
+ *
568
+ * @return array chart data
569
+ */
570
+ public static function get_chart( $response_data ) {
571
+ $chart_data = array();
572
+ if ( !empty( $response_data ) ) {
573
+ $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
574
+ $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
575
+ if ( !empty( $rows ) ) {
576
+ foreach ( $rows as $key => $row ) {
577
+ if ( $key < 7 ) {
578
+ $chart_data[ $key ][ 'previous' ] = !empty( $row[ 'metrics' ][ 1 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 1 ][ 'values' ][ 0 ] : 0;
579
+ $chart_data[ $key ][ 'previous-day' ] = date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) );
580
+ } else {
581
+ $chart_data[ $key - 7 ][ 'day' ] = date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) );
582
+ $chart_data[ $key - 7 ][ 'current' ] = !empty( $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] : 0;
583
+ $chart_data[ 'date' ] = strtotime( $row[ 'dimensions' ][ 0 ] );
584
+ }
585
+ }
586
+ }
587
+ }
588
+
589
+ return $chart_data;
590
+ }
591
+
592
+ /**
593
+ * Get gender chart from response data
594
+ *
595
+ * @param array $response_data Analytics response data
596
+ *
597
+ * @return array chart data
598
+ */
599
+ public static function get_gender_chart( $response_data ) {
600
+ $chart_data = [];
601
+ if ( !empty( $response_data ) ) {
602
+ $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
603
+ $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
604
+ if ( !empty( $rows ) ) {
605
+ foreach ( $rows as $key => $row ) {
606
+ $chart_data[$row['dimensions'][0]] = self::getGenderValue($row['metrics']);
607
+ }
608
+ }
609
+ }
610
+
611
+ return $chart_data;
612
+ }
613
+
614
+ /**
615
+ * Get the value of gender response.
616
+ *
617
+ * @param $metrics
618
+ */
619
+ private static function getGenderValue($metrics)
620
+ {
621
+ if (is_array($metrics)) {
622
+ foreach($metrics as $metric) {
623
+ $values[] = $metric['values'][0];
624
+ }
625
+ }
626
+
627
+ return $values[0];
628
+ }
629
+
630
+ /**
631
+ * Get gender chart from response data
632
+ *
633
+ * @param array $response_data Analytics response data
634
+ *
635
+ * @return array chart data
636
+ */
637
+ public static function get_age_chart( $response_data ) {
638
+ $chart_data = [];
639
+ if ( !empty( $response_data ) ) {
640
+ $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
641
+ $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
642
+ if ( !empty( $rows ) ) {
643
+ foreach ( $rows as $key => $row ) {
644
+ $chart_data[$row['dimensions'][0]] = self::getAgeValue($row['metrics']);
645
+ }
646
+ }
647
+ }
648
+
649
+ return $chart_data;
650
+ }
651
+
652
+ /**
653
+ * Get the value of gender response.
654
+ *
655
+ * @param $metrics
656
+ */
657
+ private static function getAgeValue($metrics)
658
+ {
659
+ if (is_array($metrics)) {
660
+ foreach($metrics as $metric) {
661
+ $values[] = $metric['values'][0];
662
+ }
663
+ }
664
+
665
+ return $values[0];
666
+ }
667
+
668
+ /**
669
+ * Get dasboard chart from response data
670
+ *
671
+ * @param array $response_data Analytics response data
672
+ *
673
+ * @return array dashboard chart data
674
+ */
675
+ public static function get_dashboard_chart( $response_data ) {
676
+ $chart_data = array();
677
+ if ( !empty( $response_data ) ) {
678
+ $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
679
+ $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
680
+ if ( !empty( $rows ) ) {
681
+ foreach ( $rows as $row ) {
682
+ $chart_data[] = array(
683
+ 'day' => date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) ),
684
+ 'current' => !empty( $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] : 0
685
+ );
686
+ }
687
+ }
688
+ }
689
+
690
+ return $chart_data;
691
+ }
692
+
693
+ /**
694
+ * Get boxes from response data
695
+ *
696
+ * @param array $data Analytics response data
697
+ *
698
+ * @return array boxes data
699
+ */
700
+ public static function get_boxes( $data ) {
701
+ if ( !empty( $data ) ) {
702
+ $data = self::get_reports_from_response( $data );
703
+ self::handle_more_reports( $data );
704
+ $report = self::get_single_report( $data );
705
+ self::get_report_column_header( $report );
706
+ $report_data = self::get_report_data( $report );
707
+ $totals = self::get_totals( $report_data );
708
+
709
+ return self::get_boxes_from_totals( $totals );
710
+ }
711
+ }
712
+
713
+ /**
714
+ * Get boxes from totals
715
+ *
716
+ * @param array $totals Analytics response totals
717
+ *
718
+ * @return array boxes data
719
+ */
720
+ public static function get_boxes_from_totals( $totals ) {
721
+ if ( !empty( $totals ) ) {
722
+ $boxes_data = array();
723
+ foreach ( $totals as $key => $total ) {
724
+ if ( $key == 0 ) {
725
+ $boxes_data[ 'Users' ][ 'current' ] = $total[ 'values' ][ 0 ];
726
+ $boxes_data[ 'Pageviews' ][ 'current' ] = $total[ 'values' ][ 1 ];
727
+ $boxes_data[ 'PageviewsPerSession' ][ 'current' ] = $total[ 'values' ][ 2 ];
728
+ $boxes_data[ 'BounceRate' ][ 'current' ] = round( $total[ 'values' ][ 3 ], 2 );
729
+ } else {
730
+ $boxes_data[ 'Users' ][ 'previous' ] = $total[ 'values' ][ 0 ];
731
+ $boxes_data[ 'Pageviews' ][ 'previous' ] = $total[ 'values' ][ 1 ];
732
+ $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] = $total[ 'values' ][ 2 ];
733
+ $boxes_data[ 'BounceRate' ][ 'previous' ] = round( $total[ 'values' ][ 3 ], 2 );
734
+ }
735
+ }
736
+
737
+ return self::prepare_boxes( $boxes_data );
738
+ }
739
+
740
+ return false;
741
+ }
742
+
743
+ /**
744
+ * Prepare boxes data
745
+ *
746
+ * @param array $boxes_data Boxes data
747
+ *
748
+ * @return array boxes data
749
+ */
750
+ public static function prepare_boxes( $boxes_data ) {
751
+ $boxes_data[ 'Users' ][ 'diff' ] = ( $boxes_data[ 'Users' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'Users' ][ 'current' ] - $boxes_data[ 'Users' ][ 'previous' ] ) / $boxes_data[ 'Users' ][ 'previous' ] * 100, 2 ) : 100;
752
+ $boxes_data[ 'Pageviews' ][ 'diff' ] = ( $boxes_data[ 'Pageviews' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'Pageviews' ][ 'current' ] - $boxes_data[ 'Pageviews' ][ 'previous' ] ) / $boxes_data[ 'Pageviews' ][ 'previous' ] * 100, 2 ) : 100;
753
+ $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'PageviewsPerSession' ][ 'current' ] - $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] ) / $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] * 100, 2 ) : 100;
754
+ $boxes_data[ 'BounceRate' ][ 'diff' ] = ( $boxes_data[ 'BounceRate' ][ 'previous' ] > 0 ) ? round( ( $boxes_data[ 'BounceRate' ][ 'current' ] - $boxes_data[ 'BounceRate' ][ 'previous' ] ) / $boxes_data[ 'BounceRate' ][ 'previous' ] * 100, 2 ) : 100;
755
+ $boxes_data[ 'Users' ][ 'diff' ] = ( $boxes_data[ 'Users' ][ 'previous' ] == 0 && $boxes_data[ 'Users' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Users' ][ 'diff' ];
756
+ $boxes_data[ 'Pageviews' ][ 'diff' ] = ( $boxes_data[ 'Pageviews' ][ 'previous' ] == 0 && $boxes_data[ 'Pageviews' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Pageviews' ][ 'diff' ];
757
+ $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] == 0 && $boxes_data[ 'PageviewsPerSession' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'PageviewsPerSession' ][ 'diff' ];
758
+ $boxes_data[ 'BounceRate' ][ 'diff' ] = ( $boxes_data[ 'BounceRate' ][ 'previous' ] == 0 && $boxes_data[ 'BounceRate' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'BounceRate' ][ 'diff' ];
759
+ $boxes_data[ 'Users' ][ 'label' ] = 'Users';
760
+ $boxes_data[ 'Pageviews' ][ 'label' ] = 'Pageviews';
761
+ $boxes_data[ 'PageviewsPerSession' ][ 'label' ] = 'Pages / Session';
762
+ $boxes_data[ 'BounceRate' ][ 'label' ] = 'Bounce Rate';
763
+ $boxes_data[ 'Users' ][ 'comparison' ] = $boxes_data[ 'Users' ][ 'current' ] . ' vs ' . $boxes_data[ 'Users' ][ 'previous' ];
764
+ $boxes_data[ 'Pageviews' ][ 'comparison' ] = $boxes_data[ 'Pageviews' ][ 'current' ] . ' vs ' . $boxes_data[ 'Pageviews' ][ 'previous' ];
765
+ $boxes_data[ 'PageviewsPerSession' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'current' ], 2, '.', ',' ) . ' vs ' . self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ], 2, '.', ',' );
766
+ $boxes_data[ 'BounceRate' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'current' ], 2, '.', ',' ) . '% vs ' . self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'previous' ], 2, '.', ',' ) . '%';
767
+ $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] > 0 ) ? 'green' : 'red';
768
+ $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] > 0 ) ? 'green' : 'red';
769
+ $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] > 0 ) ? 'green' : 'red';
770
+ $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] > 0 ) ? 'red' : 'green';
771
+ $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Users' ][ 'color' ] : 'black';
772
+ $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Pageviews' ][ 'color' ] : 'black';
773
+ $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] != 0 ) ? $boxes_data[ 'PageviewsPerSession' ][ 'color' ] : 'black';
774
+ $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] != 0 ) ? $boxes_data[ 'BounceRate' ][ 'color' ] : 'black';
775
+
776
+ return $boxes_data;
777
+ }
778
+
779
+ /**
780
+ * Number format for boxes
781
+ *
782
+ * @param float $number Number to format
783
+ * @param int $precision Precision
784
+ * @param string $dec_point Decimal point
785
+ * @param string $thousands_sep Thousands Separator
786
+ *
787
+ * @return string clean number format
788
+ */
789
+ public static function number_format_clean( $number, $precision = 0, $dec_point = '.', $thousands_sep = ',' ) {
790
+ if ( $number == 0 ) {
791
+ return 0;
792
+ } else {
793
+ $format = number_format( $number, $precision, $dec_point, $thousands_sep );
794
+ if ( substr( $format, 2 ) == '.00' ) {
795
+ return substr( $format, 0, - 3 );
796
+ }
797
+
798
+ return $format;
799
+ }
800
+ }
801
+
802
+ /**
803
+ * Get sources from analytics response data
804
+ *
805
+ * @param array $data Analytics response data
806
+ *
807
+ * @return array sources data
808
+ */
809
+ public static function get_sources( $data ) {
810
+ if ( !empty( $data ) ) {
811
+ $data = self::get_reports_from_response( $data );
812
+ self::handle_more_reports( $data );
813
+ $report = self::get_single_report( $data );
814
+ self::get_report_column_header( $report );
815
+ $report_data = self::get_report_data( $report );
816
+ $rows = self::get_rows( $report_data );
817
+ $totals = self::get_totals( $report_data );
818
+ $totalCount = array();
819
+ if ( !empty( $totals ) ) {
820
+ foreach ( $totals as $key => $total ) {
821
+ $totalCount = $total[ 'values' ][ 0 ];
822
+ }
823
+ }
824
+ $sources = array(
825
+ 'total' => $totalCount,
826
+ 'sum' => 0,
827
+ 'rows' => array(),
828
+ );
829
+ if ( !empty( $rows ) ) {
830
+ $i = 1;
831
+ foreach ( $rows as $row ) {
832
+ if ( !empty( $row ) ) {
833
+ foreach ( $row as $key => $value ) {
834
+ if ( $key == 'dimensions' ) {
835
+ $sources[ 'rows' ][ $i ][ 'name' ] = $value[ 0 ];
836
+ $sources[ 'rows' ][ $i ][ 'url' ] = $value[ 0 ];
837
+ } elseif ( $key == 'metrics' ) {
838
+ $sources[ 'rows' ][ $i ][ 'number' ] = $value[ 0 ][ 'values' ][ 0 ];
839
+ $sources[ 'rows' ][ $i ][ 'percent' ] = (!empty( $totalCount ) ) ? round( $value[ 0 ][ 'values' ][ 0 ] / $totalCount * 100, 2 ) : 0;
840
+ $sources[ 'sum' ] += $value[ 0 ][ 'values' ][ 0 ];
841
+ }
842
+ }
843
+ $i ++;
844
+ }
845
+ }
846
+ }
847
+
848
+ return $sources;
849
+ }
850
+
851
+ return false;
852
+ }
853
+
854
+ /**
855
+ * Get dashboard boxes data from analytics response data
856
+ *
857
+ * @param array $data Analytics response data
858
+ *
859
+ * @return array dashboard boxes data
860
+ */
861
+ public static function get_dashboard_boxes_data( $data ) {
862
+ if ( !empty( $data ) ) {
863
+ $data = self::get_reports_from_response( $data );
864
+ self::handle_more_reports( $data );
865
+ $report = self::get_single_report( $data );
866
+ self::get_report_column_header( $report );
867
+ $report_data = self::get_report_data( $report );
868
+ $totals = self::get_totals( $report_data );
869
+ $boxes_data = array();
870
+ $boxes_data[ 'Sessions' ] = array(
871
+ 'label' => 'Visits',
872
+ 'value' => $totals[ 0 ][ 'values' ][ 0 ],
873
+ );
874
+ $boxes_data[ 'Pageviews' ] = array(
875
+ 'label' => 'Pageviews',
876
+ 'value' => $totals[ 0 ][ 'values' ][ 1 ],
877
+ );
878
+ $boxes_data[ 'pageviewsPerSession' ] = array(
879
+ 'label' => 'Pages / Visit',
880
+ 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 2 ], 2, '.', ',' ),
881
+ );
882
+ $boxes_data[ 'BounceRate' ] = array(
883
+ 'label' => 'Bounce Rate',
884
+ 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 3 ], 2, '.', ',' ) . '%',
885
+ );
886
+ $boxes_data[ 'avgTimeOnPage' ] = array(
887
+ 'label' => 'Avg. Time on Site',
888
+ 'value' => gmdate( "H:i:s", $totals[ 0 ][ 'values' ][ 4 ] ),
889
+ );
890
+ $boxes_data[ 'percentNewSessions' ] = array(
891
+ 'label' => '% of New Visits',
892
+ 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 5 ], 2, '.', ',' ),
893
+ );
894
+
895
+ return $boxes_data;
896
+ }
897
+ }
898
+
899
+ public static function get_empty_boxes_structure() {
900
+ $boxes_data = array();
901
+ $boxes_data[ 'Sessions' ] = array(
902
+ 'label' => 'Visits',
903
+ 'value' => 0,
904
+ );
905
+ $boxes_data[ 'Pageviews' ] = array(
906
+ 'label' => 'Pageviews',
907
+ 'value' => 0,
908
+ );
909
+ $boxes_data[ 'pageviewsPerSession' ] = array(
910
+ 'label' => 'Pages / Visit',
911
+ 'value' => self::number_format_clean( 0, 2, '.', ',' ),
912
+ );
913
+ $boxes_data[ 'BounceRate' ] = array(
914
+ 'label' => 'Bounce Rate',
915
+ 'value' => self::number_format_clean( 0, 2, '.', ',' ) . '%',
916
+ );
917
+ $boxes_data[ 'avgTimeOnPage' ] = array(
918
+ 'label' => 'Avg. Time on Site',
919
+ 'value' => gmdate( "H:i:s", 0 ),
920
+ );
921
+ $boxes_data[ 'percentNewSessions' ] = array(
922
+ 'label' => '% of New Visits',
923
+ 'value' => self::number_format_clean( 0, 2, '.', ',' ),
924
+ );
925
+
926
+ return $boxes_data;
927
+ }
928
+ }
class/core/Ga_View_Core.php CHANGED
@@ -22,6 +22,7 @@ class Ga_View_Core {
22
  foreach ( $data_array as $k => $v ) {
23
  $$k = $v;
24
  }
 
25
  ob_start();
26
  include GA_PLUGIN_DIR . "/" . self::PATH . "/" . $view . ".php";
27
  if ( $html ) {
22
  foreach ( $data_array as $k => $v ) {
23
  $$k = $v;
24
  }
25
+
26
  ob_start();
27
  include GA_PLUGIN_DIR . "/" . self::PATH . "/" . $view . ".php";
28
  if ( $html ) {
css/ga_additional.css CHANGED
@@ -7,7 +7,7 @@
7
  background-color: transparent;
8
  width: 100%;
9
  max-width: 100%;
10
- margin-bottom: 20px;
11
  border: 1px solid #dddddd;
12
  }
13
 
@@ -121,7 +121,7 @@
121
  .ga-panel > .ga-table:last-child > tbody:last-child > tr:last-child td:first-child,
122
  .ga-panel > .ga-table:last-child > tbody:last-child > tr:last-child th:first-child {
123
  border-bottom-left-radius: 3px;
124
- border-bottom-right-radius: 3px;
125
  }
126
  .ga-panel > .ga-panel-body + .ga-table,
127
  .ga-panel > .ga-table + .ga-panel-body {
@@ -152,7 +152,7 @@
152
  }
153
  .ga-col-name{
154
  width: 20%;
155
- overflow: hidden;
156
  text-overflow: ellipsis;
157
  word-wrap: break-word;
158
  }
@@ -189,13 +189,13 @@
189
  .ga-box-comparison{
190
  color: grey;
191
  font-size: 12px;
192
- overflow: hidden;
193
  text-overflow: ellipsis;
194
- white-space: nowrap;
195
  }
196
  .ga-box-label{
197
  font-size: 14px;
198
- overflow: hidden;
199
  text-overflow: ellipsis;
200
  white-space: nowrap;
201
  }
@@ -214,7 +214,7 @@
214
  }
215
  .ga-box-comparison{
216
  font-size: 16px
217
- }
218
  .ga-box-label{
219
  font-size: 16px
220
  }
@@ -226,8 +226,8 @@
226
  }
227
  .ga-chart {
228
  width:300px;
229
- overflow: hidden;
230
- text-overflow: ellipsis;
231
  }
232
  }
233
  .ga-source-name{
@@ -322,4 +322,4 @@ input:checked + .ga-slider:before {
322
 
323
  .ga-slider.round:before {
324
  border-radius: 50%;
325
- }
7
  background-color: transparent;
8
  width: 100%;
9
  max-width: 100%;
10
+ margin-bottom: 20px;
11
  border: 1px solid #dddddd;
12
  }
13
 
121
  .ga-panel > .ga-table:last-child > tbody:last-child > tr:last-child td:first-child,
122
  .ga-panel > .ga-table:last-child > tbody:last-child > tr:last-child th:first-child {
123
  border-bottom-left-radius: 3px;
124
+ border-bottom-right-radius: 3px;
125
  }
126
  .ga-panel > .ga-panel-body + .ga-table,
127
  .ga-panel > .ga-table + .ga-panel-body {
152
  }
153
  .ga-col-name{
154
  width: 20%;
155
+ overflow: hidden;
156
  text-overflow: ellipsis;
157
  word-wrap: break-word;
158
  }
189
  .ga-box-comparison{
190
  color: grey;
191
  font-size: 12px;
192
+ overflow: hidden;
193
  text-overflow: ellipsis;
194
+ white-space: nowrap;
195
  }
196
  .ga-box-label{
197
  font-size: 14px;
198
+ overflow: hidden;
199
  text-overflow: ellipsis;
200
  white-space: nowrap;
201
  }
214
  }
215
  .ga-box-comparison{
216
  font-size: 16px
217
+ }
218
  .ga-box-label{
219
  font-size: 16px
220
  }
226
  }
227
  .ga-chart {
228
  width:300px;
229
+ overflow: hidden;
230
+ text-overflow: ellipsis;
231
  }
232
  }
233
  .ga-source-name{
322
 
323
  .ga-slider.round:before {
324
  border-radius: 50%;
325
+ }
css/googleanalytics.css CHANGED
@@ -1,263 +1,615 @@
1
- @CHARSET "UTF-8";
2
-
3
- .wrap.ga-wrap {
4
- margin: 0px auto !important;
5
- margin-top: 10px !important;
6
- margin-right: 0px !important;
7
- font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
8
- font-size: 14px;
9
- line-height: 1.42857143;
10
- color: #333333;
11
- }
12
-
13
- .ga_container {
14
- margin-top: 25px;
15
- }
16
-
17
- .ga_container a.view-report {
18
- background: #c6dafc;
19
- width: 200px;
20
- display: inline-block;
21
- padding: 10px 20px;
22
- border-radius: 5px;
23
- text-decoration: none;
24
- font-size: 15px;
25
- font-weight: bold;
26
- text-align: center;
27
- margin: 10px 5px 10px 0;
28
- }
29
-
30
- .ga_container .filter-choices a {
31
- background: #f9f9f9;
32
- width: 100px;
33
- display: inline-block;
34
- padding: 10px 20px;
35
- border-radius: 5px;
36
- text-decoration: none;
37
- font-size: 15px;
38
- font-weight: bold;
39
- text-align: center;
40
- margin: 10px 5px 10px 0;
41
- color: #888888;
42
- }
43
-
44
- .ga_container .filter-choices a.selected {
45
- background: #c6dafc;
46
- color: #353535;
47
- }
48
-
49
- #exTab1, #exTab2 {
50
- margin-right: 15px !important;
51
- }
52
-
53
- #exTab1 .tab-content {
54
- color: white;
55
- background-color: #428bca;
56
- padding: 5px 15px;
57
- }
58
-
59
- #exTab2 h3 {
60
- color: white;
61
- background-color: #428bca;
62
- padding: 5px 15px;
63
- }
64
-
65
- /* remove border radius for the tab */
66
- #exTab1 .nav-pills > li > a {
67
- border-radius: 0;
68
- }
69
-
70
- /* change border radius for the tab , apply corners on top*/
71
- #exTab3 .nav-pills > li > a {
72
- border-radius: 4px 4px 0 0;
73
- }
74
-
75
- #exTab3 .tab-content {
76
- color: white;
77
- background-color: #428bca;
78
- padding: 5px 15px;
79
- }
80
-
81
- label.ga_checkbox_label {
82
- margin-top: 6px !important;
83
- }
84
-
85
- .form-table th {
86
- width: 250px !important;
87
- }
88
-
89
- .wrap.ga-notice {
90
- width: 100% !important;
91
- margin-left: 0px !important;
92
- margin-right: 10px !important;
93
- }
94
-
95
- .ga_warning {
96
- font-size: 12px;
97
- font-weight: normal;
98
- margin-top: 10px;
99
- }
100
-
101
- .ga-boxes-container {
102
-
103
- }
104
-
105
- .ga-box-row {
106
- display: table;
107
- width: 100%;
108
- table-layout: fixed;
109
- border-spacing: 10px;
110
- }
111
-
112
- .ga-box-column {
113
- display: table-cell;
114
- -moz-box-shadow: 0 0 5px #e5e5e5;
115
- -webkit-box-shadow: 0 0 5px #e5e5e5;
116
- box-shadow: 0px 0px 5px #e5e5e5;
117
- }
118
-
119
- .ga-box-dashboard {
120
- border: 1px solid #cccccc;
121
- border-radius: 0px;
122
- padding: 3px;
123
- text-align: center;
124
- }
125
-
126
- .ga-box-centered {
127
- text-align: center;
128
- }
129
-
130
- .ga-loader-wrapper {
131
- float: right;
132
- margin-top: 4px;
133
- margin-right: 5px;
134
- }
135
-
136
- .ga-loader-wrapper.stats-page {
137
- width: 45px;
138
- text-align: center;
139
- margin: 0 auto;
140
- float: none;
141
- }
142
-
143
- .ga-loader {
144
- border: 4px solid #f3f3f3; /* Light grey */
145
- border-top: 4px solid #3498db; /* Blue */
146
- border-radius: 50%;
147
- width: 15px;
148
- height: 15px;
149
- animation: spin 2s linear infinite;
150
- display: none;
151
- }
152
-
153
- .ga-loader.stats-page-loader {
154
- width: 45px;
155
- height: 45px;
156
- border-width: 6px;
157
- }
158
-
159
- @keyframes spin {
160
- 0% {
161
- transform: rotate(0deg);
162
- }
163
- 100% {
164
- transform: rotate(360deg);
165
- }
166
- }
167
-
168
- .ga-review-us {
169
- position: fixed;
170
- right: 10px;
171
- bottom: 40px;
172
- background: #fff;
173
- border: solid 1px #3379b7;
174
- border-radius: 10px;
175
- padding: 15px;
176
- width: 295px;
177
- text-align: center;
178
- }
179
-
180
- .ga-review-us #close-review-us {
181
- cursor: pointer;
182
- }
183
-
184
- .ga-review-us h3 {
185
- color: #3379b7;
186
- }
187
-
188
- .ga-review-us h3 a {
189
- color: #ff9801;
190
- }
191
-
192
- .ga-chart {
193
- }
194
-
195
- .label-grey {
196
- color: #ccc;
197
- }
198
-
199
- .ga-tooltip {
200
- position: relative;
201
- cursor: not-allowed;
202
- }
203
-
204
- .ga-tooltip input[disabled] {
205
- cursor: not-allowed;
206
- }
207
-
208
- .ga-tooltiptext {
209
- background-color: #ffe692;
210
- border: 1px solid #ffb900;
211
- border-radius: 4px;
212
- color: #444;
213
- font-size: 12px;
214
- font-weight: 500;
215
- margin-left: 10px;
216
- margin-top: 30px;
217
- padding: 3px 10px;
218
- position: relative;
219
- white-space: nowrap;
220
- text-align: center;
221
- visibility: hidden;
222
- width: auto;
223
- z-index: 1;
224
- }
225
-
226
- .ga-tt-abs {
227
- display: inline-block;
228
- position: absolute;
229
- margin-top: 0px;
230
- }
231
-
232
- .ga-tooltip:hover .ga-tooltiptext {
233
- visibility: visible;
234
- }
235
-
236
- .weight-normal{
237
- font-weight: normal;
238
- }
239
-
240
- .ga-love-text {
241
- display: block;
242
- margin-top: 20px;
243
- font-size: 10px;
244
- font-weight: bold;
245
- }
246
-
247
- .ga-debug-form-div{
248
- margin:5px;
249
- }
250
-
251
- .ga-debug-form-label {
252
- vertical-align: top;
253
- display: inline-block;
254
- width: 20%;
255
- }
256
-
257
- .ga-debug-form-field {
258
- width: 50%;
259
- }
260
-
261
- .ga-debug-form-loader {
262
- width: 20%;
263
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @CHARSET "UTF-8";
2
+
3
+ .wrap.ga-wrap {
4
+ margin: 0px auto !important;
5
+ margin-top: 10px !important;
6
+ margin-right: 0px !important;
7
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
8
+ font-size: 14px;
9
+ line-height: 1.42857143;
10
+ color: #333333;
11
+ }
12
+
13
+ .ga_container {
14
+ margin-top: 25px;
15
+ }
16
+
17
+ .google-analytics_page_googleanalytics-settings .ga_container {
18
+ display: flex;
19
+ flex-wrap: wrap;
20
+ justify-content: space-between;
21
+ }
22
+
23
+ .ga_container .sidebar-ad {
24
+ width: 50%;
25
+ background: #fff;
26
+ padding: 16px;
27
+ box-shadow: 6px 7px 12px 6px #cdcdcd;
28
+ }
29
+
30
+ .ga_container .sidebar-ad img {
31
+ width: 100%;
32
+ }
33
+
34
+ .ga_container a.view-report {
35
+ background: #c6dafc;
36
+ width: 200px;
37
+ display: inline-block;
38
+ padding: 10px 20px;
39
+ border-radius: 5px;
40
+ text-decoration: none;
41
+ font-size: 15px;
42
+ font-weight: bold;
43
+ text-align: center;
44
+ margin: 10px 5px 10px 0;
45
+ }
46
+
47
+ .ga_container .filter-choices a {
48
+ background: #f9f9f9;
49
+ width: 100px;
50
+ display: inline-block;
51
+ padding: 10px 20px;
52
+ border-radius: 5px;
53
+ text-decoration: none;
54
+ font-size: 15px;
55
+ font-weight: bold;
56
+ text-align: center;
57
+ margin: 10px 5px 10px 0;
58
+ color: #888888;
59
+ }
60
+
61
+ .ga_container .filter-choices a.selected {
62
+ background: #c6dafc;
63
+ color: #353535;
64
+ }
65
+
66
+ #exTab1, #exTab2 {
67
+ margin-right: 15px !important;
68
+ }
69
+
70
+ #exTab1 .tab-content {
71
+ color: white;
72
+ background-color: #428bca;
73
+ padding: 5px 15px;
74
+ }
75
+
76
+ #exTab2 h3 {
77
+ color: white;
78
+ background-color: #428bca;
79
+ padding: 5px 15px;
80
+ }
81
+
82
+ /* remove border radius for the tab */
83
+ #exTab1 .nav-pills > li > a {
84
+ border-radius: 0;
85
+ }
86
+
87
+ /* change border radius for the tab , apply corners on top*/
88
+ #exTab3 .nav-pills > li > a {
89
+ border-radius: 4px 4px 0 0;
90
+ }
91
+
92
+ #exTab3 .tab-content {
93
+ color: white;
94
+ background-color: #428bca;
95
+ padding: 5px 15px;
96
+ }
97
+
98
+ label.ga_checkbox_label {
99
+ margin-top: 6px !important;
100
+ }
101
+
102
+ .form-table th {
103
+ width: 250px !important;
104
+ }
105
+
106
+ .wrap.ga-notice {
107
+ width: 100% !important;
108
+ margin-left: 0px !important;
109
+ margin-right: 10px !important;
110
+ }
111
+
112
+ .ga_warning {
113
+ font-size: 12px;
114
+ font-weight: normal;
115
+ margin-top: 10px;
116
+ }
117
+
118
+ .ga-boxes-container {
119
+
120
+ }
121
+
122
+ .ga-box-row {
123
+ display: table;
124
+ width: 100%;
125
+ table-layout: fixed;
126
+ border-spacing: 10px;
127
+ }
128
+
129
+ .ga-box-column {
130
+ display: table-cell;
131
+ -moz-box-shadow: 0 0 5px #e5e5e5;
132
+ -webkit-box-shadow: 0 0 5px #e5e5e5;
133
+ box-shadow: 0px 0px 5px #e5e5e5;
134
+ }
135
+
136
+ .ga-box-dashboard {
137
+ border: 1px solid #cccccc;
138
+ border-radius: 0px;
139
+ padding: 3px;
140
+ text-align: center;
141
+ }
142
+
143
+ .ga-box-centered {
144
+ text-align: center;
145
+ }
146
+
147
+ .ga-loader-wrapper {
148
+ float: right;
149
+ margin-top: 4px;
150
+ margin-right: 5px;
151
+ }
152
+
153
+ .ga-loader-wrapper.stats-page {
154
+ width: 45px;
155
+ text-align: center;
156
+ margin: 0 auto;
157
+ float: none;
158
+ }
159
+
160
+ .ga-loader {
161
+ border: 4px solid #f3f3f3; /* Light grey */
162
+ border-top: 4px solid #3498db; /* Blue */
163
+ border-radius: 50%;
164
+ width: 15px;
165
+ height: 15px;
166
+ animation: spin 2s linear infinite;
167
+ display: none;
168
+ }
169
+
170
+ .ga-loader.stats-page-loader {
171
+ width: 45px;
172
+ height: 45px;
173
+ border-width: 6px;
174
+ }
175
+
176
+ @keyframes spin {
177
+ 0% {
178
+ transform: rotate(0deg);
179
+ }
180
+ 100% {
181
+ transform: rotate(360deg);
182
+ }
183
+ }
184
+
185
+ .ga-review-us {
186
+ position: fixed;
187
+ right: 10px;
188
+ bottom: 40px;
189
+ background: #fff;
190
+ border: solid 1px #3379b7;
191
+ border-radius: 10px;
192
+ padding: 15px;
193
+ width: 295px;
194
+ text-align: center;
195
+ }
196
+
197
+ .ga-review-us #close-review-us {
198
+ cursor: pointer;
199
+ }
200
+
201
+ .ga-review-us h3 {
202
+ color: #3379b7;
203
+ }
204
+
205
+ .ga-review-us h3 a {
206
+ color: #ff9801;
207
+ }
208
+
209
+ .ga-chart {
210
+ }
211
+
212
+ .label-grey {
213
+ color: #ccc;
214
+ }
215
+
216
+ .ga-tooltip {
217
+ position: relative;
218
+ cursor: not-allowed;
219
+ }
220
+
221
+ .ga-tooltip input[disabled] {
222
+ cursor: not-allowed;
223
+ }
224
+
225
+ .ga-tooltiptext {
226
+ background-color: #ffe692;
227
+ border: 1px solid #ffb900;
228
+ border-radius: 4px;
229
+ color: #444;
230
+ font-size: 12px;
231
+ font-weight: 500;
232
+ margin-left: 10px;
233
+ margin-top: 30px;
234
+ padding: 3px 10px;
235
+ position: relative;
236
+ white-space: nowrap;
237
+ text-align: center;
238
+ visibility: hidden;
239
+ width: auto;
240
+ z-index: 1;
241
+ }
242
+
243
+ .ga-tt-abs {
244
+ display: inline-block;
245
+ position: absolute;
246
+ margin-top: 0px;
247
+ }
248
+
249
+ .ga-tooltip:hover .ga-tooltiptext {
250
+ visibility: visible;
251
+ }
252
+
253
+ .weight-normal{
254
+ font-weight: normal;
255
+ }
256
+
257
+ .ga-love-text {
258
+ display: block;
259
+ margin-top: 20px;
260
+ font-size: 10px;
261
+ font-weight: bold;
262
+ }
263
+
264
+ .ga-debug-form-div{
265
+ margin:5px;
266
+ }
267
+
268
+ .ga-debug-form-label {
269
+ vertical-align: top;
270
+ display: inline-block;
271
+ width: 20%;
272
+ }
273
+
274
+ .ga-debug-form-field {
275
+ width: 50%;
276
+ }
277
+
278
+ .ga-debug-form-loader {
279
+ width: 20%;
280
+ }
281
+
282
+ .demo-ad.ga-panel-default img {
283
+ opacity: .3;
284
+ background: #cdcdcd;
285
+ width: 100%;
286
+ }
287
+
288
+ .demo-enable-popup {
289
+ display: none;
290
+ position: fixed;
291
+ top: 50%;
292
+ left: 50%;
293
+ transform: translate(-50%,-50%);
294
+ background: #fff;
295
+ border: 2px solid #428aca;
296
+ padding: 15px;
297
+ width: 500px;
298
+ }
299
+
300
+ .demo-enable-popup.engage {
301
+ display: block;
302
+ }
303
+
304
+ .demo-enable-popup .close-demo-modal {
305
+ position: absolute;
306
+ right: 10px;
307
+ top: 10px;
308
+ border: 1px solid;
309
+ border-radius: 20px;
310
+ padding: 0 7px;
311
+ color: #fff;
312
+ background: #cdcdcd;
313
+ cursor: pointer;
314
+ }
315
+
316
+ .demo-enable-popup p {
317
+ font-size: 18px;
318
+ text-align: center;
319
+ padding: 0 20px 10px;
320
+ }
321
+
322
+ #sharethis-form-color {
323
+ display: flex;
324
+ flex-wrap: wrap;
325
+ margin: auto;
326
+ width: 50%;
327
+ }
328
+
329
+ #sharethis-form-color .color {
330
+ box-shadow: 1px 1px 4px 1px #cdcdcd;
331
+ cursor: pointer;
332
+ margin: 5px;
333
+ }
334
+
335
+ #sharethis-form-color .color.selected{
336
+ box-shadow: 0px 0px 3px 5px #4583ee;
337
+ }
338
+
339
+ #publisher-purpose .purpose-item {
340
+ margin-bottom: 25px;
341
+ }
342
+
343
+ #publisher-purpose .purpose-item div.title {
344
+ display: block;
345
+ text-align: left;
346
+ font-weight: bold;
347
+ width: 100%;
348
+ }
349
+
350
+ #publisher-purpose .purpose-item label {
351
+ display: inline-block;
352
+ width: 21%;
353
+ }
354
+
355
+ .gdpr-platform {
356
+ margin: 20px auto;
357
+ max-width: 900px;
358
+ background: #fff;
359
+ padding: 20px;
360
+ z-index: 999;
361
+ position: relative;
362
+ }
363
+
364
+ .gdpr-submit-button {
365
+ margin: 20px auto;
366
+ max-width: 900px;
367
+ z-index: 999;
368
+ position: relative;
369
+ }
370
+
371
+ .gdpr-platform #publisher-purpose .purpose-item label:first-of-type {
372
+ width: 14%;
373
+ }
374
+
375
+ .gdpr-platform .switch label input[type=radio]:checked+.lever,
376
+ .gdpr-platform .switch label input[type=checkbox]:checked+.lever {
377
+ background: #4583ee;
378
+ border: 2px solid #4583ee;
379
+ box-shadow: none;
380
+ }
381
+
382
+ .gdpr-platform input[type="text"] {
383
+ line-height: 40px;
384
+ margin-bottom: 10px;
385
+ width: 300px;
386
+ }
387
+
388
+ .gdpr-platform select {
389
+ margin-bottom: 10px;
390
+ line-height: 40px;
391
+ width: 300px;
392
+ }
393
+
394
+ .gdpr-platform .switch label input[type=radio]:checked+.lever:before,
395
+ .gdpr-platform .switch label input[type=checkbox]:checked+.lever:before {
396
+ content:"✓";
397
+ position: absolute;
398
+ top:0;
399
+ left:3px;
400
+ color: #fff;
401
+ }
402
+
403
+ .gdpr-platform .switch label {
404
+ font-size: 12px;
405
+ display: block;
406
+ margin-top: 10px;
407
+ }
408
+
409
+ .gdpr-platform .switch label .lever {
410
+ content: "";
411
+ display: inline-block;
412
+ position: relative;
413
+ width: 20px;
414
+ height: 18px;
415
+ background-color: #fff;
416
+ border: 1px solid #cdcdcd;
417
+ border-radius: 0;
418
+ margin-right: 10px;
419
+ transition: background .3s ease;
420
+ vertical-align: middle;
421
+ margin: 0 16px;
422
+ }
423
+
424
+ .gdpr-platform .switch label .lever:after {
425
+ border-radius: 0;
426
+ content: unset;
427
+ position: unset;
428
+ }
429
+
430
+
431
+ .gdpr-platform [type=radio].filled-in+label:before, .gdpr-platform [type=checkbox].filled-in+label:before {
432
+ content: "";
433
+ left: 0;
434
+ position: absolute;
435
+ transition: border .25s,background-color .25s,width .2s .1s,height .2s .1s,top .2s .1s,left .2s .1s;
436
+ z-index: 1;
437
+ }
438
+
439
+ p.form-color {
440
+ float: left;
441
+ width: 100%;
442
+ padding-top: 15px;
443
+ }
444
+
445
+ #sharethis-form-color {
446
+ display: flex;
447
+ flex-wrap: wrap;
448
+ margin: auto;
449
+ width: 50%;
450
+ }
451
+
452
+ #sharethis-form-color .color {
453
+ box-shadow: 1px 1px 4px 1px #cdcdcd;
454
+ cursor: pointer;
455
+ margin: 5px;
456
+ }
457
+
458
+ #sharethis-form-color .color.selected{
459
+ box-shadow: 0px 0px 3px 5px #4583ee;
460
+ }
461
+
462
+ .empty-choices {
463
+ display: flex;
464
+ justify-content: center;
465
+ opacity: 1;
466
+ transition: opacity .6s;
467
+ }
468
+
469
+ a.st-rc-link {
470
+ margin: 20px 0 0 10px;
471
+ }
472
+
473
+ .gdpr-platform a.st-rc-link {
474
+ background: #4583ee;
475
+ border-radius: 3px;
476
+ border: none;
477
+ color: #fff;
478
+ cursor: pointer;
479
+ margin: .8em auto .5em;
480
+ display: inline-block;
481
+ padding: 15px 60px;
482
+ letter-spacing: 0.1em;
483
+ text-align: center;
484
+ text-decoration: none;
485
+ transition: opacity 0.2s ease-out
486
+ }
487
+
488
+ .empty-choices a {
489
+ padding: 10px;
490
+ min-width: 250px;
491
+ }
492
+
493
+ .empty-choices a.st-rc-link {
494
+ margin: 20px 0 20px 10px;
495
+ }
496
+
497
+ .gdpr-platform .vendor-table-body {
498
+ border: solid 1px #cdcdcd;
499
+ padding: 10px;
500
+ max-height: 600px;
501
+ overflow: scroll;
502
+ }
503
+
504
+ .gdpr-platform .vendor-table-cell {
505
+ padding: 20px 0;
506
+ border-bottom: solid 1px #cdcdcd;
507
+ }
508
+
509
+ .gdpr-platform .purpose-item input[type="checkbox"],
510
+ .gdpr-platform .vendor-table-cell input[type="checkbox"],
511
+ .gdpr-platform .purpose-item input[type="radio"]
512
+ {
513
+ display: none;
514
+ }
515
+
516
+ .gdpr-platform .exclusion-item {
517
+ padding: 20px 0;
518
+ border-bottom: solid 1px #cdcdcd;
519
+ }
520
+
521
+ .gdpr-platform label.enable-tool {
522
+ font-weight: bold;
523
+ font-size: 16px;
524
+ margin-bottom: 20px;
525
+ }
526
+
527
+ .gdpr-platform h2 {
528
+ text-align: center;
529
+ }
530
+
531
+ .gdpr-platform .error-message {
532
+ display: block;
533
+ text-align: left;
534
+ }
535
+
536
+ .gdpr-platform .gdpr-landing img {
537
+ margin: auto;
538
+ width: 600px;
539
+ display: block;
540
+ }
541
+
542
+ .register-section {
543
+ text-align: center;
544
+ padding: 30px 20px;
545
+ }
546
+
547
+ .register-section button {
548
+ background-color: #4583ee;
549
+ color: #fff;
550
+ padding: 10px 15px;
551
+ border: none;
552
+ border-radius: 5px;
553
+ }
554
+
555
+ .accor-wrap {
556
+ margin: 1rem 0 0;
557
+ display: inline-block;
558
+ width: 100%;
559
+ }
560
+
561
+ .accor-wrap .accor-tab {
562
+ background: #4582ec;
563
+ padding: .5rem;
564
+ border-radius: 5px;
565
+ color: #ffffff;
566
+ }
567
+
568
+ .accor-wrap .well {
569
+ display: inline-block;
570
+ width: 100%;
571
+ }
572
+
573
+ .accor-wrap .well h3 {
574
+ margin: 1rem 0;
575
+ padding-top: 20px;
576
+ text-align: center;
577
+ width: 100%;
578
+ }
579
+
580
+ .accor-wrap .well .col-md-12:first-of-type h3 {
581
+ margin-top: 0;
582
+ }
583
+
584
+ .accor-wrap .accor-content {
585
+ display: none;
586
+ }
587
+
588
+ .accor-wrap .accor-arrow {
589
+ border: 1px solid #ffffff;
590
+ padding: .2rem .4rem;
591
+ border-radius: 5px;
592
+ margin: 0 .3rem 0 .3rem;
593
+ font-size: 12px;
594
+ cursor: pointer;
595
+ font-family: arial, sans-serif;
596
+ }
597
+
598
+ .ga-demo-chart .ga-chart {
599
+ display: inline-block;
600
+ width: 46%;
601
+ }
602
+
603
+ #ga_form .form-table {
604
+ max-width: 560px;
605
+ }
606
+
607
+ .adBanner {
608
+ background-color: transparent;
609
+ height: 1px;
610
+ width: 1px;
611
+ }
612
+
613
+ #adblocker-notice {
614
+ display: none;
615
+ }
googleanalytics.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: Google Analytics
5
  * Plugin URI: http://wordpress.org/extend/plugins/googleanalytics/
6
  * Description: Use Google Analytics on your WordPress site without touching any code, and view visitor reports right in your WordPress admin dashboard!
7
- * Version: 2.3.8
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
@@ -47,7 +47,7 @@ if ( !preg_match( '/(\/|\\\)' . GA_NAME . '(\/|\\\)/', realpath( __FILE__ ), $te
47
  die();
48
  }
49
 
50
- define( 'GOOGLEANALYTICS_VERSION', '2.3.8' );
51
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
52
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
53
  include_once GA_PLUGIN_DIR . '/tools/class-support-logging.php';
4
  * Plugin Name: Google Analytics
5
  * Plugin URI: http://wordpress.org/extend/plugins/googleanalytics/
6
  * Description: Use Google Analytics on your WordPress site without touching any code, and view visitor reports right in your WordPress admin dashboard!
7
+ * Version: 2.4.0
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
47
  die();
48
  }
49
 
50
+ define( 'GOOGLEANALYTICS_VERSION', '2.4.0' );
51
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
52
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
53
  include_once GA_PLUGIN_DIR . '/tools/class-support-logging.php';
js/googleanalytics.js CHANGED
@@ -1,28 +1,28 @@
1
- /**
2
- * Created by mdn on 2016-12-02.
3
- */
4
-
5
- (function ($) {
6
- ga_loader = {
7
- show: function () {
8
- $('.ga-loader').show();
9
- },
10
- hide: function () {
11
- $('.ga-loader').hide();
12
- }
13
- };
14
-
15
- ga_tools = {
16
- getCurrentWidth: function (wrapperSelector) {
17
- return $(wrapperSelector).width();
18
- },
19
- recomputeChartWidth: function (minWidth, offset, wrapperSelector) {
20
- const currentWidth = ga_tools.getCurrentWidth(wrapperSelector);
21
- if (currentWidth >= minWidth) {
22
- return parseInt(currentWidth - offset);
23
- } else {
24
- return minWidth;
25
- }
26
- }
27
- };
28
- })(jQuery);
1
+ /**
2
+ * Created by mdn on 2016-12-02.
3
+ */
4
+
5
+ (function ($) {
6
+ ga_loader = {
7
+ show: function () {
8
+ $('.ga-loader').show();
9
+ },
10
+ hide: function () {
11
+ $('.ga-loader').hide();
12
+ }
13
+ };
14
+
15
+ ga_tools = {
16
+ getCurrentWidth: function (wrapperSelector) {
17
+ return $(wrapperSelector).width();
18
+ },
19
+ recomputeChartWidth: function (minWidth, offset, wrapperSelector) {
20
+ const currentWidth = ga_tools.getCurrentWidth(wrapperSelector);
21
+ if (currentWidth >= minWidth) {
22
+ return parseInt(currentWidth - offset);
23
+ } else {
24
+ return minWidth;
25
+ }
26
+ }
27
+ };
28
+ })(jQuery);
js/googleanalytics_page.js CHANGED
@@ -66,6 +66,86 @@ const GA_AUTHENTICATION_CODE_ERROR = 'That looks like your Google Analytics Trac
66
 
67
  ga_events = {
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  click: function (selector, callback) {
70
  $(selector).live('click', callback);
71
  },
@@ -130,7 +210,264 @@ const GA_AUTHENTICATION_CODE_ERROR = 'That looks like your Google Analytics Trac
130
  $('#' + GA_DEBUG_MODAL_CONTENT_ID ).click(function(event){
131
  event.stopPropagation();
132
  });
133
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  };
135
 
136
  /**
@@ -170,12 +507,16 @@ const GA_AUTHENTICATION_CODE_ERROR = 'That looks like your Google Analytics Trac
170
 
171
  $(document).ready(function () {
172
  ga_events.initModalEvents();
 
 
173
  });
174
 
175
  const offset = 50;
176
  const minWidth = 350;
177
  const wrapperSelector = '#ga-stats-container';
178
  const chartContainer = 'chart_div';
 
 
179
 
180
  ga_charts = {
181
 
@@ -240,7 +581,44 @@ const GA_AUTHENTICATION_CODE_ERROR = 'That looks like your Google Analytics Trac
240
  var chart = new google.visualization.AreaChart(document
241
  .getElementById(chartContainer));
242
  chart.draw(data, options);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  };
245
  ga_debug = {
246
  url: '',
66
 
67
  ga_events = {
68
 
69
+ /**
70
+ * Send Demographic data.
71
+ *
72
+ * @param data
73
+ */
74
+ sendDemoData: function(demoData) {
75
+
76
+ // Send demographic data.
77
+ $.ajax( {
78
+ url: 'https://platform-api.sharethis.com/v1.0/property?id=' + ga_property_id + '&secret=' + ga_secret_id,
79
+ method: 'PUT',
80
+ async: false,
81
+ contentType: 'application/json; charset=utf-8',
82
+ data: JSON.stringify( {
83
+ "demographics": demoData
84
+ } ),
85
+ success: function( results ) {
86
+ }
87
+ } );
88
+ },
89
+ /**
90
+ * Returns gdpr onboarding config values.
91
+ */
92
+ setGDPRConfig: function(isGDPR) {
93
+
94
+ /**
95
+ * Check if ad blocker exists and notify if so.
96
+ */
97
+ $(document).ready(function(){
98
+ if($("#detectadblock").height() > 0) {
99
+ } else {
100
+ $('#adblocker-notice').show();
101
+ }
102
+ });
103
+
104
+ if (!isGDPR || undefined === gaGdprConfig) {
105
+ return;
106
+ }
107
+
108
+ var config = JSON.parse(gaGdprConfig);
109
+
110
+ $('.gdpr-platform input[name="gdpr-enable"]').prop('checked', config['enabled'] === 'true');
111
+ $('#sharethis-publisher-name').val(config['publisher_name']);
112
+ $(`#sharethis-user-type option[value="${config['display']}"]` ).attr('selected',true);
113
+ $(`#sharethis-consent-type option[value="${config['scope']}"]`).attr('selected', true);
114
+ $(`#sharethis-form-color .color[data-value="${config['color']}"]`).addClass('selected');
115
+ $(`#st-language option[value="${config['language']}"]`).attr('selected', true);
116
+
117
+ if (undefined !== config['publisher_purposes']) {
118
+ $( "#publisher-purpose .purpose-item input" ).prop('checked', false);
119
+
120
+ config['publisher_purposes'].map( ( purpVal ) => {
121
+ var legit = 'true' === purpVal['legitimate_interest'] || true === purpVal['legitimate_interest'];
122
+ var consent = 'false' === purpVal['legitimate_interest'] || false === purpVal['legitimate_interest'];
123
+
124
+ $( `#publisher-purpose .purpose-item input[name="purposes[${purpVal.id}]"][value="legitimate"]` ).prop( 'checked', legit );
125
+ $( `#publisher-purpose .purpose-item input[name="purposes[${purpVal.id}]"][value="consent"]` ).prop( 'checked', consent );
126
+ } );
127
+ }
128
+
129
+ if (undefined !== config['publisher_restrictions']) {
130
+ $( ".vendor-table-cell-wrapper input" ).prop('checked', false);
131
+
132
+ $.map(config['publisher_restrictions'], function (id, venVal ) {
133
+ if(id) {
134
+ $( `input[type="checkbox"][data-id="${venVal}"]` ).prop( 'checked', true );
135
+ }
136
+ } );
137
+ }
138
+ },
139
+ scrollToAnchor: function(aid) {
140
+ var aTag = $("a[name='"+ aid.toLowerCase() +"']");
141
+
142
+ $('.vendor-table-body').animate({
143
+ scrollTop: 0
144
+ }, 0).animate({
145
+ scrollTop: aTag.offset().top - 740
146
+ }, 0);
147
+ },
148
+
149
  click: function (selector, callback) {
150
  $(selector).live('click', callback);
151
  },
210
  $('#' + GA_DEBUG_MODAL_CONTENT_ID ).click(function(event){
211
  event.stopPropagation();
212
  });
213
+ },
214
+
215
+ getConfig: function () {
216
+ var config,
217
+ enabled = $('input[name="gdpr-enable"]').is(':checked'),
218
+ publisherPurposes = [],
219
+ display = $( '#sharethis-user-type option:selected' ).val(),
220
+ name = $( '#sharethis-publisher-name' ).val(),
221
+ scope = $( '#sharethis-consent-type option:selected' ).val(),
222
+ color = $( '#sharethis-form-color .color.selected' ).attr('data-value'),
223
+ publisherRestrictions = {},
224
+ language = $( '#st-language' ).val();
225
+
226
+ $('#publisher-purpose input:checked').each( function( index, value ) {
227
+ var theId = $(value).attr('data-id'),
228
+ legit = 'consent' !== $(value).val();
229
+
230
+ publisherPurposes.push({ 'id': theId, 'legitimate_interest' : legit });
231
+ });
232
+
233
+ $('.vendor-table-cell-wrapper label input:checked').each( function( index, value ) {
234
+ var vendorId = $(value).attr('data-id');
235
+ if (vendorId) {
236
+ publisherRestrictions[vendorId] = true;
237
+ }
238
+ });
239
+
240
+ config = {
241
+ enabled: enabled,
242
+ display: display,
243
+ publisher_name: name,
244
+ publisher_purposes: publisherPurposes,
245
+ publisher_restrictions: publisherRestrictions,
246
+ language: language,
247
+ color: color,
248
+ scope: scope,
249
+ };
250
+
251
+ return config;
252
+ },
253
+
254
+ enableGdpr: function () {
255
+ var timer = '';
256
+ this.$gdprContainer = $('.gdpr-platform');
257
+
258
+ // New color select.
259
+ this.$gdprContainer.on('click', "#sharethis-form-color .color", function() {
260
+ $('#sharethis-form-color .color').removeClass('selected');
261
+ $(this).addClass('selected');
262
+ });
263
+
264
+ // clear or show choices.
265
+ this.$gdprContainer.on('click', '#clear-choices', function(e) {
266
+ e.preventDefault();
267
+ e.stopPropagation();
268
+
269
+ $( '.purpose-item input' ).prop( 'checked', false );
270
+ });
271
+
272
+ // clear or show choices.
273
+ this.$gdprContainer.on('click', '#see-st-choices', function(e) {
274
+ e.preventDefault();
275
+ e.stopPropagation();
276
+ $('.purpose-item input[name="purposes[1]"]').prop('checked', true);
277
+ $('.purpose-item input[name="purposes[3]"][value="consent"]').prop('checked', true);
278
+ $('.purpose-item input[name="purposes[5]"][value="consent"]').prop('checked', true);
279
+ $('.purpose-item input[name="purposes[6]"][value="consent"]').prop('checked', true);
280
+ $('.purpose-item input[name="purposes[9]"][value="legitimate"]').prop('checked', true);
281
+ $('.purpose-item input[name="purposes[10]"][value="legitimate"]').prop('checked', true);
282
+ });
283
+
284
+ // Uncheck radio if click on selected box.
285
+ this.$gdprContainer.on( 'click', '.lever', (e) => {
286
+ e.preventDefault();
287
+ e.stopPropagation();
288
+
289
+ const theInput = $( e.currentTarget ).siblings( 'input' );
290
+
291
+ if ( theInput.is( ':checked' ) ) {
292
+ $( `input[name="${theInput.attr( 'name' )}"]` ).prop( 'checked', false )
293
+ } else {
294
+ theInput.prop( 'checked', true )
295
+ }
296
+ } );
297
+
298
+ // Toggle button menus when arrows are clicked.
299
+ $( 'body' ).on( 'click', '.accor-wrap .accor-tab', function() {
300
+ var type = $( this ).find( 'span.accor-arrow' );
301
+
302
+ var closestButton = $( type ).parent( '.accor-tab' ).parent( '.accor-wrap' );
303
+
304
+ if ( '►' === type.html() ) {
305
+
306
+ // Show the button configs.
307
+ closestButton.find( '.accor-content' ).slideDown();
308
+
309
+ // Change the icon next to title.
310
+ closestButton.find( '.accor-arrow' ).html( '&#9660;' );
311
+ } else {
312
+
313
+ // Show the button configs.
314
+ closestButton.find( '.accor-content' ).slideUp();
315
+
316
+ // Change the icon next to title.
317
+ closestButton.find( '.accor-arrow' ).html( '&#9658;' );
318
+ }
319
+ } );
320
+
321
+ $('body').on('click', '.demo-enable-popup .close-demo-modal', function(e) {
322
+ e.preventDefault();
323
+ e.stopPropagation();
324
+ $('.demo-enable-popup').removeClass('engage');
325
+ });
326
+
327
+ $('body').on('click', '#demographic-popup', function(e) {
328
+ e.preventDefault();
329
+ e.stopPropagation();
330
+ $('.demo-enable-popup').addClass('engage');
331
+ });
332
+
333
+ $('body').on('click', '#enable-demographic, #Enable-demographic', function(e) {
334
+ e.preventDefault();
335
+ e.stopPropagation();
336
+ ga_events.enableDemographic('enable');
337
+ });
338
+
339
+
340
+ $('body').on('click', '#Disable-demographic', function(e) {
341
+ e.preventDefault();
342
+ e.stopPropagation();
343
+ ga_events.enableDemographic('disable');
344
+ });
345
+
346
+ // Enable GDPR tool.
347
+ $('body').on('click', '.gdpr-submit', function(e) {
348
+ e.preventDefault();
349
+ e.stopPropagation();
350
+
351
+ var dataObj = {},
352
+ self = this,
353
+ config = ga_events.getConfig();
354
+
355
+ theData = JSON.stringify( {
356
+ 'secret': ga_secret_id,
357
+ 'id': ga_property_id,
358
+ 'product': 'gdpr-compliance-tool-v2',
359
+ 'config': config
360
+ } );
361
+
362
+ // Send new button status value.
363
+ $.ajax( {
364
+ url: 'https://platform-api.sharethis.com/v1.0/property/product',
365
+ method: 'POST',
366
+ async: false,
367
+ contentType: 'application/json; charset=utf-8',
368
+ data: theData,
369
+ success: function( results ) {
370
+ }
371
+ } );
372
+
373
+ dataObj['action'] = "ga_ajax_enable_gdpr";
374
+ dataObj['nonce'] = 'true';
375
+ dataObj['config'] = config;
376
+
377
+ $.ajax({
378
+ type: "post",
379
+ dataType: "json",
380
+ url: ajaxurl,
381
+ data: dataObj,
382
+ success: function (response) {
383
+ window.location.reload();
384
+ }
385
+ });
386
+ });
387
+
388
+
389
+ // Enable GDPR tool.
390
+ $('body').on('click', '.gdpr-enable', function(e) {
391
+ e.preventDefault();
392
+ e.stopPropagation();
393
+
394
+ var dataObj = {},
395
+ self = this,
396
+ config = ga_events.getConfig();
397
+
398
+ if ($('body').hasClass('google-analytics_page_googleanalytics-settings')) {
399
+ config = {
400
+ enabled: true,
401
+ display: 'eu',
402
+ publisher_name: '',
403
+ publisher_purposes: [],
404
+ language: 'en',
405
+ color: '',
406
+ scope: 'global',
407
+ };
408
+ }
409
+
410
+ theData = JSON.stringify( {
411
+ 'secret': ga_secret_id,
412
+ 'id': ga_property_id,
413
+ 'product': 'gdpr-compliance-tool-v2',
414
+ 'config': config
415
+ } );
416
+
417
+ // Send new button status value.
418
+ $.ajax( {
419
+ url: 'https://platform-api.sharethis.com/v1.0/property/product',
420
+ method: 'POST',
421
+ async: false,
422
+ contentType: 'application/json; charset=utf-8',
423
+ data: theData,
424
+ success: function( results ) {
425
+ }
426
+ } );
427
+
428
+ dataObj['action'] = "ga_ajax_enable_gdpr";
429
+ dataObj['nonce'] = 'true';
430
+ dataObj['config'] = config;
431
+
432
+ $.ajax({
433
+ type: "post",
434
+ dataType: "json",
435
+ url: ajaxurl,
436
+ data: dataObj,
437
+ success: function (response) {
438
+ window.location.href = siteAdminUrl + 'admin.php?page=googleanalytics%2Fgdpr';
439
+ }
440
+ });
441
+ });
442
+
443
+ // Scroll to anchor in vendor list.
444
+ // Send user input to category search AFTER they stop typing.
445
+ $('body').on( 'keyup', '.vendor-search input', function( e ) {
446
+ clearTimeout( timer );
447
+
448
+ timer = setTimeout( function() {
449
+ ga_events.scrollToAnchor($(this).val());
450
+ }.bind( this, ga_events ), 500 );
451
+ } );
452
+ },
453
+
454
+ enableDemographic: function(disable) {
455
+ var dataObj = {};
456
+
457
+ dataObj['action'] = "ga_ajax_enable_demographic";
458
+ dataObj['nonce'] = ga_demo_nonce;
459
+ dataObj['enabled'] = 'disable' === disable ? 'false' : 'true';
460
+
461
+ $.ajax({
462
+ type: "post",
463
+ dataType: "json",
464
+ url: ajaxurl,
465
+ data: dataObj,
466
+ success: function (response) {
467
+ window.location.reload();
468
+ }
469
+ });
470
+ }
471
  };
472
 
473
  /**
507
 
508
  $(document).ready(function () {
509
  ga_events.initModalEvents();
510
+ ga_events.enableGdpr();
511
+ ga_events.setGDPRConfig($('body').hasClass('google-analytics_page_googleanalytics-gdpr'));
512
  });
513
 
514
  const offset = 50;
515
  const minWidth = 350;
516
  const wrapperSelector = '#ga-stats-container';
517
  const chartContainer = 'chart_div';
518
+ const demoChartGenderContainer = 'demo_chart_gender_div';
519
+ const demoChartAgeContainer = 'demo_chart_age_div';
520
 
521
  ga_charts = {
522
 
581
  var chart = new google.visualization.AreaChart(document
582
  .getElementById(chartContainer));
583
  chart.draw(data, options);
584
+ },
585
+ drawDemoGenderChart: function (data, chartWidth) {
586
+
587
+ if (typeof chartWidth == 'undefined') {
588
+ chartWidth = ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector);
589
+ }
590
+
591
+ var data = google.visualization.arrayToDataTable(data);
592
+
593
+ var options = {
594
+ title: 'Gender'
595
+ };
596
+
597
+ var chart = new google.visualization.PieChart(document.getElementById(demoChartGenderContainer));
598
+
599
+ chart.draw(data, options);
600
+ },
601
+
602
+ drawDemoAgeChart: function (data, chartWidth) {
603
+
604
+ if (typeof chartWidth == 'undefined') {
605
+ chartWidth = ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector);
606
  }
607
+
608
+ var data = google.visualization.arrayToDataTable(data);
609
+
610
+ var options = {
611
+ title: 'Age',
612
+ chartArea: {width: '50%'},
613
+ hAxis: {
614
+ minValue: 0
615
+ },
616
+ };
617
+
618
+ var chart = new google.visualization.BarChart(document.getElementById(demoChartAgeContainer));
619
+
620
+ chart.draw(data, options);
621
+ }
622
  };
623
  ga_debug = {
624
  url: '',
lib/Ga_Lib_Api_Client.php CHANGED
@@ -1,75 +1,75 @@
1
- <?php
2
-
3
- abstract class Ga_Lib_Api_Client {
4
-
5
- /**
6
- * Keeps error messages.
7
- * @var array
8
- */
9
- protected $errors = array();
10
-
11
- /**
12
- * Returns errors array.
13
- * @return array
14
- */
15
- public function get_errors() {
16
- return $this->errors;
17
- }
18
-
19
- /**
20
- * Calls private API method from context client.
21
- *
22
- * @param $callback
23
- * @param $args
24
- *
25
- * @return Ga_Lib_Api_Response
26
- */
27
- abstract function call_api_method( $callback, $args );
28
-
29
- /**
30
- * Calls api methods.
31
- *
32
- * @param string $callback
33
- * @param mixed $args
34
- *
35
- * @return mixed
36
- */
37
- public function call( $callback, $args = null ) {
38
- try {
39
- update_option( 'googleanalytics_sherethis_error_log', false );
40
- return $this->call_api_method( $callback, $args );
41
- } catch ( Ga_Lib_Api_Client_Exception $e ) {
42
- $this->add_error( $e );
43
-
44
- return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
45
- } catch ( Ga_Lib_Api_Request_Exception $e ) {
46
- $this->add_error( $e );
47
-
48
- return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
49
- } catch ( Exception $e ) {
50
- $this->add_error( $e );
51
-
52
- return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
53
- }
54
- }
55
-
56
- /**
57
- * Prepares error data.
58
- *
59
- * @param Exception $e
60
- *
61
- */
62
- protected function add_error( Exception $e ) {
63
- $this->errors[ $e->getCode() ] = array( 'class' => get_class( $e ), 'message' => $e->getMessage() );
64
- do_action( 'st_support_save_error', $e );
65
- }
66
-
67
- public function add_own_error( $code, $message, $class = '' ) {
68
- $this->errors[ $code ] = array( 'class' => $class, 'message' => $message );
69
- }
70
-
71
- }
72
-
73
- class Ga_Lib_Api_Client_Exception extends Exception {
74
-
75
- }
1
+ <?php
2
+
3
+ abstract class Ga_Lib_Api_Client {
4
+
5
+ /**
6
+ * Keeps error messages.
7
+ * @var array
8
+ */
9
+ protected $errors = array();
10
+
11
+ /**
12
+ * Returns errors array.
13
+ * @return array
14
+ */
15
+ public function get_errors() {
16
+ return $this->errors;
17
+ }
18
+
19
+ /**
20
+ * Calls private API method from context client.
21
+ *
22
+ * @param $callback
23
+ * @param $args
24
+ *
25
+ * @return Ga_Lib_Api_Response
26
+ */
27
+ abstract function call_api_method( $callback, $args );
28
+
29
+ /**
30
+ * Calls api methods.
31
+ *
32
+ * @param string $callback
33
+ * @param mixed $args
34
+ *
35
+ * @return mixed
36
+ */
37
+ public function call( $callback, $args = null ) {
38
+ try {
39
+ delete_option('googleanalytics_sherethis_error_log');
40
+ return $this->call_api_method( $callback, $args );
41
+ } catch ( Ga_Lib_Api_Client_Exception $e ) {
42
+ $this->add_error( $e );
43
+
44
+ return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
45
+ } catch ( Ga_Lib_Api_Request_Exception $e ) {
46
+ $this->add_error( $e );
47
+
48
+ return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
49
+ } catch ( Exception $e ) {
50
+ $this->add_error( $e );
51
+
52
+ return new Ga_Lib_Api_Response( Ga_Lib_Api_Response::$empty_response );
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Prepares error data.
58
+ *
59
+ * @param Exception $e
60
+ *
61
+ */
62
+ protected function add_error( Exception $e ) {
63
+ $this->errors[ $e->getCode() ] = array( 'class' => get_class( $e ), 'message' => $e->getMessage() );
64
+ do_action( 'st_support_save_error', $e );
65
+ }
66
+
67
+ public function add_own_error( $code, $message, $class = '' ) {
68
+ $this->errors[ $code ] = array( 'class' => $class, 'message' => $message );
69
+ }
70
+
71
+ }
72
+
73
+ class Ga_Lib_Api_Client_Exception extends Exception {
74
+
75
+ }
lib/Ga_Lib_Api_Request.php CHANGED
@@ -1,223 +1,215 @@
1
- <?php
2
-
3
- class Ga_Lib_Api_Request {
4
-
5
- static $instance = null;
6
-
7
- const HEADER_CONTENT_TYPE = "application/x-www-form-urlencoded";
8
- const HEADER_CONTENT_TYPE_JSON = "Content-type: application/json";
9
- const HEADER_ACCEPT = "Accept: application/json, text/javascript, */*; q=0.01";
10
- const TIMEOUT = 5;
11
- const USER_AGENT = 'googleanalytics-wordpress-plugin';
12
-
13
- private $headers = array();
14
-
15
- // Whether to cache or not
16
- private $cache = false;
17
-
18
- private $appendix = '';
19
-
20
- private function __construct( $cache = false, $appendix = '' ) {
21
- $this->cache = $cache;
22
- $this->appendix = $appendix;
23
- }
24
-
25
- /**
26
- * Returns API client instance.
27
- *
28
- * @return Ga_Lib_Api_Request|null
29
- */
30
- public static function get_instance( $cache = false, $appendix = '' ) {
31
- if ( self::$instance === null ) {
32
- self::$instance = new Ga_Lib_Api_Request( $cache, $appendix );
33
- }
34
-
35
- return self::$instance;
36
- }
37
-
38
- /**
39
- * Sets request headers.
40
- *
41
- * @param $headers
42
- */
43
- public function set_request_headers( $headers ) {
44
- if ( is_array( $headers ) ) {
45
- $this->headers = array_merge( $this->headers, $headers );
46
- } else {
47
- $this->headers[] = $headers;
48
- }
49
- }
50
-
51
- /**
52
- * Perform HTTP request.
53
- *
54
- * @param string $url URL address
55
- * @param string $rawPostBody
56
- * @param boolean $json Whether to append JSON content type
57
- * @param boolean $force_no_cache Whether to force not to cache response data even if cache property is set to true
58
- *
59
- * @return string Response
60
- * @throws Exception
61
- */
62
- public function make_request( $url, $rawPostBody = null, $json = false, $force_no_cache = false ) {
63
-
64
- // Return cached data if exist
65
- if ( ! $force_no_cache ) {
66
- if ( $this->cache ) {
67
- $wp_transient_name = Ga_Cache::get_transient_name( $url, $rawPostBody, $this->appendix );
68
-
69
- if ( $cached = Ga_Cache::get_cached_result( $wp_transient_name ) ) {
70
- if ( ! Ga_Cache::is_data_cache_outdated( $wp_transient_name, $this->appendix ) ) {
71
- return $cached;
72
- }
73
- }
74
-
75
- // Check if the next request after error is allowed
76
- if ( false === Ga_Cache::is_next_request_allowed( $wp_transient_name ) ) {
77
- throw new Ga_Lib_Api_Client_Exception( _( 'There are temporary connection issues, please try again later.' ) );
78
- }
79
- }
80
- }
81
-
82
-
83
- if ( !function_exists( 'curl_init' ) ) {
84
- throw new Ga_Lib_Api_Client_Exception( _( 'cURL functions are not available' ) );
85
- }
86
-
87
- // Set default headers
88
- $this->set_request_headers( array(
89
- ( $json ? self::HEADER_CONTENT_TYPE_JSON : self::HEADER_CONTENT_TYPE ),
90
- self::HEADER_ACCEPT
91
- ) );
92
-
93
- $ch = curl_init( $url );
94
- $headers = $this->headers;
95
-
96
- $curl_timeout = self::TIMEOUT;
97
- $php_execution_time = ini_get( 'max_execution_time' );
98
- if ( !empty( $php_execution_time ) && is_numeric( $php_execution_time ) ) {
99
- if ( $php_execution_time < 36 && $php_execution_time > 9 ) {
100
- $curl_timeout = $php_execution_time - 5;
101
- } elseif ( $php_execution_time < 10 ) {
102
- $curl_timeout = 5;
103
- }
104
- }
105
-
106
- // Set the proxy configuration. The user can provide this in wp-config.php
107
- if ( defined( 'WP_PROXY_HOST' ) ) {
108
- curl_setopt( $ch, CURLOPT_PROXY, WP_PROXY_HOST );
109
- }
110
- if ( defined( 'WP_PROXY_PORT' ) ) {
111
- curl_setopt( $ch, CURLOPT_PROXYPORT, WP_PROXY_PORT );
112
- }
113
- if ( defined( 'WP_PROXY_USERNAME' ) ) {
114
- $auth = WP_PROXY_USERNAME;
115
- if ( defined( 'WP_PROXY_PASSWORD' ) ) {
116
- $auth .= ':' . WP_PROXY_PASSWORD;
117
- }
118
- curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $auth );
119
- }
120
-
121
- curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout );
122
- curl_setopt( $ch, CURLOPT_TIMEOUT, $curl_timeout );
123
- curl_setopt( $ch, CURLOPT_HEADER, true );
124
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
125
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
126
-
127
- if ( !function_exists( 'ini_get' ) || ! ini_get( 'curl.cainfo' ) ) {
128
- curl_setopt( $ch, CURLOPT_CAINFO, $this->get_cert_path() );
129
- }
130
-
131
- curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
132
- curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
133
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
134
- curl_setopt( $ch, CURLOPT_USERAGENT, self::USER_AGENT );
135
- if ( defined( 'CURLOPT_IPRESOLVE' ) && defined( 'CURL_IPRESOLVE_V4' ) ) {
136
- curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
137
- }
138
-
139
- // POST body
140
- if ( !empty( $rawPostBody ) ) {
141
- curl_setopt( $ch, CURLOPT_POST, true );
142
- curl_setopt( $ch, CURLOPT_POSTFIELDS, ( $json ? $rawPostBody : http_build_query( $rawPostBody , null, '&' ) ) );
143
- }
144
-
145
- // Execute request
146
- $response = curl_exec( $ch );
147
-
148
- if ( $error = curl_error( $ch ) ) {
149
- $errno = curl_errno( $ch );
150
- curl_close( $ch );
151
-
152
- // Store last cache time when unsuccessful
153
- if ( false === $force_no_cache ) {
154
- if ( true === $this->cache ) {
155
- Ga_Cache::set_last_cache_time( $wp_transient_name );
156
- Ga_Cache::set_last_time_attempt();
157
- }
158
- }
159
-
160
- throw new Ga_Lib_Api_Client_Exception( $error . ' (' . $errno . ')' );
161
- } else {
162
- $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
163
- $headerSize = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
164
- $header = substr( $response, 0, $headerSize );
165
- $body = substr( $response, $headerSize, strlen( $response ) );
166
- if ( preg_match( '/^(4|5)[0-9]{2}/', $httpCode ) ) {
167
-
168
- // Store last cache time when unsuccessful
169
- if ( false === $force_no_cache ) {
170
- if ( true === $this->cache ) {
171
- Ga_Cache::set_last_cache_time( $wp_transient_name );
172
- Ga_Cache::set_last_time_attempt();
173
- }
174
- }
175
-
176
- throw new Ga_Lib_Api_Request_Exception( ( $httpCode == 404 ? _( 'Requested URL doesn\'t exists: ' . $url ) : $body ) );
177
- }
178
-
179
- curl_close( $ch );
180
-
181
- $response_data = array( $header, $body );
182
-
183
- if (isset($_REQUEST['key'])) {
184
- $profile = array(
185
- 'key' => sanitize_text_field( $_REQUEST['key'] ),
186
- 'token' => sanitize_text_field( $_REQUEST['token'] ),
187
- 'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
188
- 'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
189
- 'a' => sanitize_text_field( $_REQUEST['a'] ), // AccountID
190
- 'w' => sanitize_text_field( $_REQUEST['w'] ), // PropertyID
191
- 'p' => sanitize_text_field( $_REQUEST['p'] ), // View ID
192
- 'siteurl' => site_url(),
193
- 'neturl' => network_admin_url(),
194
- );
195
- }
196
-
197
-
198
-
199
- // Cache result
200
- if ( false === $force_no_cache ) {
201
- if ( true === $this->cache ) {
202
- Ga_Cache::set_cache( $wp_transient_name, $response_data );
203
- }
204
- }
205
-
206
-
207
- return $response_data;
208
- }
209
- }
210
-
211
- private function get_cert_path() {
212
- return GA_PLUGIN_DIR . '/lib/cert/cacerts.pem';
213
- }
214
-
215
- }
216
-
217
- class Ga_Lib_Api_Request_Exception extends Exception {
218
-
219
- public function __construct( $message ) {
220
- parent::__construct( $message );
221
- }
222
-
223
- }
1
+ <?php
2
+
3
+ class Ga_Lib_Api_Request {
4
+
5
+ static $instance = null;
6
+
7
+ const HEADER_CONTENT_TYPE = "application/x-www-form-urlencoded";
8
+ const HEADER_CONTENT_TYPE_JSON = "Content-type: application/json";
9
+ const HEADER_ACCEPT = "Accept: application/json, text/javascript, */*; q=0.01";
10
+ const TIMEOUT = 5;
11
+ const USER_AGENT = 'googleanalytics-wordpress-plugin';
12
+
13
+ private $headers = array();
14
+
15
+ // Whether to cache or not
16
+ private $cache = false;
17
+
18
+ private $appendix = '';
19
+
20
+ private function __construct( $cache = false, $appendix = '' ) {
21
+ $this->cache = $cache;
22
+ $this->appendix = $appendix;
23
+ }
24
+
25
+ /**
26
+ * Returns API client instance.
27
+ *
28
+ * @return Ga_Lib_Api_Request|null
29
+ */
30
+ public static function get_instance( $cache = false, $appendix = '' ) {
31
+ if ( self::$instance === null ) {
32
+ self::$instance = new Ga_Lib_Api_Request( $cache, $appendix );
33
+ }
34
+
35
+ return self::$instance;
36
+ }
37
+
38
+ /**
39
+ * Sets request headers.
40
+ *
41
+ * @param $headers
42
+ */
43
+ public function set_request_headers( $headers ) {
44
+ if ( is_array( $headers ) ) {
45
+ $this->headers = array_merge( $this->headers, $headers );
46
+ } else {
47
+ $this->headers[] = $headers;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Perform HTTP request.
53
+ *
54
+ * @param string $url URL address
55
+ * @param string $rawPostBody
56
+ * @param boolean $json Whether to append JSON content type
57
+ * @param boolean $force_no_cache Whether to force not to cache response data even if cache property is set to true
58
+ *
59
+ * @return string Response
60
+ * @throws Exception
61
+ */
62
+ public function make_request( $url, $rawPostBody = null, $json = false, $force_no_cache = false ) {
63
+
64
+ // Return cached data if exist
65
+ if ( ! $force_no_cache ) {
66
+ if ( $this->cache ) {
67
+ $wp_transient_name = Ga_Cache::get_transient_name( $url, $rawPostBody, $this->appendix );
68
+
69
+ if ( $cached = Ga_Cache::get_cached_result( $wp_transient_name ) ) {
70
+ if ( ! Ga_Cache::is_data_cache_outdated( $wp_transient_name, $this->appendix ) ) {
71
+ return $cached;
72
+ }
73
+ }
74
+
75
+ // Check if the next request after error is allowed
76
+ if ( false === Ga_Cache::is_next_request_allowed( $wp_transient_name ) ) {
77
+ throw new Ga_Lib_Api_Client_Exception( _( 'There are temporary connection issues, please try again later.' ) );
78
+ }
79
+ }
80
+ }
81
+
82
+
83
+ if ( !function_exists( 'curl_init' ) ) {
84
+ throw new Ga_Lib_Api_Client_Exception( _( 'cURL functions are not available' ) );
85
+ }
86
+
87
+ // Set default headers
88
+ $this->set_request_headers( array(
89
+ ( $json ? self::HEADER_CONTENT_TYPE_JSON : self::HEADER_CONTENT_TYPE ),
90
+ self::HEADER_ACCEPT
91
+ ) );
92
+
93
+ $ch = curl_init( $url );
94
+ $headers = $this->headers;
95
+
96
+ $curl_timeout = self::TIMEOUT;
97
+ $php_execution_time = ini_get( 'max_execution_time' );
98
+ if ( !empty( $php_execution_time ) && is_numeric( $php_execution_time ) ) {
99
+ if ( $php_execution_time < 36 && $php_execution_time > 9 ) {
100
+ $curl_timeout = $php_execution_time - 5;
101
+ } elseif ( $php_execution_time < 10 ) {
102
+ $curl_timeout = 5;
103
+ }
104
+ }
105
+
106
+ // Set the proxy configuration. The user can provide this in wp-config.php
107
+ if ( defined( 'WP_PROXY_HOST' ) ) {
108
+ curl_setopt( $ch, CURLOPT_PROXY, WP_PROXY_HOST );
109
+ }
110
+ if ( defined( 'WP_PROXY_PORT' ) ) {
111
+ curl_setopt( $ch, CURLOPT_PROXYPORT, WP_PROXY_PORT );
112
+ }
113
+ if ( defined( 'WP_PROXY_USERNAME' ) ) {
114
+ $auth = WP_PROXY_USERNAME;
115
+ if ( defined( 'WP_PROXY_PASSWORD' ) ) {
116
+ $auth .= ':' . WP_PROXY_PASSWORD;
117
+ }
118
+ curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $auth );
119
+ }
120
+
121
+ curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout );
122
+ curl_setopt( $ch, CURLOPT_TIMEOUT, $curl_timeout );
123
+ curl_setopt( $ch, CURLOPT_HEADER, true );
124
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
125
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
126
+
127
+ if ( !function_exists( 'ini_get' ) || ! ini_get( 'curl.cainfo' ) ) {
128
+ curl_setopt( $ch, CURLOPT_CAINFO, $this->get_cert_path() );
129
+ }
130
+
131
+ curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
132
+ curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
133
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
134
+ curl_setopt( $ch, CURLOPT_USERAGENT, self::USER_AGENT );
135
+ if ( defined( 'CURLOPT_IPRESOLVE' ) && defined( 'CURL_IPRESOLVE_V4' ) ) {
136
+ curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
137
+ }
138
+
139
+ // POST body
140
+ if ( !empty( $rawPostBody ) ) {
141
+ curl_setopt( $ch, CURLOPT_POST, true );
142
+ curl_setopt( $ch, CURLOPT_POSTFIELDS, ( $json ? $rawPostBody : http_build_query( $rawPostBody , null, '&' ) ) );
143
+ }
144
+
145
+ // Execute request
146
+ $response = curl_exec( $ch );
147
+
148
+ if ( $error = curl_error( $ch ) ) {
149
+ $errno = curl_errno( $ch );
150
+ curl_close( $ch );
151
+
152
+ // Store last cache time when unsuccessful
153
+ if ( false === $force_no_cache ) {
154
+ if ( true === $this->cache ) {
155
+ Ga_Cache::set_last_cache_time( $wp_transient_name );
156
+ Ga_Cache::set_last_time_attempt();
157
+ }
158
+ }
159
+
160
+ throw new Ga_Lib_Api_Client_Exception( $error . ' (' . $errno . ')' );
161
+ } else {
162
+ $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
163
+ $headerSize = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
164
+ $header = substr( $response, 0, $headerSize );
165
+ $body = substr( $response, $headerSize, strlen( $response ) );
166
+ if ( preg_match( '/^(4|5)[0-9]{2}/', $httpCode ) ) {
167
+
168
+ throw new Ga_Lib_Api_Request_Exception( ( $httpCode == 404 ? _( 'Requested URL doesn\'t exists: ' . $url ) : $body ) );
169
+ }
170
+
171
+ curl_close( $ch );
172
+
173
+ $response_data = array( $header, $body );
174
+
175
+ if (isset($_REQUEST['key'])) {
176
+ $profile = array(
177
+ 'key' => sanitize_text_field( $_REQUEST['key'] ),
178
+ 'token' => sanitize_text_field( $_REQUEST['token'] ),
179
+ 'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
180
+ 'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
181
+ 'a' => sanitize_text_field( $_REQUEST['a'] ), // AccountID
182
+ 'w' => sanitize_text_field( $_REQUEST['w'] ), // PropertyID
183
+ 'p' => sanitize_text_field( $_REQUEST['p'] ), // View ID
184
+ 'siteurl' => site_url(),
185
+ 'neturl' => network_admin_url(),
186
+ );
187
+ }
188
+
189
+
190
+
191
+ // Cache result
192
+ if ( false === $force_no_cache ) {
193
+ if ( true === $this->cache ) {
194
+ Ga_Cache::set_cache( $wp_transient_name, $response_data );
195
+ }
196
+ }
197
+
198
+
199
+ return $response_data;
200
+ }
201
+ }
202
+
203
+ private function get_cert_path() {
204
+ return GA_PLUGIN_DIR . '/lib/cert/cacerts.pem';
205
+ }
206
+
207
+ }
208
+
209
+ class Ga_Lib_Api_Request_Exception extends Exception {
210
+
211
+ public function __construct( $message ) {
212
+ parent::__construct( $message );
213
+ }
214
+
215
+ }
 
 
 
 
 
 
 
 
lib/Ga_Lib_Sharethis_Api_Client.php CHANGED
@@ -54,7 +54,7 @@ class Ga_Lib_Sharethis_Api_Client extends Ga_Lib_Api_Client {
54
  private function ga_api_create_sharethis_property( $query_params ) {
55
  $request = Ga_Lib_Api_Request::get_instance(self::USE_CACHE);
56
  try {
57
- $response = $request->make_request( $this->add_protocol( self::GA_SHARETHIS_ENDPOINT ), wp_json_encode( $query_params ), true ); var_dump($response); exit;
58
  } catch ( Ga_Lib_Api_Request_Exception $e ) {
59
  throw new Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception( $e->getMessage() );
60
  }
54
  private function ga_api_create_sharethis_property( $query_params ) {
55
  $request = Ga_Lib_Api_Request::get_instance(self::USE_CACHE);
56
  try {
57
+ $response = $request->make_request( $this->add_protocol( self::GA_SHARETHIS_ENDPOINT ), wp_json_encode( $query_params ), true );
58
  } catch ( Ga_Lib_Api_Request_Exception $e ) {
59
  throw new Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception( $e->getMessage() );
60
  }
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: sharethis, scottstorebloom
3
  Tags: analytics, google analytics, google analytics plugin, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
- Tested up to: 5.4.2
6
- Stable tag: 2.3.8
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
@@ -26,6 +26,8 @@ Start making data-driven decisions with real time stats including:
26
  * Visitor trends – Dive deeper into your website’s page views, users, pages per session, and bounce rate for the past 7 days as compared to the previous 7 days
27
  * Traffic sources – Discover which 5 traffic sources are driving the most visitors to your website
28
  * Top pages – Stay updated on your 10 most viewed pages
 
 
29
 
30
  With our mobile-optimized plugin, you’ll be able to stay informed and get actionable insights on any device. For more accurate stats, you also have the option to disable tracking for any role like Admins or Editors so your analytics represent real visitors.
31
 
@@ -87,6 +89,11 @@ We are always happy to help.
87
 
88
  == Changelog ==
89
 
 
 
 
 
 
90
  = 2.3.8 =
91
  * Fix compatibility with WPML.
92
 
2
  Contributors: sharethis, scottstorebloom
3
  Tags: analytics, google analytics, google analytics plugin, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
+ Tested up to: 5.5.1
6
+ Stable tag: 2.4.0
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
26
  * Visitor trends – Dive deeper into your website’s page views, users, pages per session, and bounce rate for the past 7 days as compared to the previous 7 days
27
  * Traffic sources – Discover which 5 traffic sources are driving the most visitors to your website
28
  * Top pages – Stay updated on your 10 most viewed pages
29
+ * New! Demographics - Get age and gender data in your dashboard.
30
+ * New! GDPR Compliance Tool - For all your EU Compliance, we've integrated the ShareThis GDPR Compliance Tool into our plugin!
31
 
32
  With our mobile-optimized plugin, you’ll be able to stay informed and get actionable insights on any device. For more accurate stats, you also have the option to disable tracking for any role like Admins or Editors so your analytics represent real visitors.
33
 
89
 
90
  == Changelog ==
91
 
92
+ = 2.4.0 =
93
+ * Add GDPR compliance tool integration.
94
+ * Add Demographic data chart option.
95
+ * Fix ST terms agreement.
96
+
97
  = 2.3.8 =
98
  * Fix compatibility with WPML.
99
 
view/ga_notice.php CHANGED
@@ -1,18 +1,11 @@
1
- <div id="googleanalytics_terms_notice"
2
- class="notice notice-warning <?php echo ( Ga_Helper::is_plugin_page() ) ? '' : 'is-dismissible' ?>">
3
- <p>
4
- Google Analytics <?php echo esc_html( GOOGLEANALYTICS_VERSION ); ?> plugin <a
5
- href="http://www.sharethis.com/news/2016/12/sharethis-adds-analytics-plugin-to-suite-of-tools/"
6
- target="_blank">has joined the ShareThis family.</a> <strong>A host of new features</strong> have been added in this version, including Google Analytics dashboards and Alerts. The update requires agreeing to the <a href="http://www.sharethis.com/privacy/" target="_blank">privacy policy</a> and <a
7
- href="http://www.sharethis.com/publisher-terms-of-use/" target="_blank">terms of use</a> to enable them.
8
- <a href="<?php echo esc_url( $url ); ?>"><span class="button button-primary">I accept</span></a>
9
- </p>
10
- </div>
11
- <script type="text/javascript">
12
- jQuery(document).ready(function () {
13
- jQuery('#googleanalytics_terms_notice .notice-dismiss').live('click', function (event) {
14
- event.preventDefault();
15
- jQuery.post(ajaxurl, {action: 'googleanalytics_hide_terms'});
16
- });
17
- });
18
- </script>
1
+ <div id="googleanalytics_terms_notice"
2
+ class="notice notice-warning <?php echo ( Ga_Helper::is_plugin_page() ) ? '' : 'is-dismissible' ?>">
3
+ <p>
4
+ Google Analytics <?php echo esc_html( GOOGLEANALYTICS_VERSION ); ?> plugin <a
5
+ href="http://www.sharethis.com/news/2016/12/sharethis-adds-analytics-plugin-to-suite-of-tools/"
6
+ target="_blank">has joined the ShareThis family.</a> <strong>A host of new features</strong> have been added in this version, including Google Analytics dashboards and demographics.
7
+ The update requires agreeing to the <a href="http://www.sharethis.com/privacy/" target="_blank">privacy policy</a> and <a
8
+ href="http://www.sharethis.com/publisher-terms-of-use/" target="_blank">terms of use</a> to enable them.
9
+ <a href="<?php echo esc_url( $url ); ?>"><span class="button button-primary">I accept</span></a>
10
+ </p>
11
+ </div>
 
 
 
 
 
 
 
view/page.php CHANGED
@@ -1,215 +1,318 @@
1
- <?php
2
- $optimize_code = get_option( 'googleanalytics_optimize_code' );
3
- $universal = get_option( 'googleanalytics_enable_universal_analytics', true );
4
- $anonymization = get_option( 'googleanalytics_ip_anonymization', true );
5
- ?>
6
- <div id="ga_access_code_modal" class="ga-modal" tabindex="-1">
7
- <div class="ga-modal-dialog">
8
- <div class="ga-modal-content">
9
- <div class="ga-modal-header">
10
- <span id="ga_close" class="ga-close">&times;</span>
11
- <h4 class="ga-modal-title"><?php _e( 'Please paste the access code obtained from Google below:' ) ?></h4>
12
- </div>
13
- <div class="ga-modal-body">
14
- <div id="ga_code_error" class="ga-alert ga-alert-danger" style="display: none;"></div>
15
- <label for="ga_access_code"><strong><?php _e( 'Access Code' ); ?></strong>:</label>
16
- &nbsp;<input id="ga_access_code_tmp" type="text"
17
- placeholder="<?php _e( 'Paste your access code here' ) ?>"/>
18
- <div class="ga-loader-wrapper">
19
- <div class="ga-loader"></div>
20
- </div>
21
- </div>
22
- <div class="ga-modal-footer">
23
- <button id="ga_btn_close" type="button" class="button">Close</button>
24
- <button type="button" class="button-primary"
25
- id="ga_save_access_code"
26
- onclick="ga_popup.saveAccessCode( event )"><?php _e( 'Save Changes' ); ?></button>
27
- </div>
28
- </div><!-- /.modal-content -->
29
- </div><!-- /.modal-dialog -->
30
- </div><!-- /.modal -->
31
- <?php echo $data[ 'debug_modal' ] ?>
32
- <div class="wrap ga-wrap">
33
- <h2>Google Analytics - <?php _e( 'Settings' ); ?></h2>
34
- <div class="ga_container">
35
- <?php if ( ! empty( $data['error_message'] ) ) : ?>
36
- <?php echo $data['error_message']; ?>
37
- <?php endif; ?>
38
- <form id="ga_form" method="post" action="options.php">
39
- <?php settings_fields( 'googleanalytics' ); ?>
40
- <input id="ga_access_code" type="hidden"
41
- name="<?php echo esc_attr( Ga_Admin::GA_OAUTH_AUTH_CODE_OPTION_NAME ); ?>" value=""/>
42
- <table class="form-table">
43
- <tr valign="top">
44
- <?php if ( ! empty( $data['popup_url'] ) ): ?>
45
- <th scope="row">
46
- <label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php echo _e( 'Google Profile' ) ?>
47
- :
48
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
49
- </label>
50
- </th>
51
- <td <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="ga-tooltip"' : ''; ?>>
52
- <?php echo $data[ 'auth_button' ] ?>
53
- <span class="ga-tooltiptext"><?php _e( $tooltip ); ?></span>
54
- <?php if ( ! empty( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] ) ): ?>
55
- <div class="ga_warning">
56
- <strong><?php _e( 'Notice' ) ?></strong>:&nbsp;<?php _e( 'Please uncheck the "Manually enter Tracking ID" option to authenticate and view statistics.' ); ?>
57
- </div>
58
- <?php endif; ?>
59
- </td>
60
- <?php endif; ?>
61
-
62
- <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
63
- <th scope="row"><?php echo _e( 'Google Analytics Account' ) ?>:</th>
64
- <td>
65
- <?php echo $data['ga_accounts_selector']; ?>
66
- </td>
67
- <?php endif; ?>
68
-
69
- </tr>
70
-
71
- <tr valign="top">
72
-
73
- <th scope="row">
74
- <div class="checkbox">
75
- <label class="ga_checkbox_label <?php echo Ga_Helper::get_code_manually_label_classes() ?>"
76
- for="ga_enter_code_manually"> <input
77
- <?php if ( Ga_Helper::are_features_enabled() ) : ?>
78
- onclick="ga_events.click( this, ga_events.codeManuallyCallback( <?php echo Ga_Helper::are_features_enabled() ? 1 : 0; ?> ) )"
79
- <?php endif; ?>
80
- type="checkbox"
81
- <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'disabled="disabled"' : ''; ?>
82
- name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ); ?>"
83
- id="ga_enter_code_manually"
84
- value="1"
85
- <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_terms_accepted() ) ? 'checked="checked"' : '' ); ?>/>&nbsp;
86
- <?php _e( 'Manually enter Tracking ID' ) ?>
87
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
88
- </label>
89
- <?php if ( ! Ga_Helper::are_features_enabled() ) : ?>
90
- <input id="ga_enter_code_manually_hidden" type="hidden"
91
- name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ); ?>"
92
- value="1"/>
93
- <?php endif; ?>
94
- </div>
95
- </th>
96
- <td></td>
97
- </tr>
98
- <tr valign="top"
99
- id="ga_manually_wrapper" <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_features_enabled() ) ? '' : 'style="display: none"' ); ?> >
100
-
101
- <th scope="row"><?php _e( 'Tracking ID' ) ?>:</th>
102
- <td>
103
- <input type="text"
104
- name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ); ?>"
105
- value="<?php echo esc_attr( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] ); ?>"
106
- id="ga_manually_input"/>&nbsp;
107
- <div class="ga_warning">
108
- <strong><?php _e( 'Warning' ); ?></strong>:&nbsp;<?php _e( 'If you enter your Tracking ID manually, Analytics statistics will not be shown.' ); ?>
109
- <br>
110
- <?php _e( 'We strongly recommend to authenticate with Google using the button above.' ); ?>
111
- </div>
112
- </td>
113
-
114
- </tr>
115
-
116
- <tr valign="top" id="ga_roles_wrapper">
117
- <th scope="row">
118
- <label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php _e( 'Exclude Tracking for Roles' ) ?>
119
- :
120
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
121
- </label>
122
- </th>
123
- <td>
124
-
125
-
126
- <?php
127
- if ( ! empty( $data['roles'] ) ) {
128
- $roles = $data['roles'];
129
- foreach ( $roles as $role ) {
130
- ?>
131
- <div class="checkbox">
132
- <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : ''; ?>"
133
- for="checkbox_<?php echo $role['id']; ?>">
134
- <input id="checkbox_<?php echo $role['id']; ?>" type="checkbox"
135
- <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'disabled="disabled"' : ''; ?>
136
- name="<?php echo esc_attr( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME . "[" . $role['id'] . "]" ); ?>"
137
- id="<?php echo esc_attr( $role['id'] ); ?>"
138
- <?php echo esc_attr( ( $role['checked'] ? 'checked="checked"' : '' ) ); ?> />&nbsp;
139
- <?php echo esc_html( $role['name'] ); ?>
140
- <span class="ga-tooltiptext"><?php _e( $tooltip ); ?></span>
141
- </label>
142
- </div>
143
- <?php
144
- }
145
- }
146
- ?>
147
-
148
- </td>
149
- </tr>
150
- <tr valign="top">
151
- <th scope="row"><?php _e( 'Enable IP Anonymization' ) ?>:</th>
152
- <td>
153
- <label class="ga-switch">
154
- <input id="ga-anonymization" name="googleanalytics_ip_anonymization"
155
- type="checkbox" <?php echo checked( $anonymization, 'on' ); ?>>
156
- <div id="ga-slider" class="ga-slider round"></div>
157
- </label
158
- </td>
159
- </tr>
160
- <tr valign="top">
161
- <th scope="row"><?php _e( 'If using Google Optimize, enter optimize code here' ) ?>:</th>
162
- <td>
163
- <label class="ga-text">
164
- <input id="ga-optimize" name="googleanalytics_optimize_code"
165
- type="text" placeholder="GTM-XXXXXX" value="<?php echo esc_attr( $optimize_code ); ?>">
166
- </label
167
- </td>
168
- </tr>
169
- <tr valign="top">
170
- <th scope="row"><?php _e( 'Disable all features' ) ?>:</th>
171
- <td>
172
- <label class="ga-switch">
173
- <input id="ga-disable" name="<?php echo Ga_Admin::GA_DISABLE_ALL_FEATURES; ?>"
174
- type="checkbox">
175
- <div id="ga-slider" class="ga-slider-disable ga-slider round"></div>
176
- </label
177
- </td>
178
- </tr>
179
- </table>
180
-
181
- <p class="submit">
182
- <input type="submit" class="button-primary"
183
- value="<?php _e( 'Save Changes' ) ?>"/>
184
- </p>
185
- </form>
186
- </div>
187
- <?php if ( $data['debug_info'] ) : ?>
188
- <tr valign="top">
189
- <td colspan="2">
190
- <p>If you are still experiencing an issue, we are here to help! We recommend clickingthe "Send Debugging Info" button below and pasting the information within an email to support@sharethis.com.</p>
191
- <p>
192
- <button id="ga_debug_button" class="button button-secondary" onclick="ga_debug.open_modal( event )" >Send Debugging Info</button>
193
- <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
194
- <?php echo $data[ 'auth_button' ] ?>
195
- <br>
196
- <small class="notice">
197
- *If you reset your google password you MUST re-authenticate to continue viewing your analytics dashboard.
198
- </small>
199
- <?php endif; ?>
200
- </p>
201
- </td>
202
- </tr>
203
- <?php endif; ?>
204
-
205
- <p class="ga-love-text"><?php _e( 'Love this plugin?' ); ?> <a
206
- href="https://wordpress.org/support/plugin/googleanalytics/reviews/#new-post"><?php _e( ' Please help spread the word by leaving a 5-star review!' ); ?> </a>
207
- </p>
208
- </div>
209
- <script type="text/javascript">
210
- const GA_DISABLE_FEATURE_URL = '<?php echo Ga_Helper::create_url(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL), array(Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_disable_all_features')); ?>';
211
- const GA_ENABLE_FEATURE_URL = '<?php echo Ga_Helper::create_url(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL), array(Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features')); ?>';
212
- jQuery(document).ready(function () {
213
- ga_switcher.init('<?php echo $data[ Ga_Admin::GA_DISABLE_ALL_FEATURES ]; ?>');
214
- });
215
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $optimize_code = get_option( 'googleanalytics_optimize_code' );
3
+ $universal = get_option( 'googleanalytics_enable_universal_analytics', true );
4
+ $anonymization = get_option( 'googleanalytics_ip_anonymization', true );
5
+ $gdpr_config = get_option( 'googleanalytics_gdpr_config');
6
+ ?>
7
+ <div id="adblocker-notice" class="notice notice-error is-dismissible">
8
+ <p>
9
+ <?php echo esc_html__( 'It appears you have an ad blocker enabled. To avoid affecting this plugin\'s functionality, please disable while using its admin configurations and registrations. Thank you.', 'sharethis-share-buttons' ); ?>
10
+ </p>
11
+ </div>
12
+ <div id="detectadblock">
13
+ <div class="adBanner">
14
+ </div>
15
+ </div>
16
+ <div id="ga_access_code_modal" class="ga-modal" tabindex="-1">
17
+ <div class="ga-modal-dialog">
18
+ <div class="ga-modal-content">
19
+ <div class="ga-modal-header">
20
+ <span id="ga_close" class="ga-close">&times;</span>
21
+ <h4 class="ga-modal-title"><?php _e( 'Please paste the access code obtained from Google below:' ) ?></h4>
22
+ </div>
23
+ <div class="ga-modal-body">
24
+ <div id="ga_code_error" class="ga-alert ga-alert-danger" style="display: none;"></div>
25
+ <label for="ga_access_code"><strong><?php _e( 'Access Code' ); ?></strong>:</label>
26
+ &nbsp;<input id="ga_access_code_tmp" type="text"
27
+ placeholder="<?php _e( 'Paste your access code here' ) ?>"/>
28
+ <div class="ga-loader-wrapper">
29
+ <div class="ga-loader"></div>
30
+ </div>
31
+ </div>
32
+ <div class="ga-modal-footer">
33
+ <button id="ga_btn_close" type="button" class="button">Close</button>
34
+ <button type="button" class="button-primary"
35
+ id="ga_save_access_code"
36
+ onclick="ga_popup.saveAccessCode( event )"><?php _e( 'Save Changes' ); ?></button>
37
+ </div>
38
+ </div><!-- /.modal-content -->
39
+ </div><!-- /.modal-dialog -->
40
+ </div><!-- /.modal -->
41
+ <?php echo $data[ 'debug_modal' ] ?>
42
+ <div class="wrap ga-wrap">
43
+ <h2>Google Analytics - <?php _e( 'Settings' ); ?></h2>
44
+ <div class="ga_container">
45
+ <?php if ( ! empty( $data['error_message'] ) ) : ?>
46
+ <?php echo $data['error_message']; ?>
47
+ <?php endif; ?>
48
+ <form id="ga_form" method="post" action="options.php">
49
+ <?php settings_fields( 'googleanalytics' ); ?>
50
+ <input id="ga_access_code" type="hidden"
51
+ name="<?php echo esc_attr( Ga_Admin::GA_OAUTH_AUTH_CODE_OPTION_NAME ); ?>" value=""/>
52
+ <table class="form-table">
53
+ <tr valign="top">
54
+ <?php if ( ! empty( $data['popup_url'] ) ): ?>
55
+ <th scope="row">
56
+ <label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php echo _e( 'Google Profile' ) ?>
57
+ :
58
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
59
+ </label>
60
+ </th>
61
+ <td <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="ga-tooltip"' : ''; ?>>
62
+ <?php echo $data[ 'auth_button' ] ?>
63
+ <span class="ga-tooltiptext"><?php _e( $tooltip ); ?></span>
64
+ <?php if ( ! empty( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] ) ): ?>
65
+ <div class="ga_warning">
66
+ <strong><?php _e( 'Notice' ) ?></strong>:&nbsp;<?php _e( 'Please uncheck the "Manually enter Tracking ID" option to authenticate and view statistics.' ); ?>
67
+ </div>
68
+ <?php endif; ?>
69
+ </td>
70
+ <?php endif; ?>
71
+
72
+ <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
73
+ <th scope="row"><?php echo _e( 'Google Analytics Account' ) ?>:</th>
74
+ <?php endif; ?>
75
+ </tr>
76
+ <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
77
+ <tr valign="top">
78
+ <td>
79
+ <?php echo $data['ga_accounts_selector']; ?>
80
+ </td>
81
+ </tr>
82
+ <?php endif; ?>
83
+ <tr valign="top">
84
+ <th scope="row">
85
+ <div class="checkbox">
86
+ <label class="ga_checkbox_label <?php echo Ga_Helper::get_code_manually_label_classes() ?>"
87
+ for="ga_enter_code_manually"> <input
88
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
89
+ onclick="ga_events.click( this, ga_events.codeManuallyCallback( <?php echo Ga_Helper::are_features_enabled() ? 1 : 0; ?> ) )"
90
+ <?php endif; ?>
91
+ type="checkbox"
92
+ <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'disabled="disabled"' : ''; ?>
93
+ name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ); ?>"
94
+ id="ga_enter_code_manually"
95
+ value="1"
96
+ <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_terms_accepted() ) ? 'checked="checked"' : '' ); ?>/>&nbsp;
97
+ <?php _e( 'Manually enter Tracking ID' ) ?>
98
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
99
+ </label>
100
+ <?php if ( ! Ga_Helper::are_features_enabled() ) : ?>
101
+ <input id="ga_enter_code_manually_hidden" type="hidden"
102
+ name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ); ?>"
103
+ value="1"/>
104
+ <?php endif; ?>
105
+ </div>
106
+ </th>
107
+ <td></td>
108
+ </tr>
109
+ <tr valign="top"
110
+ id="ga_manually_wrapper" <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_features_enabled() ) ? '' : 'style="display: none"' ); ?> >
111
+
112
+ <th scope="row"><?php _e( 'Tracking ID' ) ?>:</th>
113
+ </tr>
114
+ <tr valing="top">
115
+ <td>
116
+ <input type="text"
117
+ name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ); ?>"
118
+ value="<?php echo esc_attr( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] ); ?>"
119
+ id="ga_manually_input"/>&nbsp;
120
+ <div class="ga_warning">
121
+ <strong><?php _e( 'Warning' ); ?></strong>:&nbsp;<?php _e( 'If you enter your Tracking ID manually, Analytics statistics will not be shown.' ); ?>
122
+ <br>
123
+ <?php _e( 'We strongly recommend to authenticate with Google using the button above.' ); ?>
124
+ </div>
125
+ </td>
126
+ </tr>
127
+ <tr valign="top" id="ga_roles_wrapper">
128
+ <th scope="row">
129
+ <label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php _e( 'Exclude Tracking for Roles' ) ?>
130
+ :
131
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
132
+ </label>
133
+ </th>
134
+ </tr>
135
+ <tr valign="top">
136
+ <td>
137
+ <?php
138
+ if ( ! empty( $data['roles'] ) ) {
139
+ $roles = $data['roles'];
140
+ foreach ( $roles as $role ) {
141
+ ?>
142
+ <div class="checkbox">
143
+ <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : ''; ?>"
144
+ for="checkbox_<?php echo $role['id']; ?>">
145
+ <input id="checkbox_<?php echo $role['id']; ?>" type="checkbox"
146
+ <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'disabled="disabled"' : ''; ?>
147
+ name="<?php echo esc_attr( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME . "[" . $role['id'] . "]" ); ?>"
148
+ id="<?php echo esc_attr( $role['id'] ); ?>"
149
+ <?php echo esc_attr( ( $role['checked'] ? 'checked="checked"' : '' ) ); ?> />&nbsp;
150
+ <?php echo esc_html( $role['name'] ); ?>
151
+ <span class="ga-tooltiptext"><?php _e( $tooltip ); ?></span>
152
+ </label>
153
+ </div>
154
+ <?php
155
+ }
156
+ }
157
+ ?>
158
+ </td>
159
+ </tr>
160
+ <tr valign="top">
161
+ <th scope="row"><?php _e( 'Enable IP Anonymization' ) ?>:</th>
162
+ </tr>
163
+ <tr valign="top">
164
+ <td>
165
+ <label class="ga-switch <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
166
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
167
+ <input id="ga-anonymization" name="googleanalytics_ip_anonymization"
168
+ type="checkbox" <?php echo checked( $anonymization, 'on' ); ?>>
169
+
170
+ <div id="ga-slider" class="ga-slider round"></div>
171
+ <?php else: ?>
172
+ <input id="ga-anonymization" name="googleanalytics_ip_anonymization"
173
+ type="checkbox" disabled="disabled">
174
+
175
+ <div id="ga-slider" class="ga-slider round"></div>
176
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
177
+ <?php endif; ?>
178
+ </label>
179
+ </td>
180
+ </tr>
181
+ <tr valign="top">
182
+ <th scope="row"><?php _e( 'If using Google Optimize, enter optimize code here' ) ?>:</th>
183
+ </tr>
184
+ <tr valign="top">
185
+ <td>
186
+ <label class="ga-text <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
187
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
188
+ <input id="ga-optimize" name="googleanalytics_optimize_code"
189
+ type="text" placeholder="GTM-XXXXXX" value="<?php echo esc_attr( $optimize_code ); ?>">
190
+ <?php else: ?>
191
+ <input id="ga-optimize" name="googleanalytics_optimize_code"
192
+ type="text" placeholder="GTM-XXXXXX" value="<?php echo esc_attr( $optimize_code ); ?>" readonly>
193
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
194
+ <?php endif; ?>
195
+ </label>
196
+ </td>
197
+ </tr>
198
+ <tr valign="top">
199
+ <th scope="row"><?php _e( 'Disable all features' ) ?>:</th>
200
+ </tr>
201
+ <tr valign="top">
202
+ <td>
203
+ <label class="ga-switch <?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
204
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
205
+ <input id="ga-disable" name="<?php echo Ga_Admin::GA_DISABLE_ALL_FEATURES; ?>"
206
+ type="checkbox">
207
+ <div id="ga-slider" class="ga-slider-disable ga-slider round"></div>
208
+ <?php else: ?>
209
+ <input id="ga-disable" name="<?php echo Ga_Admin::GA_DISABLE_ALL_FEATURES; ?>"
210
+ type="checkbox" disabled="disabled">
211
+ <div id="ga-slider" class="ga-slider-disable ga-slider round"></div>
212
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
213
+ <?php endif; ?>
214
+ </label>
215
+ </td>
216
+ </tr>
217
+ <?php include plugin_dir_path(__FILE__) . 'templates/gdpr.php'; ?>
218
+ </table>
219
+
220
+ <p class="submit">
221
+ <input type="submit" class="button-primary"
222
+ value="<?php _e( 'Save Changes' ) ?>"/>
223
+ </p>
224
+ </form>
225
+ <?php if(empty($gdpr_config)) : ?>
226
+ <div class="sidebar-ad">
227
+ <h2 style="text-decoration: underline;">
228
+ <?php esc_html_e('Check out our new GDPR Compliance Tool!', 'googleanalytics'); ?>
229
+ </h2>
230
+ <div class="row">
231
+ <div class="col-md-12">
232
+ <img src="<?php echo trailingslashit(get_home_url()) . 'wp-content/plugins/googleanalytics/assets/images/gdpr-ex.png'; ?>" />
233
+ </div>
234
+ <div class="col-md-6">
235
+ <h3><?php esc_html_e('Confirm Consent', 'googleanalytics'); ?></h3>
236
+ <p>
237
+ <?php esc_html_e(
238
+ 'A simple and streamlined way to confirm a user’s initial acceptance or rejection of cookie collection',
239
+ 'googleanalytics'
240
+ ); ?>
241
+ </p>
242
+ </div>
243
+ <div class="col-md-6">
244
+ <h3><?php esc_html_e('Select Purpose', 'googleanalytics'); ?></h3>
245
+ <p>
246
+ <?php esc_html_e(
247
+ 'A transparent system of verifying the intent of collecting a user’s cookies, and giving the option to opt in or out',
248
+ 'googleanalytics'
249
+ ); ?>
250
+ </p>
251
+ </div>
252
+ </div>
253
+ <div class="row">
254
+ <div class="col-md-6">
255
+ <h3><?php esc_html_e('Indicate Company', 'googleanalytics'); ?></h3>
256
+ <p>
257
+ <?php esc_html_e(
258
+ 'A comprehensive record of company-level information that allows users to monitor and control the recipients of cookie collection',
259
+ 'googleanalytics'
260
+ ); ?>
261
+ </p>
262
+ </div>
263
+ <div class="col-md-6">
264
+ <h3><?php esc_html_e('Access Data Rights', 'googleanalytics'); ?></h3>
265
+ <p>
266
+ <?php esc_html_e(
267
+ 'A centralized database where users can review the latest privacy policies and information pertaining to their cookie collection',
268
+ 'googleanalytics'
269
+ ); ?>
270
+ </p>
271
+ </div>
272
+ </div>
273
+ <div class="row register-section">
274
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
275
+ <td>
276
+ <button class="gdpr-enable"><?php esc_html_e('Enable'); ?></button>
277
+ </td>
278
+ <?php else : ?>
279
+ <td>
280
+ <label class="<?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
281
+ <button class="gdpr-enable" disabled="disabled"><?php esc_html_e('Enable'); ?></button>
282
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
283
+ </label>
284
+ </td>
285
+ <?php endif; ?>
286
+ </div>
287
+ </div>
288
+ <?php endif; ?>
289
+ </div>
290
+ <?php if ( $data['debug_info'] ) : ?>
291
+ <tr valign="top">
292
+ <td colspan="2">
293
+ <p>If you are still experiencing an issue, we are here to help! We recommend clickingthe "Send Debugging Info" button below and pasting the information within an email to support@sharethis.com.</p>
294
+ <p>
295
+ <button id="ga_debug_button" class="button button-secondary" onclick="ga_debug.open_modal( event )" >Send Debugging Info</button>
296
+ <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
297
+ <?php echo $data[ 'auth_button' ] ?>
298
+ <br>
299
+ <small class="notice">
300
+ *If you reset your google password you MUST re-authenticate to continue viewing your analytics dashboard.
301
+ </small>
302
+ <?php endif; ?>
303
+ </p>
304
+ </td>
305
+ </tr>
306
+ <?php endif; ?>
307
+
308
+ <p class="ga-love-text"><?php _e( 'Love this plugin?' ); ?> <a
309
+ href="https://wordpress.org/support/plugin/googleanalytics/reviews/#new-post"><?php _e( ' Please help spread the word by leaving a 5-star review!' ); ?> </a>
310
+ </p>
311
+ </div>
312
+ <script type="text/javascript">
313
+ const GA_DISABLE_FEATURE_URL = '<?php echo Ga_Helper::create_url(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL), array(Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_disable_all_features')); ?>';
314
+ const GA_ENABLE_FEATURE_URL = '<?php echo Ga_Helper::create_url(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL), array(Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features')); ?>';
315
+ jQuery(document).ready(function () {
316
+ ga_switcher.init('<?php echo $data[ Ga_Admin::GA_DISABLE_ALL_FEATURES ]; ?>');
317
+ });
318
+ </script>
view/stats.php CHANGED
@@ -1,184 +1,242 @@
1
- <?php
2
- $account_data = json_decode(get_option('googleanalytics_account_data', true), true);
3
- $selected_data = json_decode(get_option('googleanalytics_selected_account', true), true);
4
-
5
- foreach ( $account_data as $properties ) {
6
- if ( $properties['id'] === $selected_data[0] ) {
7
- foreach ( $properties['webProperties'] as $web_property ) {
8
- if ( $web_property['webPropertyId'] === $selected_data[1] ) {
9
- $internal_prop = $web_property['internalWebPropertyId'];
10
- }
11
- }
12
- }
13
- }
14
-
15
- $sevenorthirty = isset($_GET['th']) ? '30' : '7';
16
- $selected7 = '7' === $sevenorthirty ? 'selected' : '';
17
- $selected30 = '30' === $sevenorthirty ? 'selected' : '';
18
- $selectedpage = isset($_GET['ts']) ? '' : 'selected';
19
- $selectedsource = isset($_GET['ts']) ? 'selected' : '';
20
- $report_url = 'https://analytics.google.com/analytics/web/#/report/content-pages/a' . $selected_data[0] . 'w' . $internal_prop . 'p' . $selected_data[2];
21
- $source_page_url = isset($_GET['ts']) ? str_replace('content-pages', 'trafficsources-all-traffic', $report_url ) : $report_url;
22
- $type_label = isset($_GET['ts']) ? 'Traffic Sources' : 'Pages/Posts';
23
- $thirty_url = isset($_GET['ts']) ? 'admin.php?page=googleanalytics&th&ts' : 'admin.php?page=googleanalytics&th';
24
- $seven_url = isset($_GET['ts']) ? 'admin.php?page=googleanalytics&ts' : 'admin.php?page=googleanalytics';
25
- $source_url = isset($_GET['th']) ? 'admin.php?page=googleanalytics&ts&th' : 'admin.php?page=googleanalytics&ts';
26
- $page_view_url = isset($_GET['th']) ? 'admin.php?page=googleanalytics&th' : 'admin.php?page=googleanalytics';
27
- ?>
28
- <div class="wrap ga-wrap" id="ga-stats-container">
29
- <?php if ( ! empty( $chart ) ) : ?>
30
- <div class="filter-choices">
31
- <a href="<?php echo get_admin_url('', $seven_url ); ?>" class="<?php echo esc_attr( $selected7 ); ?>">
32
- 7 days
33
- </a>
34
- <a href="<?php echo get_admin_url('', $thirty_url ); ?>" class="<?php echo esc_attr( $selected30 ); ?>">
35
- 30 days
36
- </a>
37
- </div>
38
- <div class="ga-panel ga-panel-default">
39
- <div class="ga-panel-heading">
40
- <strong>
41
- <?php echo 'Pageviews - Last ' . esc_html( $sevenorthirty ) . ' days'; ?>
42
- </strong>
43
- </div>
44
- <div class="ga-panel-body ga-chart">
45
- <div id="chart_div" style="width: 100%;"></div>
46
- <div class="ga-loader-wrapper stats-page">
47
- <div class="ga-loader stats-page-loader"></div>
48
- </div>
49
- </div>
50
- </div>
51
- <?php endif; ?>
52
-
53
- <?php if ( ! empty( $boxes ) ) : ?>
54
- <div class="ga-panel ga-panel-default">
55
- <div class="ga-panel-heading"><strong><?php echo 'Comparison - Last ' . esc_html( $sevenorthirty ) . ' days vs previous ' . esc_html( $sevenorthirty ) . ' days'; ?></strong>
56
- </div>
57
- <div class="ga-panel-body">
58
- <div class="ga-row">
59
- <?php foreach ( $boxes as $box ) : ?>
60
- <div class="ga-box">
61
- <div class="ga-panel ga-panel-default">
62
- <div class="ga-panel-body ga-box-centered">
63
- <div class="ga-box-label"><?php echo esc_html( $box['label'] ); ?></div>
64
- <div class="ga-box-diff" style="color: <?php echo esc_attr( $box['color'] ); ?>;">
65
- <?php echo Ga_Helper::format_percent( $box['diff'] ); ?>
66
- </div>
67
- <div class="ga-box-comparison"><?php echo $box['comparison']; ?></div>
68
- </div>
69
- </div>
70
- </div>
71
- <?php endforeach; ?>
72
- </div>
73
- </div>
74
- </div>
75
- <?php endif; ?>
76
-
77
- <?php if ( ! empty( $sources ) ) : ?>
78
- <div class="filter-choices">
79
- <a href="<?php echo get_admin_url('', $page_view_url); ?>" class="<?php echo esc_attr( $selectedpage ); ?>">
80
- Page View
81
- </a>
82
- <a href="<?php echo get_admin_url('', $source_url); ?>" class="<?php echo esc_attr( $selectedsource ); ?>">
83
- Traffic Source
84
- </a>
85
- </div>
86
- <div class="ga-panel ga-panel-default">
87
- <div class="ga-panel-heading"><strong><?php _e( "Top 10 " . $type_label . " by page views" ); ?></strong>
88
- </div>
89
- <div class="ga-panel-body">
90
-
91
- <div id="table-container">
92
- <table class="ga-table">
93
- <tr>
94
- <td colspan="2">
95
- </td>
96
- <th style="text-align: right;">
97
- <?php _e( 'Pageviews' ); ?>
98
- </th>
99
- <th style="text-align: right;">
100
- <?php echo '%'; ?>
101
- </th>
102
- </tr>
103
- <tr>
104
- <td colspan="2"></td>
105
- <td class="ga-col-pageviews" style="text-align: right">
106
- <div style="font-size: 16px;"><?php echo $sources['total'] ?></div>
107
- <div style="color: grey; font-size: 10px;">% of
108
- Total: <?php echo Ga_Helper::format_percent( ( ! empty( $sources['total'] ) ) ? number_format( $sources['sum'] / $sources['total'] * 100,
109
- 2, '.', ' ' ) : 100 );
110
- ?>
111
- (<?php echo $sources['sum'] ?>)
112
- </div>
113
- </td>
114
- <td class="ga-col-progressbar" style="text-align: right">
115
- <div style="font-size: 16px;"><?php echo $sources['total'] ?></div>
116
- <div style="color: grey; font-size: 10px;">% of
117
- Total: <?php echo Ga_Helper::format_percent( ( ! empty( $sources['total'] ) ) ? number_format( $sources['sum'] / $sources['total'] * 100,
118
- 2, '.', ' ' ) : 100 );
119
- ?>
120
- (<?php echo $sources['sum'] ?>)
121
- </div>
122
- </td>
123
- </tr>
124
- <?php foreach ( $sources['rows'] as $key => $source ): ?>
125
- <tr>
126
- <td style="width: 5%;text-align: right"><?php echo $key ?>.</td>
127
- <td class="ga-col-name">
128
- <?php if ( $source['name'] != '(direct) / (none)' ) :
129
-
130
- $single_breakdown = isset($_GET['ts']) ? '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.sourceMedium:' : '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.pagePath:';
131
-
132
- ?>
133
- <a class="ga-source-name" href="<?php echo esc_url( $source_page_url . $single_breakdown . str_replace( '+', '%20', str_replace( '2F', '~2F', str_replace( '%', '', urlencode( $source['url'] ) ) ) ) ); ?>/"
134
- target="_blank"><?php echo $source['name'] ?></a>
135
- <?php else: ?>
136
- <?php echo $source['name'] ?>
137
- <?php endif; ?>
138
- </td>
139
- <td style="text-align: right"><?php echo $source['number'] ?></td>
140
- <td>
141
- <div class="progress">
142
- <div class="progress-bar" role="progressbar"
143
- aria-valuenow="<?php echo $source['percent'] ?>" aria-valuemin="0"
144
- aria-valuemax="100"
145
- style="width: <?php echo $source['percent'] ?>%;"></div>
146
- <span style="margin-left: 10px;"><?php echo Ga_Helper::format_percent( $source['percent'] ); ?></span>
147
- </div>
148
- </td>
149
- </tr>
150
- <?php endforeach; ?>
151
- </table>
152
- </div>
153
- <a href="<?php echo esc_url( $source_page_url ); ?>/" class="view-report" target="_blank">
154
- <?php echo esc_html__('View Full Report' ); ?>
155
- </a>
156
- </div>
157
- </div>
158
- <?php endif; ?>
159
-
160
- <?php if ( ! empty( $chart ) ) :
161
-
162
- $label_count = isset($_GET['th']) ? $labels['thisMonth'] : $labels['thisWeek'];
163
-
164
- ?>
165
- <script type="text/javascript">
166
-
167
- ga_charts.init(function () {
168
-
169
- var data = new google.visualization.DataTable();
170
- data.addColumn('string', 'Day');
171
- data.addColumn('number', '<?php echo $label_count ?>');
172
- data.addColumn({type: 'string', role: 'tooltip', 'p': {'html': true}});
173
-
174
- <?php foreach ( $chart as $row ) : ?>
175
- data.addRow(['<?php echo $row['day'] ?>', <?php echo $row['current'] ?>, ga_charts.createTooltip('<?php echo $row['day'] ?>', '<?php echo $row['current'] ?>')]);
176
- <?php endforeach; ?>
177
- ga_charts.events(data);
178
- ga_charts.drawChart(data);
179
- ga_loader.hide();
180
- }
181
- );
182
- </script>
183
- <?php endif; ?>
184
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $account_data = json_decode(get_option('googleanalytics_account_data', true), true);
3
+ $selected_data = json_decode(get_option('googleanalytics_selected_account', true), true);
4
+
5
+ foreach ( $account_data as $properties ) {
6
+ if ( $properties['id'] === $selected_data[0] ) {
7
+ foreach ( $properties['webProperties'] as $web_property ) {
8
+ if ( $web_property['webPropertyId'] === $selected_data[1] ) {
9
+ $internal_prop = $web_property['internalWebPropertyId'];
10
+ }
11
+ }
12
+ }
13
+ }
14
+
15
+ $demo_enabled = get_option('googleanalytics_demographic');
16
+ $demo_enabled = !empty($demo_enabled) && $demo_enabled ? true: false;
17
+ $sevenorthirty = isset($_GET['th']) ? '30' : '7';
18
+ $selected7 = '7' === $sevenorthirty ? 'selected' : '';
19
+ $selected30 = '30' === $sevenorthirty ? 'selected' : '';
20
+ $selectedpage = isset($_GET['ts']) ? '' : 'selected';
21
+ $selectedsource = isset($_GET['ts']) ? 'selected' : '';
22
+ $report_url = 'https://analytics.google.com/analytics/web/#/report/content-pages/a' . $selected_data[0] . 'w' . $internal_prop . 'p' . $selected_data[2];
23
+ $source_page_url = isset($_GET['ts']) ? str_replace('content-pages', 'trafficsources-all-traffic', $report_url) : $report_url;
24
+ $demographic_page_url = str_replace('content-pages', 'visitors-demographics-overview', $report_url);
25
+ $type_label = isset($_GET['ts']) ? 'Traffic Sources' : 'Pages/Posts';
26
+ $thirty_url = isset($_GET['ts']) ? 'admin.php?page=googleanalytics&th&ts' : 'admin.php?page=googleanalytics&th';
27
+ $seven_url = isset($_GET['ts']) ? 'admin.php?page=googleanalytics&ts' : 'admin.php?page=googleanalytics';
28
+ $source_url = isset($_GET['th']) ? 'admin.php?page=googleanalytics&ts&th' : 'admin.php?page=googleanalytics&ts';
29
+ $page_view_url = isset($_GET['th']) ? 'admin.php?page=googleanalytics&th' : 'admin.php?page=googleanalytics';
30
+ $send_data = get_option('googleanalytics_send_data');
31
+ ?>
32
+ <div class="wrap ga-wrap" id="ga-stats-container">
33
+ <?php if ( ! empty( $chart ) ) : ?>
34
+ <div class="filter-choices">
35
+ <a href="<?php echo get_admin_url('', $seven_url ); ?>" class="<?php echo esc_attr( $selected7 ); ?>">
36
+ 7 days
37
+ </a>
38
+ <a href="<?php echo get_admin_url('', $thirty_url ); ?>" class="<?php echo esc_attr( $selected30 ); ?>">
39
+ 30 days
40
+ </a>
41
+ </div>
42
+ <div class="ga-panel ga-panel-default">
43
+ <div class="ga-panel-heading">
44
+ <strong>
45
+ <?php echo 'Pageviews - Last ' . esc_html( $sevenorthirty ) . ' days'; ?>
46
+ </strong>
47
+ </div>
48
+ <div class="ga-panel-body ga-chart">
49
+ <div id="chart_div" style="width: 100%;"></div>
50
+ <div class="ga-loader-wrapper stats-page">
51
+ <div class="ga-loader stats-page-loader"></div>
52
+ </div>
53
+ </div>
54
+ </div>
55
+ <?php endif; ?>
56
+
57
+ <?php if ( ! empty( $boxes ) ) : ?>
58
+ <div class="ga-panel ga-panel-default">
59
+ <div class="ga-panel-heading"><strong><?php echo 'Comparison - Last ' . esc_html( $sevenorthirty ) . ' days vs previous ' . esc_html( $sevenorthirty ) . ' days'; ?></strong>
60
+ </div>
61
+ <div class="ga-panel-body">
62
+ <div class="ga-row">
63
+ <?php foreach ( $boxes as $box ) : ?>
64
+ <div class="ga-box">
65
+ <div class="ga-panel ga-panel-default">
66
+ <div class="ga-panel-body ga-box-centered">
67
+ <div class="ga-box-label"><?php echo esc_html( $box['label'] ); ?></div>
68
+ <div class="ga-box-diff" style="color: <?php echo esc_attr( $box['color'] ); ?>;">
69
+ <?php echo Ga_Helper::format_percent( $box['diff'] ); ?>
70
+ </div>
71
+ <div class="ga-box-comparison"><?php echo $box['comparison']; ?></div>
72
+ </div>
73
+ </div>
74
+ </div>
75
+ <?php endforeach; ?>
76
+ </div>
77
+ </div>
78
+ </div>
79
+ <?php
80
+ endif;
81
+
82
+ include plugin_dir_path(__FILE__) . '/templates/demographic-chart.php';
83
+
84
+ if ( ! empty( $sources ) ) : ?>
85
+ <div class="filter-choices">
86
+ <a href="<?php echo get_admin_url('', $page_view_url); ?>" class="<?php echo esc_attr( $selectedpage ); ?>">
87
+ Page View
88
+ </a>
89
+ <a href="<?php echo get_admin_url('', $source_url); ?>" class="<?php echo esc_attr( $selectedsource ); ?>">
90
+ Traffic Source
91
+ </a>
92
+ </div>
93
+ <div class="ga-panel ga-panel-default">
94
+ <div class="ga-panel-heading"><strong><?php _e( "Top 10 " . $type_label . " by page views" ); ?></strong>
95
+ </div>
96
+ <div class="ga-panel-body">
97
+
98
+ <div id="table-container">
99
+ <table class="ga-table">
100
+ <tr>
101
+ <td colspan="2">
102
+ </td>
103
+ <th style="text-align: right;">
104
+ <?php _e( 'Pageviews' ); ?>
105
+ </th>
106
+ <th style="text-align: right;">
107
+ <?php echo '%'; ?>
108
+ </th>
109
+ </tr>
110
+ <tr>
111
+ <td colspan="2"></td>
112
+ <td class="ga-col-pageviews" style="text-align: right">
113
+ <div style="font-size: 16px;"><?php echo $sources['total'] ?></div>
114
+ <div style="color: grey; font-size: 10px;">% of
115
+ Total: <?php echo Ga_Helper::format_percent( ( ! empty( $sources['total'] ) ) ? number_format( $sources['sum'] / $sources['total'] * 100,
116
+ 2, '.', ' ' ) : 100 );
117
+ ?>
118
+ (<?php echo $sources['sum'] ?>)
119
+ </div>
120
+ </td>
121
+ <td class="ga-col-progressbar" style="text-align: right">
122
+ <div style="font-size: 16px;"><?php echo $sources['total'] ?></div>
123
+ <div style="color: grey; font-size: 10px;">% of
124
+ Total: <?php echo Ga_Helper::format_percent( ( ! empty( $sources['total'] ) ) ? number_format( $sources['sum'] / $sources['total'] * 100,
125
+ 2, '.', ' ' ) : 100 );
126
+ ?>
127
+ (<?php echo $sources['sum'] ?>)
128
+ </div>
129
+ </td>
130
+ </tr>
131
+ <?php foreach ( $sources['rows'] as $key => $source ): ?>
132
+ <tr>
133
+ <td style="width: 5%;text-align: right"><?php echo $key ?>.</td>
134
+ <td class="ga-col-name">
135
+ <?php if ( $source['name'] != '(direct) / (none)' ) :
136
+
137
+ $single_breakdown = isset($_GET['ts']) ? '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.sourceMedium:' : '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.pagePath:';
138
+
139
+ ?>
140
+ <a class="ga-source-name" href="<?php echo esc_url( $source_page_url . $single_breakdown . str_replace( '+', '%20', str_replace( '2F', '~2F', str_replace( '%', '', urlencode( $source['url'] ) ) ) ) ); ?>/"
141
+ target="_blank"><?php echo $source['name'] ?></a>
142
+ <?php else: ?>
143
+ <?php echo $source['name'] ?>
144
+ <?php endif; ?>
145
+ </td>
146
+ <td style="text-align: right"><?php echo $source['number'] ?></td>
147
+ <td>
148
+ <div class="progress">
149
+ <div class="progress-bar" role="progressbar"
150
+ aria-valuenow="<?php echo $source['percent'] ?>" aria-valuemin="0"
151
+ aria-valuemax="100"
152
+ style="width: <?php echo $source['percent'] ?>%;"></div>
153
+ <span style="margin-left: 10px;"><?php echo Ga_Helper::format_percent( $source['percent'] ); ?></span>
154
+ </div>
155
+ </td>
156
+ </tr>
157
+ <?php endforeach; ?>
158
+ </table>
159
+ </div>
160
+ <a href="<?php echo esc_url( $source_page_url ); ?>/" class="view-report" target="_blank">
161
+ <?php echo esc_html__('View Full Report' ); ?>
162
+ </a>
163
+ </div>
164
+ </div>
165
+ <?php endif; ?>
166
+
167
+ <?php if ( ! empty( $chart ) ) :
168
+
169
+ $label_count = isset($_GET['th']) ? $labels['thisMonth'] : $labels['thisWeek'];
170
+
171
+ ?>
172
+ <script type="text/javascript">
173
+
174
+ ga_charts.init(function () {
175
+
176
+ var data = new google.visualization.DataTable();
177
+ var demoGenderData = new google.visualization.DataTable();
178
+ var demoAgeData = new google.visualization.DataTable();
179
+
180
+ data.addColumn('string', 'Day');
181
+ data.addColumn('number', '<?php echo $label_count ?>');
182
+ data.addColumn({type: 'string', role: 'tooltip', 'p': {'html': true}});
183
+
184
+ <?php foreach ( $chart as $row ) : ?>
185
+ data.addRow(['<?php echo $row['day'] ?>', <?php echo $row['current'] ?>, ga_charts.createTooltip('<?php echo $row['day'] ?>', '<?php echo $row['current'] ?>')]);
186
+ <?php endforeach; ?>
187
+ ga_charts.events(data);
188
+ ga_charts.drawChart(data);
189
+ ga_loader.hide();
190
+
191
+ // Demographic gender chart
192
+ <?php
193
+ $demoGenderData[0] = ['Gender', 'The gender of visitors'];
194
+ $x = 1;
195
+ foreach ( $gender_chart as $type => $amount ) {
196
+ $demoGenderData[$x] = [ucfirst($type), intval($amount)];
197
+ $x++;
198
+ } ?>
199
+
200
+ ga_charts.drawDemoGenderChart(<?php echo json_encode($demoGenderData); ?>);
201
+ ga_loader.hide();
202
+
203
+ // Demographic age chart
204
+ <?php
205
+ $demoAgeData[0] = ['Age', 'Average age range of visitors'];
206
+ $x = 1;
207
+ foreach ( $age_chart as $type => $amount ) {
208
+ $demoAgeData[$x] = [$type, intval($amount)];
209
+ $x++;
210
+ } ?>
211
+ ga_charts.drawDemoAgeChart(<?php echo json_encode($demoAgeData); ?>);
212
+ ga_loader.hide();
213
+
214
+ <?php if (Ga_Helper::are_features_enabled() && !empty($send_data) && "true" === $send_data) : ?>
215
+ ga_events.sendDemoData(<?php echo get_option('googleanalytics_demo_data'); ?>);
216
+ <?php
217
+ update_option('googleanalytics_demo_date', date("Y-m-d"));
218
+ update_option('googleanalytics_send_data', "false");
219
+ endif;
220
+ ?>
221
+ }
222
+ );
223
+ </script>
224
+ <?php endif; ?>
225
+ <div class="demo-enable-popup">
226
+ <p>
227
+ We are deploying additional data from your Google Analytics account to your WordPress dashboard
228
+ as a free service to assist you in operating your WordPress site.
229
+ This will result in Google Analytics having access to data collected from Your site(s)
230
+ and subject to Google’s privacy policies as described in the <a
231
+ href="http://www.sharethis.com/publisher-terms-of-use/" target="_blank">ShareThis Publisher TOU</a>.
232
+ We will also use the aggregate demographic data related to your site for analytic purposes.
233
+ We will not sell or transfer any of the demographic data relating to your site to any other party.
234
+ For more information, please visit the <a
235
+ href="http://www.sharethis.com/news/2016/12/sharethis-adds-analytics-plugin-to-suite-of-tools/"
236
+ target="_blank">ShareThis Privacy Policy</a> and <a
237
+ href="http://www.sharethis.com/publisher-terms-of-use/" target="_blank">Publisher TOU</a>.
238
+ </p>
239
+ <button id="enable-demographic">I accept</button>
240
+ <button class="close-demo-modal">Decline</button>
241
+ </div>
242
+ </div>
view/templates/appearance.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="col-md-12">
2
+ <h3><?php echo esc_html__( 'Form Color', 'googleanalytics' ); ?></h3>
3
+ </div>
4
+
5
+ <div id="sharethis-form-color" class="col-md-12">
6
+ <?php foreach ($colors as $color) : ?>
7
+ <div class="color<?php echo isset($gdpr_config['color']) && $color === $gdpr_config['color'] ? ' selected' : ''; ?>"
8
+ data-value="<?php echo esc_attr($color); ?>"
9
+ style="max-width: 30px; max-height: 30px; overflow: hidden;"
10
+ >
11
+ <span style="content: ' '; background-color:<?php echo esc_html($color); ?>; padding: 40px;"></span>
12
+ </div>
13
+ <?php endforeach; ?>
14
+ </div>
view/templates/demographic-chart.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php if(!$demo_enabled) : ?>
2
+ <div class="demo-ad ga-panel ga-panel-default">
3
+ <div class="ga-panel-heading">
4
+ <strong>
5
+ <?php esc_html_e('Get Demographic Data!'); ?>
6
+ <button id="demographic-popup">Click Here To Enable</button>
7
+ </strong>
8
+ </div>
9
+ <img src="<?php echo trailingslashit(get_home_url()) . 'wp-content/plugins/googleanalytics/assets/images/demo-ad.png'; ?>" />
10
+ </div>
11
+ <?php else: ?>
12
+ <div class="filter-choices">
13
+ <a href="<?php echo get_admin_url('', $seven_url ); ?>" class="<?php echo esc_attr( $selected7 ); ?>">
14
+ 7 days
15
+ </a>
16
+ <a href="<?php echo get_admin_url('', $thirty_url ); ?>" class="<?php echo esc_attr( $selected30 ); ?>">
17
+ 30 days
18
+ </a>
19
+ </div>
20
+ <div class="demo-ad ga-panel ga-panel-default">
21
+ <div class="ga-panel-heading">
22
+ <strong>
23
+ <?php esc_html_e('Demographic by sessions'); ?>
24
+ </strong>
25
+ </div>
26
+ <div class="ga-demo-chart">
27
+ <div class="ga-panel-body ga-chart gender">
28
+ <div id="demo_chart_gender_div" style="width: 100%;"></div>
29
+ <div class="ga-loader-wrapper stats-page">
30
+ <div class="ga-loader stats-page-loader"></div>
31
+ </div>
32
+ </div>
33
+ <div class="ga-panel-body ga-chart gender">
34
+ <div id="demo_chart_age_div" style="width: 100%;"></div>
35
+ <div class="ga-loader-wrapper stats-page">
36
+ <div class="ga-loader stats-page-loader"></div>
37
+ </div>
38
+ </div>
39
+ </div>
40
+ </div>
41
+ <a href="<?php echo esc_url( $demographic_page_url ); ?>/" class="view-report" target="_blank">
42
+ <?php echo esc_html__('View Full Report' ); ?>
43
+ </a>
44
+ <hr>
45
+ <?php
46
+ endif;
view/templates/demographic.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $enabledisable = get_option('googleanalytics_demographic') === '1' ? 'Disable' : 'Enable';
3
+ ?>
4
+ <?php if ('Enable' === $enabledisable) : ?>
5
+ <tr valign="top">
6
+ <th scope="row"><?php esc_html_e('Enable demographic charts', 'googleanalytics'); ?>:</th>
7
+ </tr>
8
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
9
+ <td>
10
+ <button id="demographic-popup"><?php esc_html_e('Enable', 'googleanalytics'); ?></button>
11
+ </td>
12
+ <?php else : ?>
13
+ <td>
14
+ <label class="<?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
15
+ <button class="gdpr-enable" disabled="disabled"><?php esc_html_e('Enable'); ?></button>
16
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
17
+ </label>
18
+ </td>
19
+ <?php endif; ?>
20
+ <?php endif; ?>
view/templates/exclusions.php ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Configure tool template for gdpr onboarding
4
+ */
5
+ ?>
6
+ <div class="vendor-exclusion">
7
+ <div class="vendor-table">
8
+ <div class="st-box ">
9
+ <div class="vendor-table-header">
10
+ <div class="vendor-name">
11
+ <h3>
12
+ <?php esc_html_e('Vendor names', 'googleanalytics'); ?>
13
+ </h3>
14
+ <div class="st-input vendor-search">
15
+ <input autocomplete="off" id="vendor-search " placeholder="Search for vendor" type="text">
16
+ </div>
17
+ </div>
18
+ </div>
19
+ <p class="vendor-info">
20
+ <?php esc_html_e('Manage third-party vendors asking for consent across your sites.', 'sharethis-share-buttons'); ?>
21
+ </p>
22
+ </div>
23
+ <div class="vendor-table-body">
24
+ <?php foreach ($vendors as $vendor ) : ?>
25
+ <div class="vendor-table-cell">
26
+ <a name="<?php echo strtolower(esc_html($vendor['name'])); ?>"></a>
27
+ <a name="<?php echo strtolower(esc_html(explode(' ', $vendor['name'])[0])); ?>"></a>
28
+ <div class="vendor-table-cell-wrapper switch">
29
+ <label>
30
+ <input data-id="<?php echo esc_attr($vendor['id']); ?>" type="checkbox" name="vendor[<?php echo esc_attr($vendor['id']); ?>]" value="consent" />
31
+ <span class="lever"></span>
32
+ <strong><?php echo esc_html($vendor['name']); ?></strong>
33
+ </label>
34
+ <div class="vendor-accor">
35
+ <p>
36
+ <strong><?php esc_html_e('Privacy Policy: ', 'sharethis-share-buttons'); ?></strong>
37
+ <?php echo '<a href="' . $vendor['policyUrl'] . '" target="_blank">' .
38
+ $vendor['policyUrl'] . '</a>'; ?>
39
+ </p>
40
+ <p>
41
+ <strong><?php esc_html_e('Purposes: ', 'sharethis-share-buttons'); ?></strong>
42
+ <div class="vendor-purpose-list">
43
+ <?php foreach ($vendor['purposes'] as $purpose) : ?>
44
+ <p><?php echo esc_html($purposes[$purpose]); ?></p>
45
+ <?php endforeach; ?>
46
+ </div>
47
+ </p>
48
+ </div>
49
+ </div>
50
+ </div>
51
+ <?php endforeach; ?>
52
+ </div>
53
+ </div>
54
+ </div>
view/templates/gdpr-config.php ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Appearence display for gdpr config.
4
+ */
5
+
6
+ // Template vars.
7
+ $colors = [
8
+ '#e31010',
9
+ '#000000',
10
+ '#ffffff',
11
+ '#09cd18',
12
+ '#ff6900',
13
+ '#fcb900',
14
+ '#7bdcb5',
15
+ '#00d084',
16
+ '#8ed1fc',
17
+ '#0693e3',
18
+ '#abb8c3',
19
+ '#eb144c',
20
+ '#f78da7',
21
+ '#9900ef',
22
+ '#b80000',
23
+ '#db3e00',
24
+ '#fccb00',
25
+ '#008b02',
26
+ '#006b76',
27
+ '#1273de',
28
+ '#004dcf',
29
+ '#5300eb',
30
+ '#eb9694',
31
+ '#fad0c3',
32
+ '#fef3bd',
33
+ '#c1e1c5',
34
+ '#bedadc',
35
+ '#c4def6',
36
+ '#bed3f3',
37
+ '#d4c4fb'
38
+ ];
39
+
40
+
41
+ // User type options.
42
+ $user_types = array(
43
+ 'eu' => esc_html__('Only visitors in the EU', 'sharethis-custom'),
44
+ 'always' => esc_html__('All visitors globally', 'sharethis-custom'),
45
+ );
46
+
47
+ // Consent type options.
48
+ $consent_types = array(
49
+ 'global' => esc_html__(
50
+ 'Global: Publisher consent = 1st party cookie; Vendors consent = 3rd party cookie',
51
+ 'sharethis-custom'
52
+ ),
53
+ 'publisher' => esc_html__(
54
+ 'Service: publisher consent = 1st party cookie; Vendors consent = 1st party cookie',
55
+ 'sharethis-custom'
56
+ ),
57
+ );
58
+
59
+ $languages = array(
60
+ 'English' => 'en',
61
+ 'German' => 'de',
62
+ 'Spanish' => 'es',
63
+ 'French' => 'fr'
64
+ );
65
+
66
+ $publisher_name = !empty($gdpr_config['publisher_name']) ? $gdpr_config['publisher_name'] : '';
67
+ $enabled = !empty($gdpr_config['enabled']) ? $gdpr_config['enabled'] : false;
68
+ ?>
69
+ <div id="adblocker-notice" class="notice notice-error is-dismissible">
70
+ <p>
71
+ <?php echo esc_html__( 'It appears you have an ad blocker enabled. To avoid affecting this plugin\'s functionality, please disable while using its admin configurations and registrations. Thank you.', 'sharethis-share-buttons' ); ?>
72
+ </p>
73
+ </div>
74
+ <div id="detectadblock">
75
+ <div class="adBanner">
76
+ </div>
77
+ </div>
78
+ <div class="gdpr-platform">
79
+ <div class="switch">
80
+ <div class="purpose-item">
81
+ <label class="enable-tool">
82
+ <?php echo esc_html__('Enable GDPR', 'googleanalytics'); ?>
83
+ <input type="checkbox" name="gdpr-enable" <?php echo checked('true', $enabled); ?>/>
84
+ <span class="lever"></span>
85
+ </label>
86
+ </div>
87
+ </div>
88
+ <div class="well">
89
+ <label class="control-label">
90
+ <?php echo esc_html__('PUBLISHER NAME * (this will be displayed in the consent tool)',
91
+ 'sharethis-share-buttons'); ?>
92
+ </label>
93
+ <div class="input-div">
94
+ <input type="text" id="sharethis-publisher-name" placeholder="Enter your company name" value="<?php echo esc_attr($publisher_name); ?>">
95
+ </div>
96
+ <label class="control-label">
97
+ <?php echo esc_html__('WHICH USERS SHOULD BE ASKED FOR CONSENT?',
98
+ 'sharethis-share-buttons'); ?>
99
+ </label>
100
+ <div class="input-div">
101
+ <select id="sharethis-user-type">
102
+ <?php foreach ($user_types as $user_value => $name) : ?>
103
+ <option value="<?php echo esc_attr($user_value); ?>" <?php echo isset($gdpr_config['display']) ? selected($user_value, $gdpr_config['display']) : ''; ?>>
104
+ <?php echo esc_html($name); ?>
105
+ </option>
106
+ <?php endforeach; ?>
107
+ </select>
108
+ </div>
109
+ <label class="control-label">
110
+ <?php echo esc_html__('CONSENT SCOPE', 'sharethis-share-buttons'); ?>
111
+ </label>
112
+ <div class="input-div">
113
+ <select id="sharethis-consent-type">
114
+ <?php foreach ($consent_types as $consent_value => $c_name) : ?>
115
+ <option
116
+ value="<?php echo esc_attr($consent_value); ?>"
117
+ <?php echo isset($gdpr_config['scope']) ? selected($consent_value, $gdpr_config['scope']) : ''; ?>
118
+ >
119
+ <?php echo esc_html($c_name); ?>
120
+ </option>
121
+ <?php endforeach; ?>
122
+ </select>
123
+ </div>
124
+ <label class="control-label">
125
+ <?php echo esc_html__('SELECT LANGUAGE', 'sharethis-share-buttons'); ?>
126
+ </label>
127
+ <div class="input-div">
128
+ <select id="st-language">
129
+ <?php foreach ($languages as $language => $code) : ?>
130
+ <option value="<?php echo esc_attr($code); ?>" <?php echo isset($gdpr_config['language']) ? selected($code, $gdpr_config['language']) : ''; ?>>
131
+ <?php echo esc_html($language); ?>
132
+ </option>
133
+ <?php endforeach; ?>
134
+ </select>
135
+ </div>
136
+ </div>
137
+ <div class="accor-wrap">
138
+ <div class="accor-tab">
139
+ <span class="accor-arrow">&#9658;</span>
140
+ <?php echo esc_html__( 'Appearance', 'simple-share-buttons-adder' ); ?>
141
+ </div>
142
+ <div class="accor-content">
143
+ <div class="well">
144
+ <?php include plugin_dir_path(__FILE__) . 'appearance.php'; ?>
145
+ </div>
146
+ </div>
147
+ </div>
148
+ <div class="accor-wrap">
149
+ <div class="accor-tab">
150
+ <span class="accor-arrow">&#9658;</span>
151
+ <?php echo esc_html__( 'Purposes', 'simple-share-buttons-adder' ); ?>
152
+ </div>
153
+ <div class="accor-content">
154
+ <div class="well">
155
+ <?php include plugin_dir_path(__FILE__) . 'purposes.php'; ?>
156
+ </div>
157
+ </div>
158
+ </div>
159
+ <div class="accor-wrap">
160
+ <div class="accor-tab">
161
+ <span class="accor-arrow">&#9658;</span>
162
+ <?php echo esc_html__( 'Exclusions', 'googleanalytics' ); ?>
163
+ </div>
164
+ <div class="accor-content">
165
+ <div class="well">
166
+ <?php include plugin_dir_path(__FILE__) . 'exclusions.php'; ?>
167
+ </div>
168
+ </div>
169
+ </div>
170
+ </div>
171
+ <div class="gdpr-submit-button">
172
+ <button class="gdpr-submit">Update</button>
173
+ </div>
view/templates/gdpr.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php if(empty($gdpr_config)) : ?>
2
+ <tr valign="top">
3
+ <th scope="row"><?php esc_html_e('Enable GDPR Consent Management Tool', 'googleanalytics'); ?>:</th>
4
+ </tr>
5
+ <tr valign="top">
6
+ <?php if ( Ga_Helper::are_features_enabled() ) : ?>
7
+ <td>
8
+ <button class="gdpr-enable"><?php esc_html_e('Enable'); ?></button>
9
+ </td>
10
+ <?php else : ?>
11
+ <td>
12
+ <label class="<?php echo ( ! Ga_Helper::are_features_enabled() ) ? 'label-grey ga-tooltip' : '' ?>">
13
+ <button class="gdpr-enable" disabled="disabled"><?php esc_html_e('Enable'); ?></button>
14
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e( $tooltip ); ?></span>
15
+ </label>
16
+ </td>
17
+ <?php endif; ?>
18
+ </tr>
19
+ <?php endif; ?>
view/templates/purposes.php ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <h3>
2
+ <?php echo esc_html__('WHY ARE YOU COLLECTING CUSTOMER DATA?', 'googleanalytics'); ?>
3
+ </h3>
4
+
5
+ <div id="publisher-purpose" class="switch">
6
+ <div class="empty-choices col-md-12">
7
+ <a id="see-st-choices" class="st-rc-link medium-btn col-md-6" href="#">See Suggested Choices</a>
8
+ <a id="clear-choices" class="st-rc-link medium-btn col-md-6" href="#">Clear Choices</a>
9
+ </div>
10
+ <div class="purpose-item">
11
+ <div class="title">
12
+ <?php echo esc_html__(
13
+ '1) Store and/or access information on a device (Do you collect information on users on your site through cookies or site identifiers?)',
14
+ 'googleanalytics'
15
+ ); ?>
16
+ </div>
17
+ <label>
18
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
19
+ <input data-id="1" type="checkbox" name="purposes[1]" value="consent" checked/>
20
+ <span class="lever"></span>
21
+ </label>
22
+ </div>
23
+ <div class="purpose-item">
24
+ <div class="title">
25
+ <?php echo esc_html__(
26
+ '2) Select basic ads (Do you serve ads on your site?)',
27
+ 'googleanalytics'
28
+ ); ?>
29
+ </div>
30
+ <label>
31
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
32
+ <input data-id="2" type="radio" name="purposes[2]" value="consent" checked/>
33
+ <span class="lever"></span>
34
+ </label>
35
+ <label>
36
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
37
+ <input data-id="2" type="radio" name="purposes[2]" value="legitimate"/>
38
+ <span class="lever"></span>
39
+ </label>
40
+ </div>
41
+ <div class="purpose-item">
42
+ <div class="title">
43
+ <?php echo esc_html__(
44
+ '3) Create a personalised ads profile (Do you create personalised advertising profiles associated with users on your site (ie: profiles based on demographic information, location, user’s activity)?)',
45
+ 'googleanalytics'
46
+ ); ?>
47
+ </div>
48
+ <label>
49
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
50
+ <input data-id="3" type="radio" name="purposes[3]" value="consent" checked/>
51
+ <span class="lever"></span>
52
+ </label>
53
+ <label>
54
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
55
+ <input data-id="3" type="radio" name="purposes[3]" value="legitimate"/>
56
+ <span class="lever"></span>
57
+ </label>
58
+ </div>
59
+ <div class="purpose-item">
60
+ <div class="title">
61
+ <?php echo esc_html__(
62
+ '4) Select personalised ads (Do you show ads to users based on this user profile)',
63
+ 'googleanalytics'
64
+ ); ?>
65
+ </div>
66
+ <label>
67
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
68
+ <input data-id="4" type="radio" name="purposes[4]" value="consent" checked/>
69
+ <span class="lever"></span>
70
+ </label>
71
+ <label>
72
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
73
+ <input data-id="4" type="radio" name="purposes[4]" value="legitimate"/>
74
+ <span class="lever"></span>
75
+ </label>
76
+ </div>
77
+ <div class="purpose-item">
78
+ <div class="title">
79
+ <?php echo esc_html__(
80
+ '5) Create a personalised content profile (Do you build a personalized content profile associated with users on your site based on the type of content they have viewed?)',
81
+ 'googleanalytics'
82
+ ); ?>
83
+ </div>
84
+ <label>
85
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
86
+ <input data-id="5" type="radio" name="purposes[5]" value="consent" checked />
87
+ <span class="lever"></span>
88
+ </label>
89
+ <label>
90
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
91
+ <input data-id="5" type="radio" name="purposes[5]" value="legitimate"/>
92
+ <span class="lever"></span>
93
+ </label>
94
+ </div>
95
+ <div class="purpose-item">
96
+ <div class="title">
97
+ <?php echo esc_html__(
98
+ '6) Select personalised content (Do you serve content to the user on your site based on your recorded content interests)',
99
+ 'googleanalytics'
100
+ ); ?>
101
+ </div>
102
+ <label>
103
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
104
+ <input data-id="6" type="radio" name="purposes[6]" value="consent" checked />
105
+ <span class="lever"></span>
106
+ </label>
107
+ <label>
108
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
109
+ <input data-id="6" type="radio" name="purposes[6]" value="legitimate"/>
110
+ <span class="lever"></span>
111
+ </label>
112
+ </div>
113
+ <div class="purpose-item">
114
+ <div class="title">
115
+ <?php echo esc_html__(
116
+ '7) Measure ad performance (Do you measure the performance of advertisements on your site)',
117
+ 'googleanalytics'
118
+ ); ?>
119
+ </div>
120
+ <label>
121
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
122
+ <input data-id="7" type="radio" name="purposes[7]" value="consent" checked/>
123
+ <span class="lever"></span>
124
+ </label>
125
+ <label>
126
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
127
+ <input data-id="7" type="radio" name="purposes[7]" value="legitimate"/>
128
+ <span class="lever"></span>
129
+ </label>
130
+ </div>
131
+ <div class="purpose-item">
132
+ <div class="title">
133
+ <?php echo esc_html__(
134
+ '8) Measure content performance (Do you measure the performance of content served to your site visitors?)',
135
+ 'googleanalytics'
136
+ ); ?>
137
+ </div>
138
+ <label>
139
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
140
+ <input data-id="8" type="radio" name="purposes[8]" value="consent" checked/>
141
+ <span class="lever"></span>
142
+ </label>
143
+ <label>
144
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
145
+ <input data-id="8" type="radio" name="purposes[8]" value="legitimate"/>
146
+ <span class="lever"></span>
147
+ </label>
148
+ </div>
149
+ <div class="purpose-item">
150
+ <div class="title">
151
+ <?php echo esc_html__(
152
+ '9) Apply market research to generate audience insights (Do you aggregate reporting on the ads or content show to your site visitors to advertisers)',
153
+ 'googleanalytics'
154
+ ); ?>
155
+ </div>
156
+ <label>
157
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
158
+ <input data-id="9" type="radio" name="purposes[9]" value="consent" checked/>
159
+ <span class="lever"></span>
160
+ </label>
161
+ <label>
162
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
163
+ <input data-id="9" type="radio" name="purposes[9]" value="legitimate"/>
164
+ <span class="lever"></span>
165
+ </label>
166
+ </div>
167
+ <div class="purpose-item">
168
+ <div class="title">
169
+ <?php echo esc_html__(
170
+ '10) Develop and improve products (Do you use data collected on your site visitors to improve your systems or software or create new products?)',
171
+ 'googleanalytics'
172
+ ); ?>
173
+ </div>
174
+ <label>
175
+ <?php echo esc_html__('Consent', 'googleanalytics'); ?>
176
+ <input data-id="10" type="radio" name="purposes[10]" value="consent" checked/>
177
+ <span class="lever"></span>
178
+ </label>
179
+ <label>
180
+ <?php echo esc_html__('Legitimate Interest', 'googleanalytics'); ?>
181
+ <input data-id="10" type="radio" name="purposes[10]" value="legitimate"/>
182
+ <span class="lever"></span>
183
+ </label>
184
+ </div>
185
+ </div>