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
|
* 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() {
|
||||||
}
|
if (window.console) {
|
||||||
<?php endif; ?>
|
console.log('Google Analytics: ' + [].slice.call(arguments));
|
||||||
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
|
}
|
||||||
</script>
|
}
|
||||||
|
<?php endif; ?>
|
||||||
|
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