Google Analytics

by | Jan 28, 2015 | Web Developer

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

  1. 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!
  2. 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.
Logo with image

About the Author:

WebDevRobert

Innovative Technology Professional, working on IT/OT Convergence & Security, in the foundry industry.

Website Designer/Developer since the mid 1990’s.

Apple, Linux and Network Administrator since the late 1990’s.

Background includes several years of IT/OT and IIoT Administration, Communications Infrastructure Design and Project Management.

Currently the VP of Technology for Oil City Iron Works, Inc., in Corsicana, TX.

Subscribe for Updates:

This field is for validation purposes and should be left unchanged.
This field is hidden when viewing the form

Next Steps: Sync an Email Add-On

To get the most out of your form, we suggest that you sync this form with an email add-on. To learn more about your email add-on options, visit the following page (https://www.gravityforms.com/the-8-best-email-plugins-for-wordpress-in-2020/). Important: Delete this tip before you publish the form.
Name(Required)
Email(Required)