forked from ArnaudHocevar/ZPGoogleAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_analytics.php
137 lines (122 loc) · 4.93 KB
/
google_analytics.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
<?php
/**
* google_analytics -- Places the tracking registation code for google analytics in a photo gallery.
* This code was modeled after the google_maps plugin by Dustin Brewer (mankind) and Stephen Billard (sbillard)
*
* @author Jeff Smith (j916) up to 2.5.0 - http://www.moto-treks.com/zenPhoto/google-analytics-for-zenphoto.html
* @author Arnaud Hocevar starting 3.0.0
* @version 4.0
* @package plugins
*/
$plugin_is_filter = 5 | THEME_PLUGIN | ADMIN_PLUGIN;
$plugin_description = gettext("Support for providing Google Analytics tracking");
$plugin_author = 'Arnaud Hocevar (original plugin from Jeff Smith)';
$plugin_version = '4.0';
$plugin_URL = "http://github.com/ArnaudHocevar/ZPGoogleAnalytics";
$option_interface = "GoogleAnalytics";
// Include all the dependencies
require_once(SERVERPATH . '/' . USER_PLUGIN_FOLDER . '/google_analytics/inc-functions.php');
require_once(SERVERPATH . '/' . USER_PLUGIN_FOLDER . '/google_analytics/inc-config.php');
// Register callbacks
zp_register_filter(getOption(GAConfig::getConfItem('TrackPageViewsPosition','property_name')), 'GoogleAnalytics::GAinitialize',0);
zp_register_filter(getOption(GAConfig::getConfItem('TrackImageViewsPosition','property_name')), 'GoogleAnalytics::GAColorboxHook',0);
class GoogleAnalytics {
// Constructor: set all the option defaults
public function GoogleAnalytics() {
//$conflist = GAConfig::getConf();
foreach (GAConfig::getConf() as $sub) {
setOptionDefault($sub['property_name'],$sub['default']);
}
}
// Set up configuration options in administration panel
public function getOptionsSupported() {
$t_arr = array();
foreach (GAConfig::getConf() as $sub) {
$t_sarr = array(
'order' => $sub['option_order'],
'key' => $sub['property_name'],
'type' => $sub['option_type'],
'desc' => $sub['option_desc']
);
if($sub['option_type'] == OPTION_TYPE_SELECTOR)
$t_sarr['selections'] = $sub['option_selections'];
if($sub['option_type'] == OPTION_TYPE_RADIO)
$t_sarr['buttons'] = $sub['option_button'];
$t_arr[$sub['option_header']] = $t_sarr;
}
return $t_arr;
}
function handleOption($option, $currentValue) {}
public static function GAinitialize() {
// Only enable analytics if a valid UA identifier is available
if(GAToolbox::validateAnalyticsId(getOption(GAConfig::getConfItem('AnalyticsId','property_name')))
&& ((zp_loggedin() && getOption('adminTracking'))
|| !zp_loggedin()) ) {
// Analytics JS header
echo "<script type=\"text/javascript\">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n";
// Analytics set up script
self::printGAOptions();
echo "</script>\n";
}
}
public static function GAColorboxHook() {
// Only enable analytics if a valid UA identifier is available AND we want to track image view
if(GAToolbox::validateAnalyticsId(getOption(GAConfig::getConfItem('AnalyticsId','property_name')))
&& ((zp_loggedin() && getOption('adminTracking'))
|| !zp_loggedin())
&& getOption(GAConfig::getConfItem('TrackImageViews','property_name')) == 1) {
echo "<script type=\"text/javascript\">
$(document).ready(function () {
$(document).bind('cbox_complete', function(){
var href = this.href;
if (href) {
ga('send', 'pageview', href]);
}
});
});
</script>\n";
}
}
private static function printGAOptions() {
$analyticUserId = getOption(GAConfig::getConfItem('AnalyticsId','property_name'));
$domainListParam = getOption('domainName');
if (!empty($analyticUserId) &&
((zp_loggedin() && getOption('admintracking'))
|| !zp_loggedin())) {
/* Initialisation of tracking code */
echo " ga('create', '" . $analyticUserId . "', {" . GAToolbox::buildCreateParams() ."});\n";
/* Additional options */
if(getOption('trackDemographics') == 1) {
echo " ga('require', 'displayfeatures');\n";
}
if(getOption('enhancedLink') == 1) {
echo " ga('require', 'linkid', 'linkid.js');\n";
}
if(!empty($domainListParam)) {
echo " ga('require', 'linker');\n";
$tok = strtok($domainListParam, " ,");
$domainList = "";
while ($tok !== false) {
if(!empty($tok))
$domainList = $domainList . "'" . $tok . "', ";
$tok = strtok(" ,");
}
echo " ga('linker:autoLink', [" . $domainList . "], false, true);\n";
}
if(getOption('anonymizeIp') == 1) {
echo " ga('set', 'anonymizeIp', true);\n";
}
if(getOption('forceSSL') == 1) {
echo " ga('set', 'forceSSL', true);\n";
}
if(getOption('trackPageViews') == 1) {
echo " ga('send', 'pageview');\n";
}
}
}
}
?>