-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleanalytics.php
executable file
·882 lines (232 loc) · 12 KB
/
googleanalytics.php
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/*
Plugin Name: Google Universal Analytics
Plugin URI: http://wordpress.org/extend/plugins/google-universal-analytics/
Description: Adds <a href="http://www.google.com/analytics/">Google Analytics</a> tracking code on all pages.
Version: 2.4.2
Author: Audrius Dobilinskas
Author URI: http://onlineads.lt/
*/
if (!defined('WP_CONTENT_URL'))
define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
if (!defined('WP_CONTENT_DIR'))
define('WP_CONTENT_DIR', ABSPATH.'wp-content');
if (!defined('WP_PLUGIN_URL'))
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
if (!defined('WP_PLUGIN_DIR'))
define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
add_action( 'plugins_loaded', 'myplugin_load_textdomain' );
/**
* Load plugin textdomain.
*
* @since 1.0.0
*/
function myplugin_load_textdomain() {
load_plugin_textdomain( 'gua', false, dirname( plugin_basename( __FILE__ ) ) . '/langs' );
}
register_deactivation_hook(__FILE__, 'deactive_google_universal_analytics');
register_activation_hook( __FILE__, 'activate_google_universal_analytics' );
add_action( 'admin_init', 'register_plugin_settings' );
add_action( 'admin_init', 'register_plugin_settings1' );
add_action( 'admin_init', 'register_plugin_settings2' );
function register_plugin_settings(){
register_setting('google-universal-settings','plugin_switch');
register_setting('google-universal-settings','web_property_id');
register_setting('google-universal-settings','in_footer');
register_setting('google-universal-settings','track_links');
register_setting('google-universal-settings','enable_display');
register_setting('google-universal-settings','anonymize_ip');
register_setting('google-universal-settings','enhancedlink_u');
register_setting('google-universal-settings','set_domain');
register_setting('google-universal-settings','set_domain_domain');
register_setting('google-universal-settings','tracking_off_for_this_role');
register_setting('google-universal-settings','tracking_off_for_role');
}
function register_plugin_settings1(){
//classic_options
register_setting('google-universal-settings-classic','classic_property_id');
register_setting('google-universal-settings-classic','classic_in_footer');
register_setting('google-universal-settings-classic','classic_plugin_switch');
register_setting('google-universal-settings-classic','classic_tracking_off_for_role');
register_setting('google-universal-settings-classic','classic_tracking_off_for_this_role');
}
function register_plugin_settings2(){
//custom_options
register_setting('google-universal-settings-custom','custom_in_footer');
register_setting('google-universal-settings-custom','custom_plugin_switch');
register_setting('google-universal-settings-custom','custom_web_property_id');
register_setting('google-universal-settings-custom','custom_tracking_off_for_role');
register_setting('google-universal-settings-custom','custom_tracking_off_for_this_role');
}
function activate_google_universal_analytics(){
}
function deactive_google_universal_analytics() {
delete_option('web_property_id');
delete_option('in_footer');
delete_option('plugin_switch');
delete_option('track_links');
delete_option('enable_display');
delete_option('anonymize_ip');
delete_option('enhancedlink_u');
delete_option('set_domain');
delete_option('set_domain_domain');
delete_option('tracking_off_for_this_role');
delete_option('tracking_off_for_role');
//classic_options
delete_option('classic_property_id');
delete_option('classic_in_footer');
delete_option('classic_plugin_switch');
delete_option('classic_tracking_off_for_role');
delete_option('classic_tracking_off_for_this_role');
//custom_options
delete_option('custom_in_footer');
delete_option('custom_plugin_switch');
delete_option('custom_web_property_id');
delete_option('custom_tracking_off_for_role');
delete_option('custom_tracking_off_for_this_role');
}
function admin_menu_google_universal_analytics() {
global $settings_page, $settings_page1, $classic_page, $cutom_page;
$settings_page = add_menu_page( __('Google Universal Analytics', 'gua'), __('Google Universal Analytics', 'gua'), 'manage_options', 'google_universal_analytics', 'options_page_google_universal_analytics' );
add_submenu_page('google_universal_analytics','','','manage_options','google_universal_analytics','options_page_google_universal_analytics');
$settings_page1 = add_submenu_page( 'google_universal_analytics', __('Universal Analytics', 'gua'), __('Universal Analytics', 'gua'), 'manage_options', 'google_universal_analytics', 'options_page_google_universal_analytics' );
$classic_page = add_submenu_page( 'google_universal_analytics', __('Classic Analytics', 'gua'), __('Classic Analytics', 'gua'), 'manage_options', 'classic_analytics', 'classic_analytics_page_google_universal_analytics' );
$cutom_page = add_submenu_page( 'google_universal_analytics', __('Custom Tracker', 'gua'), __('Custom Tracker', 'gua'), 'manage_options', 'custom_analytics', 'custom_analytics_page_google_universal_analytics' );
}
function options_page_google_universal_analytics() {
include(WP_PLUGIN_DIR.'/google-universal-analytics/options.php');
}
function classic_analytics_page_google_universal_analytics() {
include(WP_PLUGIN_DIR.'/google-universal-analytics/classic/classic-analytics.php');
}
function custom_analytics_page_google_universal_analytics() {
include(WP_PLUGIN_DIR.'/google-universal-analytics/classic/custom-analytics.php');
}
function google_universal_analytics() {
require 'tracking-code.php';
}
function google_classic_analytics() {
$classic_property_id = get_option('classic_property_id');
$classic_analytics_code = "<script type='text/javascript'>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '$classic_property_id']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>";
echo $classic_analytics_code;
}
function google_custom_analytics() {
echo get_option('custom_web_property_id');
}
function google_universal_analytics_scripts($hook){
global $settings_page, $settings_page1, $classic_page, $cutom_page;
if($hook != $settings_page && $hook != $settings_page1 && $hook != $classic_page && $hook != $cutom_page )
return;
//register styles
wp_register_style( 'bootstrap-css', plugins_url( 'google-universal-analytics/bootstrap/css/bootstrap.min.css' , dirname(__FILE__) ) );
wp_register_style( 'bootstrap-switch-css', plugins_url( 'google-universal-analytics/bootstrap/css/bootstrap-switch.min.css' , dirname(__FILE__) ) );
wp_register_style( 'main-css', plugins_url( 'google-universal-analytics/assets/gua-main.css' , dirname(__FILE__) ) );
//register scripts
wp_register_script( 'google-js', '//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js', array(), '', true );
wp_register_script( 'bootstrap-js', plugins_url( 'google-universal-analytics/bootstrap/js/bootstrap.min.js' , dirname(__FILE__) ), array('google-js'), '', true );
wp_register_script( 'bootstrap-switch-js', plugins_url( 'google-universal-analytics/bootstrap/js/bootstrap-switch.min.js' , dirname(__FILE__) ) , array('bootstrap-js'),'',true );
wp_register_script( 'main-js', plugins_url( 'google-universal-analytics/assets/gua-main.js' , dirname(__FILE__) ) , array('google-js'),'',true );
//enqueue styles
wp_enqueue_style( 'bootstrap-css' );
wp_enqueue_style( 'bootstrap-switch-css' );
wp_enqueue_style( 'main-css' );
//enqueue scripts
wp_enqueue_script( 'google-js' );
wp_enqueue_script( 'bootstrap-js' );
wp_enqueue_script( 'bootstrap-switch-js' );
wp_enqueue_script( 'main-js' );
}
if (is_admin()) {
add_action('admin_enqueue_scripts', 'google_universal_analytics_scripts');
add_action('admin_menu', 'admin_menu_google_universal_analytics');
}
add_action('init', 'display_google_universal_analytics_code');
function display_google_universal_analytics_code(){
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if(get_option('tracking_off_for_role')=='on' && strcasecmp($user_role , get_option('tracking_off_for_this_role'))!=0){
if (!is_admin() && get_option('plugin_switch')=='on') {
if(get_option('in_footer')=='on'){
add_action('wp_footer', 'google_universal_analytics');
}else{
add_action('wp_head', 'google_universal_analytics');
}
}
}elseif(is_user_logged_in() && get_option('tracking_off_for_role')!='on'){
if (!is_admin() && get_option('plugin_switch')=='on') {
if(get_option('in_footer')=='on'){
add_action('wp_footer', 'google_universal_analytics');
}else{
add_action('wp_head', 'google_universal_analytics');
}
}
}elseif(!is_user_logged_in()){
if (!is_admin() && get_option('plugin_switch')=='on') {
if(get_option('in_footer')=='on'){
add_action('wp_footer', 'google_universal_analytics');
}else{
add_action('wp_head', 'google_universal_analytics');
}
}
}
if(get_option('custom_tracking_off_for_role')=='on' && strcasecmp($user_role , get_option('custom_tracking_off_for_this_role'))!=0){
if (!is_admin() && get_option('custom_plugin_switch')=='on') {
if(get_option('custom_in_footer')=='on'){
add_action('wp_footer', 'google_custom_analytics');
}else{
add_action('wp_head', 'google_custom_analytics');
}
}
}elseif(is_user_logged_in() && get_option('custom_tracking_off_for_role')!='on'){
if (!is_admin() && get_option('custom_plugin_switch')=='on') {
if(get_option('custom_in_footer')=='on'){
add_action('wp_footer', 'google_custom_analytics');
}else{
add_action('wp_head', 'google_custom_analytics');
}
}
}elseif(!is_user_logged_in()){
if (!is_admin() && get_option('custom_plugin_switch')=='on') {
if(get_option('custom_in_footer')=='on'){
add_action('wp_footer', 'google_custom_analytics');
}else{
add_action('wp_head', 'google_custom_analytics');
}
}
}
if(get_option('classic_tracking_off_for_role')=='on' && strcasecmp($user_role , get_option('classic_tracking_off_for_this_role'))!=0){
if (!is_admin() && get_option('classic_plugin_switch')=='on') {
if(get_option('classic_in_footer')=='on'){
add_action('wp_footer', 'google_classic_analytics');
}else{
add_action('wp_head', 'google_classic_analytics');
}
}
}elseif(is_user_logged_in() && get_option('classic_tracking_off_for_role')!='on'){
if (!is_admin() && get_option('classic_plugin_switch')=='on') {
if(get_option('classic_in_footer')=='on'){
add_action('wp_footer', 'google_classic_analytics');
}else{
add_action('wp_head', 'google_classic_analytics');
}
}
}elseif(!is_user_logged_in()){
if (!is_admin() && get_option('classic_plugin_switch')=='on') {
if(get_option('classic_in_footer')=='on'){
add_action('wp_footer', 'google_classic_analytics');
}else{
add_action('wp_head', 'google_classic_analytics');
}
}
}
}