Improve Google Analytics conditional loading
The current logic for condtionally including the GA snippet is confusing and bit inconsistent. In development you would see logging when the snippet wasn't included but this wouldn't happen if you were a logged in admin. Now there's only two cases: 1. Production non admin user: GA snippet is loaded. 2. Anything else (development or admin user): log output
This commit is contained in:
@@ -84,23 +84,25 @@ add_action('wp_head', __NAMESPACE__ . '\\jquery_local_fallback');
|
|||||||
*
|
*
|
||||||
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
|
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
|
||||||
*/
|
*/
|
||||||
function google_analytics() { ?>
|
function google_analytics() {
|
||||||
<script>
|
?>
|
||||||
<?php if (WP_ENV === 'production') : ?>
|
<script>
|
||||||
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
|
<?php if (WP_ENV === 'production' && !current_user_can('manage_options')) : ?>
|
||||||
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
|
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
|
||||||
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
|
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
|
||||||
e.src='//www.google-analytics.com/analytics.js';
|
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
|
||||||
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
|
e.src='//www.google-analytics.com/analytics.js';
|
||||||
<?php else : ?>
|
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
|
||||||
function ga() {
|
<?php else: ?>
|
||||||
console.log('GoogleAnalytics: ' + [].slice.call(arguments));
|
function ga() {
|
||||||
}
|
console.log('Google Analytics: ' + [].slice.call(arguments));
|
||||||
<?php endif; ?>
|
}
|
||||||
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
|
<?php endif; ?>
|
||||||
</script>
|
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
|
||||||
|
</script>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
<?php }
|
if (GOOGLE_ANALYTICS_ID) {
|
||||||
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
|
|
||||||
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
|
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user