-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgoogle_analytics.php
More file actions
103 lines (89 loc) · 3.38 KB
/
Copy pathgoogle_analytics.php
File metadata and controls
103 lines (89 loc) · 3.38 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
<?php
/**
* google_analytics
*
* Bind google analytics script
*
* @version 1.4 - 26. 1. 2012
* @author Roland 'rosali' Liebl
* @modified_by Ondra 'Kepi' Kudlík
* @website http://github.com/igloonet/roundcube_google_analytics
* @licence GNU GPL
*
* ChangeLog
*
* 1.4 - avoid fstats with config file and support including
* instead of excluding
* 1.1 - added support for async mode and empty tracker domain
*
**/
class google_analytics extends rcube_plugin
{
function init()
{
$this->add_hook('render_page', array($this, 'add_script'));
}
function add_script($args)
{
$rcmail = rcmail::get_instance();
// test if we have global_config plugin
if ( !in_array('global_config', $plugins = $rcmail->config->get('plugins')) ) {
$this->load_config('config/config.inc.php');
}
// do not allow logged users if privacy on
if(!empty($_SESSION['user_id']) && $rcmail->config->get('google_analytics_privacy', FALSE))
return $args;
// excluding or including
if ( $rcmail->config->get('google_analytics_excluding', TRUE) ) {
if ( in_array($args['template'], $rcmail->config->get('google_analytics_exclude', array())) )
return $args;
}
else {
if ( !in_array($args['template'], $rcmail->config->get('google_analytics_include', array('login'))) )
return $args;
}
$google_analytics_domain = $rcmail->config->get('google_analytics_domain', '');
$set_domain_name = '';
// async mode
if ( $rcmail->config->get('google_analytics_async')){
// set domain if not empty
if ( !Empty($google_analytics_domain) )
$set_domain_name = " _gaq.push(['_setDomainName', '" . $rcmail->config->get('google_analytics_domain') . "']);";
$script = "<script type=\"text/javascript\">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '" . $rcmail->config->get('google_analytics_id') . "']);
" . $set_domain_name . "
_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>
";
// async mode add script to head of page
$rcmail->output->add_header($script);
// sync mode
}
else {
// set domain if not empty
if (!Empty($google_analytics_domain))
$set_domain_name = 'pageTracker._setDomainName("' . $rcmail->config->get('google_analytics_domain') . '");';
$script = '<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("' . $rcmail->config->get('google_analytics_id') . '");
' . $set_domain_name . '
pageTracker._trackPageview();
} catch(err) {}</script>
';
// sync mode add script to end of page
$rcmail->output->add_footer($script);
}
return $args;
}
}
?>