Skip to content

Instantly share code, notes, and snippets.

@dannydover
Last active October 6, 2018 22:06
Show Gist options
  • Select an option

  • Save dannydover/164f5d5abebb513d3a1b8a03568408b8 to your computer and use it in GitHub Desktop.

Select an option

Save dannydover/164f5d5abebb513d3a1b8a03568408b8 to your computer and use it in GitHub Desktop.
Add Google Analytics (gtag.js) Tracking to top of <head> in Wordpress via Theme functions.php
<?php
// Google Analytics Tracking Code Snippet
add_action( 'wp_head', 'google_analytics_tracking', 1 );
function google_analytics_tracking( $priority ) {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-X');
</script>
<?php
};
?>
// REMEMBER TO UPDATE UA CODE "UA-XXXXXXX-X" IN BOTH PLACES IN GIST ABOVE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment