-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGoogleAnalytics.php
executable file
·231 lines (155 loc) · 7.28 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
<?php
// Get correct id for plugin
$thisfile = basename(__FILE__, '.php');
// Data save file
GLOBAL $filupn_ga_data_file;
$filupn_ga_data_file = GSDATAOTHERPATH . 'GoogleAnalyticsSettings.xml';
// Register plugin
register_plugin(
$thisfile,
'Google Analytics',
'0.2',
'Philip Newcomer',
'http://philipnewcomer.net',
'Adds Google Analytics tracking code to your GetSimple site, without tracking admins',
'plugins',
'filupn_ga_admin_process'
);
add_action( 'theme-header', 'filupn_ga_add_code' );
add_action( 'plugins-sidebar', 'createSideMenu', array( $thisfile, 'Google Analytics Settings' ) );
// add_action( 'content-top', 'filupn_ga_cookie_debug' ); // For debugging only
add_action( 'logout', 'filupn_ga_logout' );
define( 'FILUPN_GA_COOKIE_LIFE', 60*60*24*365 ); // One year
$filupn_ga_settings = filupn_ga_read_settings();
// When in backend, renew cookie at every pageload
if( filupn_ga_is_backend() ) {
filupn_ga_setcookie();
}
function filupn_ga_setcookie() {
global $filupn_ga_settings;
global $thisfile;
$persistant = false;
if( $filupn_ga_settings['remember_when_not_logged_in'] == 'on' ) { $persistant = true; }
if( isset( $_GET['id'] ) and $_GET['id'] == $thisfile and isset( $_POST['submit'] ) ) { //
// When saving settings, set the new cookie life immediately or new settings will not take effect until the next admin page load.
if( isset( $_POST['remember_when_not_logged_in'] ) ) {
$persistant = true;
} else {
$persistant = false;
}
}
if( $persistant == true ) {
$cookie_time = time() + FILUPN_GA_COOKIE_LIFE;
} else {
$cookie_time = 0;
}
setcookie( 'GOOGLE_ANALYTICS_DISABLED', '1', $cookie_time, $filupn_ga_settings['site_root'] );
}
function filupn_ga_deletecookie() {
global $filupn_ga_settings;
setcookie( 'GOOGLE_ANALYTICS_DISABLED', false, time()-3600, $filupn_ga_settings['site_root'] );
}
function filupn_ga_add_code() {
global $filupn_ga_settings;
echo "\n<!-- Google Analytics plugin -->\n";
if( !isset( $_COOKIE['GOOGLE_ANALYTICS_DISABLED'] ) or ( isset( $_COOKIE['GOOGLE_ANALYTICS_DISABLED'] ) and $_COOKIE['GOOGLE_ANALYTICS_DISABLED'] != '1' ) ) {
if( strlen( $filupn_ga_settings['property_id'] ) > 0 ) {
?>
<!-- begin tracking code -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?php echo $filupn_ga_settings['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>
<!-- end Google Analytics tracking code -->
<?php
} else {
// If property_id is empty
echo '<!-- Tracking disabled; Enter your Google Analytics property ID in Plugins->Google Analytics Settings to enable tracking. -->' . "\n";
}
} else {
// If the admin cookie is detected
echo "<!-- Admin cookie detected; Google Analytics tracking disabled. -->\n";
}
echo "\n";
}
function filupn_ga_cookie_debug() {
echo '<pre>'; print_r($_COOKIE); echo '</pre>';
}
function filupn_ga_is_backend() {
$path_parts = pathinfo( $_SERVER['PHP_SELF'] );
if( function_exists( 'get_site_url' ) or $path_parts['basename'] == 'index.php' or $path_parts['basename'] == 'logout.php' ) {
return false;
} else {
return true;
}
}
function filupn_ga_admin_process() {
global $filupn_ga_data_file;
// Check for submitted data
if( isset( $_POST['submit'] ) ) {
// Save submitted data
$filupn_ga_submitted_data['property_id'] = $_POST['property_id'];
$filupn_ga_submitted_data['remember_when_not_logged_in'] = 'off';
if( isset( $_POST['remember_when_not_logged_in'] ) ) $filupn_ga_submitted_data['remember_when_not_logged_in'] = $_POST['remember_when_not_logged_in'];
$result = filupn_ga_save_settings( $filupn_ga_submitted_data );
}
$filupn_ga_settings = filupn_ga_read_settings();
echo '<h3>Google Analytics Settings</h3>';
if( isset( $result ) ) {
if( $result == true ) {
echo '<p class="updated">Settings saved.</p>';
} elseif( $result == false ) {
echo '<p class="error">Error saving data. Check permissions.</p>';
}
}
// filupn_ga_cookie_debug();
?>
<form method="post" action="<?php echo $_SERVER ['REQUEST_URI']; ?>">
<p><label for="property_id">Your Google Analytics Property ID
<br />(UA-XXXXXXXX-X)</label>
<input name="property_id" id="property_id" class="text" value="<?php echo $filupn_ga_settings['property_id']; ?>" style="width: 100px;" /></p>
<p><input type="checkbox" <?php if( $filupn_ga_settings['remember_when_not_logged_in'] == 'on' ) echo 'checked="checked" '; ?>name="remember_when_not_logged_in" id="remember_when_not_logged_in" style="float:left;margin-right:5px;position:relative;top:2px;" />
<label for="remember_when_not_logged_in">Remember me when logged out of GetSimple and don't add tracking code</label></p>
<p><input type="submit" id="submit" class="submit" value="<?php i18n('BTN_SAVESETTINGS'); ?>" name="submit" /></p>
</form>
<p><a href="http://philipnewcomer.net/getsimple-plugins/google-analytics/#findmyid" target="_new">How do I find my Google Analytics property ID?</a></p>
<?php
}
function filupn_ga_read_settings() {
global $filupn_ga_data_file;
if( file_exists( $filupn_ga_data_file ) ) {
$data = getXML( $filupn_ga_data_file );
$filupn_ga_settings['property_id'] = $data->property_id;
$filupn_ga_settings['remember_when_not_logged_in'] = $data->remember_when_not_logged_in;
} else {
$filupn_ga_settings['property_id'] = null;
$filupn_ga_settings['remember_when_not_logged_in'] = 'on'; // 'on' by default.
filupn_ga_save_settings( $filupn_ga_settings );
}
if( $filupn_ga_settings['remember_when_not_logged_in'] == '' ) { // When upgrading from a previous version
$filupn_ga_settings['remember_when_not_logged_in'] = 'on';
filupn_ga_save_settings( $filupn_ga_settings );
}
$filupn_ga_settings['site_root'] = '/';
return $filupn_ga_settings;
}
function filupn_ga_save_settings( $settings ) {
global $filupn_ga_data_file;
$xml = @new simpleXMLElement( '<google_analytics_settings></google_analytics_settings>' );
$xml->addChild( 'property_id', $settings['property_id'] );
$xml->addChild( 'remember_when_not_logged_in', $settings['remember_when_not_logged_in'] );
return $xml->asXML( $filupn_ga_data_file );
}
function filupn_ga_logout() {
global $filupn_ga_settings;
if( $filupn_ga_settings['remember_when_not_logged_in'] == 'off' ) {
filupn_ga_deletecookie();
}
}
?>