PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.2.0
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.2.0
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / wp-analytify.php

wp-analytify.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) 1.2.0, at wp-analytify.php

1,009 lines 27.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Analytify - Google Analytics Dashboard
4 * Plugin URI: http://wp-analytify.com/details
5 * Description: Analytify brings a brand new and modern feeling Google Analytics superbly integrated with WordPress Dashboard. It presents the statistics in a beautiful way under the WordPress Posts/Pages at front end, backend and in its own Dashboard. This provides Stats from Country, Referrers, Social media, General stats, New visitors, Returning visitors, Exit pages, Browser wise and Top keywords. This plugin provides the RealTime statistics in a new UI which is easy to understand & looks good.
6 * Version: 1.2.0
7 * Author: WPBrigade
8 * Author URI: http://wpbrigade.com/
9 * License: GPLv3
10 * Text Domain: wp-analytify
11 * Min WP Version: 3.0
12 * Max WP Version: 4.3.1
13 * Domain Path: /lang
14 */
15
16 // Exit if accessed directly
17 if ( !defined( 'ABSPATH' ) ) exit;
18
19 ini_set( 'include_path', dirname(__FILE__) . '/lib/' );
20
21 include_once( 'inc/class-wpa-ajax.php' );
22
23 if ( !class_exists( 'WP_Analytify' ) ) {
24
25 if ( !class_exists( 'Analytify_General_FREE' ) ) {
26
27 require_once WP_PLUGIN_DIR .'/wp-analytify/analytify-general.php';
28 }
29
30 class WP_Analytify extends Analytify_General_FREE{
31
32 public $token = false;
33 public $client = null;
34
35 function __construct() {
36
37 parent::__construct();
38
39 if ( !class_exists( 'Analytify_Google_Client' ) ) {
40
41 require_once ANALYTIFY_LIB_PATH . 'Google/Client.php';
42 require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php';
43 }
44
45 add_action( 'plugin_action_links', array(
46 $this,
47 'pa_plugin_links'
48 ),10,2);
49
50 add_action( 'plugin_row_meta', array(
51 $this,
52 'analytify_plugin_row_meta'
53 ),10,2);
54
55 add_action( 'admin_enqueue_scripts', array(
56 $this,
57 'pa_scripts'
58 ));
59 add_action( 'admin_enqueue_scripts', array(
60 $this,
61 'pa_styles'
62 ));
63
64 add_action( 'wp_enqueue_scripts', array(
65 $this,
66 'pa_front_scripts'
67 ));
68 add_action( 'wp_enqueue_scripts', array(
69 $this,
70 'pa_front_styles'
71 ));
72
73 add_action( 'admin_menu', array(
74 $this,
75 'wpa_add_menu'
76 ));
77
78 add_action( 'admin_init', array(
79 $this,
80 'wpa_check_authentication'
81 ));
82
83
84 /*Insert Google Analytics Code*/
85 if( get_option( 'analytify_code') == 1 ) {
86
87 add_action( 'wp_head', array(
88 $this,
89 'analytify_add_analytics_code'
90 ));
91 }
92
93 add_action( 'wp_ajax_nopriv_get_ajax_single_admin_analytics', array(
94 $this,
95 'get_ajax_single_admin_analytics'
96 ));
97
98 add_action( 'wp_ajax_get_ajax_single_admin_analytics', array(
99 $this,
100 'get_ajax_single_admin_analytics'
101 ));
102
103 add_action( 'wp_ajax_get_ajax_secret_keys', array(
104 $this,
105 'get_ajax_secret_keys'
106 ));
107
108 if( get_option( 'analytify_disable_front') == 0 ) {
109
110 add_filter( 'the_content', array(
111 $this,
112 'get_single_front_analytics'
113 ));
114 }
115
116 /**
117 * load Analytics under the EDIT POST Screen
118 * add action runs only for admin section and load metabox.
119 */
120
121 if ( is_admin() ) {
122 add_action( 'load-post.php', array(
123 $this,
124 'load_metaboxes'
125 ));
126 }
127
128 // Show welcome message when user activate plugin.
129 if ( get_option( 'show_tracking_pointer_1' ) != 1 ) {
130
131 add_action( 'admin_print_footer_scripts', array(
132 $this,
133 'pa_welcome_message'
134 ) );
135 }
136
137 /*
138 * localization language file filter
139 */
140
141 add_action( 'init', array(
142 $this,
143 'analytify_textdomain') );
144
145 //add_action( 'admin_footer', array( &$this, 'profile_warning' ) );
146 /*add_action( 'admin_footer', array(
147 &$this,
148 'profile_warning'
149 ));*/
150 add_action('admin_notices', array(
151 $this,
152 'analytify_admin_notice')
153 );
154
155 add_action('admin_init', array(
156 $this,
157 'analytify_nag_ignore')
158 );
159
160 }
161
162 /**
163 * Save Authentication code on return
164 * @since 1.2.8
165 */
166 public function wpa_check_authentication() {
167
168 if( isset($_GET['code']) && isset($_GET['state']) ) {
169
170 $key_google_token = sanitize_text_field( $_GET['code'] );
171 WP_Analytify::pt_save_data( $key_google_token );
172 wp_redirect( $_GET['state'].'&tab=profile' );
173 }
174 }
175
176 /*
177 * Set plugin language/localizations files directory
178 */
179 public function analytify_textdomain() {
180
181 $plugin_dir = basename( dirname(__FILE__) );
182 load_plugin_textdomain( 'wp-analytify', false , $plugin_dir . '/lang/');
183
184 }
185
186 /*
187 * Show analytics sections under the posts/pages in the metabox.
188 */
189
190 public function load_metaboxes() {
191
192 add_action( 'add_meta_boxes', array(
193 $this,
194 'show_admin_single_analytics_add_metabox'
195 ));
196 }
197
198 /*
199 * Show metabox under each Post type to display Analytics of single post/page in wp-admin
200 */
201 public function show_admin_single_analytics_add_metabox() {
202
203 $post_types = get_option( 'analytify_posts_stats' );
204
205 // Don't load boxes/sections if no any post type is selected.
206 if( !empty($post_types))
207 foreach ( $post_types as $post_type ) {
208
209 add_meta_box('pa-single-admin-analytics', // $id
210 'Analytify: Google Analytics of this page.', // $title
211 array(
212 'WP_Analytify',
213 'show_admin_single_analytics'
214 ), // $callback
215
216 $post_type, // $posts
217 'normal', // $context
218 'high' // $priority
219 );
220 } //$post_types as $post_type
221 }
222
223
224 public static function get_ajax_secret_keys(){
225
226 $response = wp_remote_get( "http://wp-analytify.com/secret/keys.json" );
227 if( is_wp_error( $response ) ) {
228 $error_message = $response->get_error_message();
229 echo "Something went wrong: $error_message";
230 } else {
231 print_r($response['body']);
232 }
233 die();
234 }
235
236 /*
237 * Show Analytics of single post/page in wp-admin under EDIT screen.
238 */
239 public static function show_admin_single_analytics() {
240
241 global $post;
242
243 $back_exclude_posts = explode( ',', get_option( 'post_analytics_exclude_posts_back' ));
244
245 if ( is_array( $back_exclude_posts ) ) {
246
247 if ( in_array( $post->ID, $back_exclude_posts ) ) {
248
249 _e('This post is excluded and will not show Analytics.');
250
251 return;
252 }
253 }
254
255 $urlPost = '';
256 $wp_analytify = new WP_Analytify();
257 $urlPost = parse_url( get_permalink( $post->ID ) );
258 $start_date = ''; $end_date = ''; $urlpost = '' ;
259 $is_access_level = get_option( 'post_analytics_access_back' );
260
261 if( $wp_analytify->pa_check_roles( $is_access_level ) ){ ?>
262
263 <div class="pa-filter">
264 <table cellspacing="0" cellpadding="0" width="400">
265 <tbody>
266 <tr>
267 <td width="0">
268 <input type="text" id="start_date" name="start_date">
269 </td>
270 <td width="0">
271 <input type="text" id="end_date" name="end_date">
272 </td>
273 <input type="hidden" name="urlpost" id="urlpost" value="<?php echo $urlPost['path']; ?>">
274 <td width="0">
275 <input type="button" id="view_analytics" name="view_analytics" value="View Analytics" class="button-primary btn-green">
276 </td>
277 </tr>
278 </tbody>
279 </table>
280 </div>
281 <div class="loading" style="display:none">
282 <img src="<?php echo plugins_url('images/loading.gif', __FILE__);?>">
283 </div>
284 <div class="show-hide">
285 <?php $wp_analytify->get_single_admin_analytics( $start_date, $end_date, $post->ID, 0 ); ?>
286 </div>
287 <?php
288 }
289 else{
290 echo _e( 'You are not allowed to see stats', 'wp-analytify' );
291 }
292 }
293
294 // Add Google Analytics JS code
295 public function analytify_add_analytics_code() {
296
297 global $current_user;
298
299 $roles = $current_user->roles;
300
301 if ( isset( $roles[0] ) and in_array( $roles[0], get_option( 'display_tracking_code' ) )) {
302
303 }
304 else{
305
306 echo '<!-- This code is added by Analytify v - ' . ANALYTIFY_VERSION . ' http://wp-analytify.com/ !--> ';
307
308 if ( get_option( 'analytify_tracking_code' ) == 'universal' ) { ?>
309
310 <script>
311 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
312 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
313 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
314 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
315 ga('create', '<?php echo get_option( "webPropertyId" );?>', 'auto');
316 ga('send', 'pageview');
317 </script>
318
319 <?php
320 }
321
322 if ( get_option( 'analytify_tracking_code' ) == 'ga' ) { ?>
323
324 <script type="text/javascript">//<![CDATA[
325 var _gaq = _gaq || [];
326 _gaq.push(['_setAccount', '<?php echo get_option( "webPropertyId" );?>']);
327 _gaq.push(['_trackPageview']);
328 (function () {
329 var ga = document.createElement('script');
330 ga.type = 'text/javascript';
331 ga.async = true;
332 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
333 var s = document.getElementsByTagName('script')[0];
334 s.parentNode.insertBefore(ga, s);
335 })();
336 //]]>
337 </script>
338
339 <?php
340 }
341
342 echo '<!-- This code is added by Analytify v - ' . ANALYTIFY_VERSION . ' !-->';
343 }
344 }
345
346 /**
347 * Add a link to the settings page to the plugins list
348 */
349 public function pa_plugin_links( $links, $file ) {
350
351 static $this_plugin;
352
353 if ( empty( $this_plugin ) ){
354
355 $this_plugin = 'wp-analytify/wp-analytify.php';
356 }
357
358 if ( $file == $this_plugin ) {
359
360 $settings_link = '<a href="' . admin_url("admin.php?page=analytify-settings") . '">' . __( 'Settings', 'wp-analytify' ) . '</a> | <a href="' . admin_url("admin.php?page=analytify-dashboard") . '">' . __( 'Dashboard', 'wp-analytify' ) . '</a>';
361 array_unshift( $links, $settings_link );
362 }
363
364 return $links;
365 }
366
367 /**
368 * Plugin row meta links
369 *
370 * @since 1.1
371 * @param array $input already defined meta links
372 * @param string $file plugin file path and name being processed
373 * @return array $input
374 */
375 function analytify_plugin_row_meta( $input, $file ) {
376 if ( $file != 'wp-analytify/wp-analytify.php' )
377 return $input;
378
379 $links = array(
380 '<a href="http://wp-analytify.com/">' . esc_html__( 'Buy Pro Version', 'edd' ) . '</a>',
381 '<a href="http://wp-analytify.com/add-ons/">' . esc_html__( 'Add Ons', 'edd' ) . '</a>',
382 );
383
384 $input = array_merge( $input, $links );
385
386 return $input;
387 }
388
389
390 /**
391 * Display warning if profiles are not selected.
392 */
393 public function pa_check_warnings(){
394
395 add_action( 'admin_footer', array(
396 &$this,
397 'profile_warning'
398 ));
399 }
400
401 /**
402 * Get current screen details
403 */
404 public static function pa_page_file_path() {
405
406 $screen = get_current_screen();
407
408 if ( strpos( $screen->base, 'analytify-settings' ) !== false ) {
409 include( ANALYTIFY_ROOT_PATH . '/inc/analytify-settings.php' );
410 }
411 else if ( strpos( $screen->base, 'analytify-campaigns' ) !== false ) {
412 include_once( ANALYTIFY_ROOT_PATH . '/inc/analytify-campaigns.php' );
413 }
414 else {
415 include( ANALYTIFY_ROOT_PATH . '/inc/analytify-dashboard.php' );
416 }
417 }
418
419 /**
420 * Styling: loading stylesheets for the plugin.
421 */
422 public function pa_styles( $page ) {
423
424 wp_enqueue_style( 'wp-analytify-style', plugins_url('css/wp-analytify-style.css', __FILE__));
425
426 // wp_enqueue_style( 'ui-css', plugins_url('jquery-ui-theme/jquery-ui.css', __FILE__));
427 wp_enqueue_style( 'chosen', plugins_url('css/chosen.css', __FILE__));
428 // wp_enqueue_style( 'jquery-ui-tooltip-css', plugins_url('css/jquery.ui.tooltip.html.css', __FILE__) );
429
430 if ( get_option( 'show_tracking_pointer_1' ) != 1 ) wp_enqueue_style( 'wp-pointer' );
431 }
432
433 public function pa_front_styles( $page ) {
434
435 if( get_option( 'analytify_disable_front') == 0 ) {
436
437 wp_enqueue_style( 'front-end-style', plugins_url('css/frontend_styles.css', __FILE__),false,ANALYTIFY_VERSION);
438 }
439 }
440
441 /**
442 * Loading scripts js for the plugin.
443 */
444 public function pa_scripts( $page ) {
445
446 wp_enqueue_script ( 'jquery' );
447 wp_enqueue_script ( 'charts_api_js', 'https://www.google.com/jsapi', false, ANALYTIFY_VERSION );
448 wp_enqueue_script ( 'chosen-js', plugins_url('js/chosen.jquery.js', __FILE__), false, ANALYTIFY_VERSION );
449 wp_enqueue_script ( 'script-js', plugins_url('js/wp-analytify.js', __FILE__), false, ANALYTIFY_VERSION );
450 wp_enqueue_script ( 'jquery-ui-tooltip' );
451 wp_enqueue_script ( 'jquery-ui-datepicker');
452
453 if ( get_option( 'show_tracking_pointer_1' ) != 1 ) wp_enqueue_script( 'wp-pointer' );
454
455 }
456
457 /**
458 * Loading scripts js for the plugin.
459 */
460 public function pa_front_scripts( $page ) {
461
462 if( get_option( 'analytify_disable_front') == 0 ){
463
464 wp_enqueue_script ('jquery');
465 wp_enqueue_script ('analytify-classie', plugins_url('js/classie.js', __FILE__), false, ANALYTIFY_VERSION);
466 wp_enqueue_script ('analytify-selectfx', plugins_url('js/selectFx.js', __FILE__), false, ANALYTIFY_VERSION);
467 wp_enqueue_script ('analytify-script', plugins_url('js/script.js', __FILE__), false, ANALYTIFY_VERSION);
468
469 }
470 }
471
472 /**
473 * Create Analytics menu at the left side of dashboard
474 */
475 public static function wpa_add_menu() {
476
477 add_menu_page( ANALYTIFY_NICK, 'Analytify', 'manage_options', 'analytify-dashboard', array(
478 __CLASS__,
479 'pa_page_file_path'
480 ), plugins_url('images/wp-analytics-logo.png', __FILE__),'2.1.9');
481
482 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Dashboard', ' Dashboard', 'manage_options', 'analytify-dashboard', array(
483 __CLASS__,
484 'pa_page_file_path'
485 ));
486
487 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Campaign', ' Campaigns', 'manage_options', 'analytify-campaigns', array(
488 __CLASS__,
489 'pa_page_file_path'
490 ));
491
492 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Settings', '<b style="color:#f9845b">Settings</b>', 'manage_options', 'analytify-settings', array(
493 __CLASS__,
494 'pa_page_file_path'
495 ));
496 }
497
498 /**
499 * Creating tabs for settings
500 * @since 1.0
501 */
502
503 public function pa_settings_tabs( $current = 'authentication' ) {
504
505 $tabs = array(
506 'authentication' => 'Authentication',
507 'profile' => 'Profile',
508 'admin' => 'Admin',
509 'advanced' => 'Advanced'
510 );
511
512 echo '<div class="left-area">';
513 echo '<div id="icon-themes" class="icon32"><br></div>';
514 echo '<h2 class="nav-tab-wrapper">';
515
516 foreach( $tabs as $tab => $name ) {
517
518 $class = ( $tab == $current ) ? ' nav-tab-active' : '';
519 echo "<a class='nav-tab$class' href='?page=analytify-settings&tab=$tab'>$name</a>";
520 }
521
522 echo '</h2>';
523 }
524
525 /**
526 * Get profiles from user Google Analytics account profiles.
527 */
528 public function pt_get_analytics_accounts() {
529
530 try {
531
532 if( get_option( 'pa_google_token' ) !='' ) {
533 $profiles = $this->service->management_profiles->listManagementProfiles( "~all", "~all" );
534 return $profiles;
535 }
536
537 else{
538 echo '<br /><p class="description">' . __( 'You must authenticate to access your web profiles.', 'wp-analytify' ) . '</p>';
539 }
540
541 }
542
543 catch (Exception $e) {
544 die('An error occured: ' . $e->getMessage() . '\n');
545 }
546 }
547
548 public function pa_setting_url() {
549
550 return admin_url('admin.php?page=analytify-settings');
551
552 }
553
554
555 public function pt_save_data( $key_google_token ) {
556
557 update_option( 'post_analytics_token', $key_google_token );
558 $this->pa_connect();
559
560 return true;
561 }
562
563 /**
564 * Warning messages.
565 */
566 public function profile_warning() {
567
568 $profile_id = get_option( "pt_webprofile" );
569 $acces_token = get_option( "post_analytics_token" );
570
571 if (! isset( $acces_token ) || empty( $acces_token )) {
572
573 echo "<div id='message' class='error'><p><strong>" . __( "Analytify is not active. Please <a href='" . menu_page_url ( 'analytify-settings', false ) ."'>Authenticate</a> in order to get started using this plugin.", 'wp-analytify' )."</p></div>";
574 }
575 else{
576
577 if (! isset( $profile_id ) || empty( $profile_id )){
578 echo '<div class="error"><p><strong>' . __( 'Google Analytics Profile is not set. Set the <a href="' . menu_page_url ( 'analytify-settings', false ) . '&tab=profile">Profile</a> ', 'wp-analytify' ) . '</p></div>';
579 }
580 }
581 }
582
583
584 public function get_single_front_analytics( $content ) {
585
586 global $post, $wp_analytify;
587
588 $front_access = get_option( 'post_analytics_access' );
589
590 if ( is_single() || is_page() ) {
591
592 $post_type = get_post_type( $post->ID );
593
594 if( strlen( get_option( 'analytify_posts_stats_front' ) < 3) ) {
595 return $content;
596 }
597
598 if( is_array( get_option( 'analytify_posts_stats_front' )) and !in_array( $post_type, get_option( 'analytify_posts_stats_front' ) ) ) {
599
600 return $content;
601 }
602
603 if ( is_array( get_option( 'post_analytics_exclude_posts_front' ) ) ) {
604
605 if ( in_array( get_the_ID(), get_option( 'post_analytics_exclude_posts_front' ) ) ) {
606
607 return $content;
608 }
609 }
610
611 // Showing stats to guests
612 if ( $front_access[0] == 'every-one' || $this->pa_check_roles( $front_access )) {
613
614 $post_analytics_settings_front = array();
615 $post_analytics_settings_front = get_option( 'post_analytics_settings_front' );
616
617 $urlPost = parse_url( get_permalink( $post->ID ) );
618
619 if ( $urlPost['host'] == 'localhost' )
620 $filter = 'ga:pagePath==/'; //.$u_post['path'];
621 else
622 $filter = 'ga:pagePath==' .$urlPost['path']. '';
623
624 if( get_the_time('Y', $post->ID) < 2005 ) {
625
626 $start_date = '2005-01-01';
627 }
628 else {
629
630 $start_date = get_the_time('Y-m-d', $post->ID);
631 }
632
633 $end_date = date('Y-m-d');
634
635 ob_start();
636
637 include ANALYTIFY_ROOT_PATH . '/inc/front-menus.php';
638
639 if(! empty( $post_analytics_settings_front )){
640
641 if (is_array( $post_analytics_settings_front )){
642
643 $stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgTimeOnPage,ga:users',$start_date, $end_date, false, false, $filter);
644
645 if ( isset( $stats->totalsForAllResults ) ) {
646
647 include ANALYTIFY_ROOT_PATH . '/views/front/general-stats.php';
648 pa_include_general( $this, $stats);
649 }
650 }
651 }
652
653 $content .= ob_get_contents();
654 ob_get_clean();
655 }
656
657 }
658
659 return $content;
660
661 }
662
663 /*
664 * get the Analytics data from ajax() call
665 *
666 */
667
668 public function get_ajax_single_admin_analytics() {
669
670 $startDate = '';
671 $endDate = '';
672 $postID = 0 ;
673 $startDate = $_POST['start_date'];
674 $endDate = $_POST['end_date'];
675 $postID = $_POST['post_id'];
676 $this->get_single_admin_analytics( $startDate, $endDate, $postID, 1 );
677
678 die();
679 }
680
681 /*
682 * get the Analytics data for wp-admin posts/pages.
683 *
684 */
685 public function get_single_admin_analytics( $start_date = '', $end_date = '', $postID = 0, $ajax = 0 ) {
686
687 global $post;
688
689 //$urlPost = parse_url( get_permalink( $post->ID ) );
690
691 if ( $postID == 0 ) {
692 $u_post = '/'; //$urlPost['path'];
693 }
694 else{
695 $u_post = parse_url( get_permalink( $postID ) );
696 }
697
698 if ( $u_post['host'] == 'localhost' )
699 $filter = false;
700 else
701 $filter = 'ga:pagePath==' .$u_post['path']. '';
702
703
704 if ( $start_date == '' ) {
705
706 $s_date = get_the_time('Y-m-d', $post->ID);
707 if(get_the_time('Y', $post->ID) < 2005){
708 $s_date = '2005-01-01';
709 }
710 }
711 else{
712 $s_date = $start_date;
713 }
714
715 if ( $end_date == '' ) {
716 $e_date = date('Y-m-d');
717 }
718 else{
719 $e_date = $end_date;
720 }
721
722 $show_settings = array();
723 $show_settings = get_option('post_analytics_settings_back');
724
725 // Stop here, if user has disable backend analytics i.e OFF
726 if ( get_option( 'post_analytics_disable_back' ) == 1 and $ajax == 0) {
727 return;
728 }
729
730 echo '<p> Displaying Analytics of this page from ' . date("jS F, Y", strtotime($s_date)) . ' to '. date("jS F, Y", strtotime($e_date)) . '</p>';
731
732 if( !empty( $show_settings )){
733
734 if (is_array( $show_settings )){
735
736 if (in_array( 'show-overall-back', $show_settings )) {
737
738 $stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgTimeOnPage,ga:users',$s_date, $e_date, false, false, $filter);
739
740 if ( isset( $stats->totalsForAllResults ) ) {
741
742 include ANALYTIFY_ROOT_PATH . '/views/admin/single-general-stats.php';
743 wpa_single_include_general( $this, $stats);
744 }
745 }
746 }
747 }
748 }
749
750
751 /*
752 * Pretty numbers
753 */
754 function wpa_pretty_numbers( $num ) {
755
756 return round(($num/1000),2).'k';
757 }
758
759 /*
760 * format numbers
761 */
762 function wpa_number_format( $num ) {
763
764 return number_format($num);
765 }
766
767 /*
768 * Pretty time to display
769 */
770 function pa_pretty_time( $time ) {
771
772 // Check if numeric
773 if ( is_numeric($time) ) {
774
775 $value = array(
776 "years" => '00',
777 "days" => '00',
778 "hours" => '',
779 "minutes" => '',
780 "seconds" => ''
781 );
782
783 if ($time >= 31556926) {
784 $value["years"] = floor($time / 31556926);
785 $time = ($time % 31556926);
786 } //$time >= 31556926
787
788 if ($time >= 86400)
789 {
790 $value["days"] = floor($time / 86400);
791 $time = ($time % 86400);
792 } //$time >= 86400
793 if ($time >= 3600)
794 {
795 $value["hours"] = str_pad(floor($time / 3600), 1, 0, STR_PAD_LEFT);
796 $time = ($time % 3600);
797 } //$time >= 3600
798 if ($time >= 60)
799 {
800 $value["minutes"] = str_pad(floor($time / 60), 1, 0, STR_PAD_LEFT);
801 $time = ($time % 60);
802 } //$time >= 60
803 $value["seconds"] = str_pad(floor($time), 1, 0, STR_PAD_LEFT);
804 # Get the hour:minute:second version
805 if($value['hours']!=''){
806 $attach_hours='<sub>h</sub> ';
807 }
808 if($value['minutes']!=''){
809 $attach_min='<sub>min</sub> ';
810 }
811 if($value['seconds']!=''){
812 $attach_sec='<sub>sec</sub>';
813 }
814 return $value['hours'] .@$attach_hours. $value['minutes'] .@$attach_min. $value['seconds'].$attach_sec;
815 //return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds'];
816 } //is_numeric($time)
817 else
818 {
819 return false;
820 }
821 }
822
823
824 public function pa_check_roles( $access_level ) {
825
826 if ( is_user_logged_in () && isset ( $access_level ) ) {
827
828 global $current_user;
829 $roles = $current_user->roles;
830
831 /*if ( ( current_user_can ( 'manage_options' ) ) ) {
832
833 return true;
834 }*/
835
836 if ( in_array ( $roles[0], $access_level ) ) {
837
838 return true;
839 }
840 else {
841
842 return false;
843 }
844 }
845 }
846
847 /**
848 * Show pointers for announcements
849 */
850
851 public function pa_welcome_message() {
852
853 $pointer_content = '<h3>Announcement:</h3>';
854 $pointer_content .= '<p><input type="checkbox" name="wpa_allow_tracking" value="1" id="wpa_allow_tracking"> ';
855 $pointer_content .= 'Help us making Analytify even better by sharing very basic plugin usage data.';
856 $pointer_content .= ' Opt-in and receive a $5 Off coupon for <a href="https://wp-analytify.com/upgrade-from-free">Analytify PRO</a>.</p>';
857 ?>
858
859 <script type="text/javascript">
860 //<![CDATA[
861 jQuery(document).ready( function($) {
862
863 if(typeof(jQuery().pointer) != 'undefined') {
864
865 $('#toplevel_page_analytify-dashboard').pointer({
866
867 content: '<?php echo $pointer_content; ?>',
868 position: {
869 edge: 'left',
870 align: 'center'
871 },
872 close: function() {
873 $.post( ajaxurl, {
874 pointer: 'tracking',
875 wpa_allow: $('#wpa_allow_tracking:checked').val(),
876 action: 'analytify_dismiss_pointer'
877 });
878
879 if($('#wpa_allow_tracking:checked').val() == 1) alert('Thankyou!\nYour Coupon code is Analytify2015');
880 }
881 }).pointer('open');
882 };
883 });
884 //]]>
885 </script>
886
887 <?php
888 }
889
890 /* Display a notice that can be dismissed */
891
892 function analytify_admin_notice() {
893
894 $profile_id = get_option( "pt_webprofile" );
895 $acces_token = get_option( "post_analytics_token" );
896
897 if ( current_user_can( 'install_plugins' )) {
898
899 global $current_user ;
900 $user_id = $current_user->ID;
901 /* Check that the user hasn't already clicked to ignore the message */
902 if ( ! get_user_meta($user_id, 'analytify_ignore_notice110') ) {
903
904 echo '<div class="updated"><p>';
905 printf(__('<b>Notice:</b> Enhanced eCommerce Tracking for <b><a href="https://wp-analytify.com/go/WooCommerce" target="_blank">WooCommerce</a></b> is finally available with Analytify! Increase sales and track - Product Clicks, Adds to cart, Checkouts and <a href="https://wp-analytify.com/go/WooCommerce-Announcement-blog" target="_blank">many more</a>. <a href="%1$s">[Hide Notice]</a>'), admin_url( 'admin.php?page=analytify-dashboard&analytify_nag_ignore=0' ));
906 echo "</p></div>";
907
908 }
909 }
910
911 /* Show notices */
912 if (! isset( $acces_token ) || empty( $acces_token ) || ! get_option( 'pa_google_token' ) ) {
913
914 echo "<div class='error'><p><b>Notice:</b> " . __( "<b><a style=\"text-decoration:none\" href='" . menu_page_url ( 'analytify-settings', false ) ."'>Connect</a></b> Analytify with your Google account.", 'wp-analytify' )."</p></div>";
915 }
916 else{
917
918 if ((! isset( $profile_id ) || empty( $profile_id )) && ! isset($_POST[ 'save_profile' ]) ) {
919
920 echo '<div class="error"><p>' . __( 'Congratulations! Analytify is now authenticated. Select your website profile <a href="' . menu_page_url ( 'analytify-settings', false ) . '&tab=profile">here</a> ', 'wp-analytify' ) . '</p></div>';
921 }
922 }
923 }
924
925 function analytify_nag_ignore() {
926 global $current_user;
927
928 $user_id = $current_user->ID;
929 /* If user clicks to ignore the notice, add that to their user meta */
930 if ( isset($_GET['analytify_nag_ignore']) && '0' == $_GET['analytify_nag_ignore'] ) {
931 add_user_meta($user_id, 'analytify_ignore_notice110', 'true', true);
932 }
933 }
934
935
936
937 }
938
939 $wp_analytify = new WP_Analytify();
940
941 } //end if
942
943 //update_option('show_tracking_pointer_1', 0);
944
945 register_activation_hook( __FILE__, 'install' );
946 register_deactivation_hook( __FILE__, 'uninstall' );
947 register_uninstall_hook( __FILE__, 'delete' );
948
949 /*
950 * Activate options by default on installing the plugin.
951 */
952 function install() {
953
954 update_option( 'analytify_posts_stats', array( 'post','page' ));
955 update_option( 'post_analytics_disable_back' , 1 );
956 update_option( 'analytify_code' , 1 );
957 update_option( 'post_analytics_settings_back' , array( 'show-overall-back' ) );
958 update_option( 'post_analytics_access_back' , array( 'editor','administrator' ) );
959 update_option( 'display_tracking_code' , array( 'administrator' ) );
960 //send_status_analytify( get_option( 'admin_email' ), 'active');
961 }
962
963 function uninstall() {
964
965 if(get_option('wpa_allow_tracking') == 1) send_status_analytify( get_option( 'admin_email' ), 'in-active');
966
967 delete_option( 'analytify_posts_stats' );
968 delete_option( 'pa_google_token' );
969 delete_option( 'show_tracking_pointer_1' );
970 delete_option( 'post_analytics_token' );
971
972 }
973
974 function delete() {
975
976 if(get_option('wpa_allow_tracking') == 1) send_status_analytify( get_option( 'admin_email' ), 'delete');
977 }
978
979 /**
980 * @param $email
981 * @param $status
982 */
983
984 function send_status_analytify( $email, $status ) {
985
986 $url = "https://wp-analytify.com/plugin-manager/";
987
988 if ( $email == "" ) {
989 $email = "track@wp-analytify.com";
990 }
991
992 $fields = array(
993 "email" => $email ,
994 "site" => get_site_url(),
995 "status" => $status,
996 "type" => 'FREE'
997 );
998
999 wp_remote_post( $url, array(
1000 "method" => "POST",
1001 "timeout" => 5,
1002 "httpversion" => "1.0",
1003 "blocking" => false,
1004 "headers" => array(),
1005 "body" => $fields
1006 )
1007 );
1008 }
1009 ?>