These Google Analytics Code Snippets will add the Google Analytics JavaScript to WordPress websites, utilizing the Genesis Framework and to basic HTML websites. Use of these Google Analytics Code Snippets will allow the Google Analytics Service to track website visitors.
PHP Code Snippet – Genesis Framework
Copy/Paste the PHP code below, to the Genesis Framework, Child Theme functions.php file:
[php]
//* Bamajr included Google Analytics
function bamajr_google_analytics() {
echo ‘
<!– ******************************************************* –>
<!– BEGIN: GOOGLE ANALYTICS –>
<!– see: http://webdevrobert.com/go/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’, ‘UA-########-1’, ‘auto’);
ga(‘require’, ‘linkid’, ‘linkid.js’);
ga(‘send’, ‘pageview’);
</script>
<!– ******************************************************* –>
<!– END: GOOGLE ANALYTICS –>
<!– ******************************************************* –>
‘;
}
add_action( ‘wp_footer’, ‘bamajr_google_analytics’, 999998);
[/php]
HTML Code Snippet
Copy/Paste the HTML code below, to the <head> section of your website:
[html]
<!– ******************************************************* –>
<!– BEGIN: GOOGLE ANALYTICS –>
<!– see: http://webdevrobert.com/go/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’, ‘UA-########-1’, ‘auto’);
ga(‘require’, ‘linkid’, ‘linkid.js’);
ga(‘send’, ‘pageview’);
</script>
<!– ******************************************************* –>
<!– END: GOOGLE ANALYTICS –>
<!– ******************************************************* –>
[/html]
Google Analytics – More Info
- NOTABLE ITEMS:
- ######## represents your own Google Analytics Site ID.
- The 999998 found in: [php]add_action( ‘wp_footer’, ‘bamajr_google_analytics’, 999998);[/php] is the Priority Argument. As this number gets higher, the priority gets lower, causing your hook to be executed further down the page. FYI: Enqueued scripts are executed at priority level 20.
- Why use such a high number in the Priority Argument? To keep analytic scripts from getting in the way of User Interface (UI) elements.
- Why use 999998 instead of 999999 in the Priority Argument? Ah, you’ll have to tune in next week!
- DISCLAIMER: Code Snippets are not posted until they have been tested and verified to work. In most cases, the Code Snippets are in use on this website or a client’s websites. However, all Code Snippets come without any warranty of functionality.