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

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