Merge pull request #1272 from roots/google-analytics-improvement
Improve Google Analytics conditional loading
This commit is contained in:
@@ -84,23 +84,27 @@ add_action('wp_head', __NAMESPACE__ . '\\jquery_local_fallback');
|
||||
*
|
||||
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
|
||||
*/
|
||||
function google_analytics() { ?>
|
||||
<script>
|
||||
<?php if (WP_ENV === 'production') : ?>
|
||||
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
|
||||
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
|
||||
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
|
||||
e.src='//www.google-analytics.com/analytics.js';
|
||||
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
|
||||
<?php else : ?>
|
||||
function ga() {
|
||||
console.log('GoogleAnalytics: ' + [].slice.call(arguments));
|
||||
}
|
||||
<?php endif; ?>
|
||||
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
|
||||
</script>
|
||||
function google_analytics() {
|
||||
?>
|
||||
<script>
|
||||
<?php if (WP_ENV === 'production' && !current_user_can('manage_options')) : ?>
|
||||
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
|
||||
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
|
||||
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
|
||||
e.src='//www.google-analytics.com/analytics.js';
|
||||
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
|
||||
<?php else: ?>
|
||||
function ga() {
|
||||
if (window.console) {
|
||||
console.log('Google Analytics: ' + [].slice.call(arguments));
|
||||
}
|
||||
}
|
||||
<?php endif; ?>
|
||||
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
<?php }
|
||||
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
|
||||
if (GOOGLE_ANALYTICS_ID) {
|
||||
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user