Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Created September 30, 2015 03:49
Show Gist options
  • Save designbuildtest/ad6c71626d7318d62a74 to your computer and use it in GitHub Desktop.
Save designbuildtest/ad6c71626d7318d62a74 to your computer and use it in GitHub Desktop.
Google Analytics tracking code HTML
<?php
/**
* Google Analytics tracking code.
*/
function dbt_add_google_analytics_tracking_code_to_document_head() {
$google_analytics_code = get_option( 'dbt_google_analytics_code' );
if ( $google_analytics_code && ! is_user_logged_in() && ! is_customize_preview() ) { ?>
<!-- Google Analytics -->
<script>
(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');
ga('create', '<?php esc_attr_e( $google_analytics_code ); ?>', 'auto');
ga('send', 'pageview');
</script>
<?php
}
}
add_action( 'wp_head', 'dbt_add_google_analytics_tracking_code_to_document_head', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment