Skip to content

Instantly share code, notes, and snippets.

@kenhowardpdx
Last active April 5, 2023 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kenhowardpdx/8484076 to your computer and use it in GitHub Desktop.
Save kenhowardpdx/8484076 to your computer and use it in GitHub Desktop.
Add Google Analytics to WordPress Theme functions.php
function init_analytics() {
$analytics = '<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-XXXXXXXX-X\']);
_gaq.push([\'_trackPageview\']);
(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://\' : \'http://\') + \'stats.g.doubleclick.net/dc.js\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>';
echo "\n" . $analytics;
}
if (!is_admin()) {
//load front-end options here.
if(!current_user_can( 'manage_options' ) ) {
add_action('wp_footer', 'init_analytics', 35);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment