PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.2.1
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.2.1
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.1, at wp-analytify.php

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