Skip to content

Instantly share code, notes, and snippets.

@rtpHarry
Forked from kenhowardpdx/WordPress Google Analytics
Last active March 16, 2019 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rtpHarry/241a63cf5800bad9ae6bfb5c8391a9ec to your computer and use it in GitHub Desktop.
Save rtpHarry/241a63cf5800bad9ae6bfb5c8391a9ec to your computer and use it in GitHub Desktop.
Add Google Analytics to WordPress Theme functions.php
// originally based on https://gist.github.com/kenhowardpdx/8484076
// updated to current gtag.js snippet and moved to head
function rtp_init_analytics() {
$analytics_id = 'YOUR UA CODE HERE';
$analytics = '<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id='. $analytics_id .'"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "'. $analytics_id . '");
</script>';
echo "\n" . $analytics;
}
// filter out administrators
if (!is_admin()) {
if(!current_user_can( 'manage_options' ) ) {
add_action('wp_head', 'rtp_init_analytics', 35);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment