-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanalytify-dashboard-addon.php
More file actions
252 lines (194 loc) · 10.4 KB
/
Copy pathanalytify-dashboard-addon.php
File metadata and controls
252 lines (194 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
if ( ! class_exists( 'Analytify_Dashboard_Addon' ) ) {
class Analytify_Dashboard_Addon {
public function __construct() {
if ( ! $this->is_access() ) { return; }
add_action( 'wp_dashboard_setup', array( $this, 'add_analytify_widget' ) );
if ( $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'profile_for_dashboard', 'wp-analytify-profile', '' ) != '' ) {
add_action( 'admin_enqueue_scripts', array( $this, 'pa_dashboard_script' ) );
add_action( 'wp_ajax_analytify_dashboard_addon', array( $this, 'analytify_general_stats' ) );
}
}
function pa_dashboard_script() {
wp_enqueue_script( 'analytify-dashboard-addon', plugins_url( '/assets/js/wp-analytify-dashboard.js', __FILE__ ), false, ANALYTIFY_DASHBOARD_VERSION );
wp_localize_script( 'analytify-dashboard-addon', 'analytify_dashboard_widget', array(
'get_stats_nonce' => wp_create_nonce( 'analytify-dashboard-widget-get-stats' )
) );
}
public function add_analytify_widget() {
wp_add_dashboard_widget( 'analytify-dashboard-addon', __( 'Google Analytics Dashboard By Analytify', 'analytify-analytics-dashboard-widget' ), array( $this, 'wpa_general_dashboard_area' ), null , null );
}
/**
* Create Widget Container.
*
* @since 1.0.0
*/
public function wpa_general_dashboard_area( $var, $dashboard_id ) {
$acces_token = get_option( 'post_analytics_token' );
if ( isset( $acces_token ) && ! empty( $acces_token ) && get_option( 'pa_google_token' ) ) {
$previous_date = get_option( 'analytify_dashboard_widget_date' );
// if previous date is stored.
if ( isset( $previous_date[0] ) && isset( $previous_date[1] ) ) {
$s_date = $previous_date[0];
$ed_date = $previous_date[1];
} else {
$start_date_val = strtotime( '- 7 days' );
$end_date_val = strtotime( 'now' );
$s_date = date( 'Y-m-d', $start_date_val );
$ed_date = date( 'Y-m-d', $end_date_val );
}
if ( $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'profile_for_dashboard', 'wp-analytify-profile', '' ) != '' ) {
?>
<div class="analytify_wraper">
<form id="analytify_dashboard" name="analytify_dashboard" method="POST" class="analytify-widget-form">
<div class="analytify_main_setting_bar">
<div class="analytify_pull_right analytify_setting">
<div class="analytify_select_date">
<form class="analytify_form_date" action="" method="post">
<div class="analytify_select_date_fields">
<input type="hidden" name="st_date" id="analytify_start_val">
<input type="hidden" name="ed_date" id="analytify_end_val">
<label for="analytify_start"><?php analytify_e( 'From:', 'wp-analytify' ) ?></label>
<input type="text" id="analytify_start" value="<?php echo isset( $s_date ) ? $s_date :
'' ?>">
<label for="analytify_end"><?php analytify_e( 'To:', 'wp-analytify' ) ?></label>
<input type="text" id="analytify_end" value="<?php echo isset( $ed_date ) ? $ed_date :
'' ?>">
<div class="analytify_arrow_date_picker"></div>
</div>
<input type="submit" value="<?php _e( 'View Stats', 'analytify-analytics-dashboard-widget' ) ?>" name="view_data" class="analytify_submit_date_btn">
<select id="analytify_dashboard_stats_type">
<option value="general-statistics"><?php analytify_e( 'General Statistics', 'wp-analytify' ) ?></option>
<option value="top-pages-by-views"><?php _e( 'Top pages', 'analytify-analytics-dashboard-widget' ) ?></option>
<option value="top-countries"><?php _e( 'Top Countries', 'analytify-analytics-dashboard-widget' ) ?></option>
<option value="top-cities"><?php _e( 'Top Cities', 'analytify-analytics-dashboard-widget' ) ?></option>
<option value="keywords"><?php _e( 'Keywords', 'analytify-analytics-dashboard-widget' ) ?></option>
<option value="social-media"><?php analytify_e( 'Social Media', 'wp-analytify' ) ?></option>
<option value="top-reffers"><?php analytify_e( 'Top Referrers', 'wp-analytify' ) ?></option>
</select>
<?php echo WPANALYTIFY_Utils::get_date_list() ?>
</form>
</div>
</div>
</div>
</form>
</div>
<?php
} else {
echo __( 'Select the Profile', 'analytify-analytics-dashboard-widget' );
}
} else {
echo __( 'Connect your Google account with Analytify', 'analytify-analytics-dashboard-widget' );
}
}
/**
* Runs on Every Ajax.
*
* @since 1.0.0
*/
public static function analytify_general_stats() {
check_ajax_referer( 'analytify-dashboard-widget-get-stats', 'nonce' );
$start_date_val = strtotime( '- 7 days' );
$end_date_val = strtotime( 'now' );
$start_date = isset( $_POST['startDate'] ) ? $_POST['startDate'] : date( 'Y-m-d', $start_date_val );
$end_date = isset( $_POST['endDate'] ) ? $_POST['endDate'] : date( 'Y-m-d', $end_date_val );
$stats_type = isset( $_POST['stats_type'] ) ? $_POST['stats_type'] : 'general-statistics';
$wp_analytify = $GLOBALS['WP_ANALYTIFY'];
update_option( 'analytify_dashboard_widget_date', array( $start_date, $end_date ), false );
$acces_token = get_option( 'post_analytics_token' );
if ( $acces_token ) {
?>
<div class="analytify_wraper">
<div id="inner_analytify_dashboard">
<?php
$_analytify_profile = get_option( 'wp-analytify-profile' );
$dashboard_profile_id = $_analytify_profile['profile_for_dashboard'];
if ( 'general-statistics' === $stats_type ) {
$stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgTimeOnPage,ga:users', $start_date, $end_date, false, false, false, false, 'widget-show-overall-dashboard' );
// New vs Returning Users
$new_returning_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date, 'ga:userType', false, false, false, 'widget-show-default-new-returning-dashboard' );
if ( $stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/general-stats.php';
pa_include_general( $wp_analytify, $stats, $new_returning_stats );
}
} else if( 'top-pages-by-views' === $stats_type ) {
$top_page_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:pageviews', $start_date, $end_date, 'ga:PageTitle', '-ga:pageviews', 'ga:pageTitle!=(not set)', 50, 'widget-show-top-pages-dashboard' );
if ( $top_page_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-pages-stats.php';
pa_include_top_pages_stats( $wp_analytify, $top_page_stats );
}
} else if( 'top-countries' === $stats_type ){
$top_countries_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date , 'ga:country' , '-ga:sessions' , 'ga:country!=(not set)', 50, 'widget-show-top-countries-dashboard' );
if ( $top_countries_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-countries-stats.php';
pa_include_countries_pages_stats( $wp_analytify, $top_countries_stats );
}
} else if( 'top-cities' === $stats_type ){
$top_cities_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date , 'ga:city' , '-ga:sessions' , 'ga:city!=(not set)', 50, 'widget-show-top-cities-dashboard' );
if ( $top_cities_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-cities-stats.php';
pa_include_cities_stats( $wp_analytify, $top_cities_stats );
}
} else if( 'keywords'=== $stats_type ) {
$top_keywords_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date, 'ga:keyword', '-ga:sessions', false, 50, 'widget-show-top-keywords-dashboard' );
if ( $top_keywords_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-keywords-stats.php';
pa_include_keywords_stats( $wp_analytify, $top_keywords_stats );
}
} else if( 'social-media' === $stats_type ) {
$top_socialmedia_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date, 'ga:socialNetwork', '-ga:sessions', 'ga:socialNetwork!=(not set)' , 50, 'widget-show-top-socialmedia-dashboard' );
if ( $top_socialmedia_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-socialmedia-stats.php';
pa_include_socialmedia_stats( $wp_analytify, $top_socialmedia_stats );
}
} else if( 'top-reffers' === $stats_type ) {
$top_reffers_stats = $wp_analytify->pa_get_analytics_dashboard( 'ga:sessions', $start_date, $end_date, 'ga:source,ga:medium', '-ga:sessions', false, 50, 'widget-show-top-reffers-dashboard' );
if ( $top_reffers_stats ) {
include ANALYTIFY_DASHBOARD_ROOT_PATH . '/views/admin/top-reffers-stats.php';
pa_include_reffers_stats( $wp_analytify, $top_reffers_stats );
}
}
?>
</div></div>
<?php
}
wp_die();
}
/**
* Check is user have access to check deshboard.
* @return boolean
*
* @since 1.0.5
*/
function is_access() {
$is_access_level = $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'show_analytics_roles_dashboard','wp-analytify-dashboard', array( 'administrator' ) );
return $GLOBALS['WP_ANALYTIFY']->pa_check_roles( $is_access_level );
}
} // End of class.
}
/**
* Helper function for translation.
*/
if ( ! function_exists( 'analytify__' ) ) {
/**
* Wrapper for __() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: wp-analytify
* @return void
*/
function analytify__( $string, $textdomain = 'wp-analytify' ) {
return __( $string, $textdomain );
}
}
if ( ! function_exists( 'analytify_e' ) ) {
/**
* Wrapper for _e() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: wp-analytify
* @return void
*/
function analytify_e( $string, $textdomain = 'wp-analytify' ) {
echo __( $string, $textdomain );
}
}
?>