How to add Google Analytics to WordPress

Before we start, you might have some pressing questions about WordPress. If you are already knowledgeable of WordPress and Google Analytics, you can skip these answers and go straight to the tutorial below.

wordpress

What is WordPress?

WordPress is a content management system. It is open-source software that anyone can use to make any website imaginable. It started as a blogging platform in 2003 but soon transformed into a CMS and later a full-fledged website building platform. The key advantage at the time was you were able to edit your website using a graphical user interface.

How does a WordPress site work?

WordPress requires two components to function on your web server: PHP and MySQL (database). PHP is the dynamic server-side language WordPress is written in. MySQL saves the dynamic data to be served to users.

How does web analytics work?

Web analytics tracks user interaction with your website and on your web pages through their behaviors and activities. Generated Personalized reports allow marketers to understand user interactions and behavior to improve the website and its content.

Why is website traffic important?

Website traffic is essential for many reasons. The more organic traffic your site receives, the more potential customers you will have. The number of visitors to your website becomes the number of opportunities your business has at giving an impression, generating qualified leads, sharing your brand, and building relationships.

Why use Google Analytics vs. WordPress stats?

Jetpack Statistics gives you just a basic overview of your site’s traffic, whereas Google Analytics allows you to take advantage of robust tracking and analytics features.

How-to: Add Google Analytics to WordPress

This how-to article assumes you have already set up Google Analytics; if you haven’t already set up Google Analytics, you can learn how to in our previous how to guide.

We have laid out three approaches to adding Google Analytics to WordPress; for Beginners, Intermediate, and Advanced users.

  1. Beginners: Adding Google Analytics

    Insert Header Footer Script Plugin

    InstallHeader Footer Script WordPress Plugin, activate the plugin.

    Go to Settings >> Insert Header Footer Script settings >> Copy and paste your Analytics tracking script into the plugins header code area.

    Be careful, if you disable the plugin at any time, the script will not track website traffic.

  2. Intermediate: Adding Google Analytics

    Add Google Analytics to WordPress

    You can add the script one of three ways:

    1. In WordPress, going to Appearances >> Theme Editor >> header.php and adding the script.

    2. Using File Manager in cPanel (hosting).

    3. Using a Text Editor, editing it via sFTP.

    When you add the script to your header file, ensure you have a child theme set up, or else you will lose your changes when you update your theme.

    Place the tracking script before the closing </head> tag; Illustrated in the image above.

  3. Advanced: Adding Google Analytics

    functions-php

    For advanced users, you can add your tracking scripts to the functions.php file. Adding the script to your functions file will ensure the Google Analytics tracking script will be added to all header files.

    Add this script to your functions.php file, be mindful of the opening and closing PHP tags. You only need to add one option below to your file. I have included two options to choose from.


    <?php
    /**
    * Option #1. add script to head with wrapping php.
    */
    function addtohead_cf() {
    ?>
    <script></script>
    <?php

    /**
    * Option #2. Or use this code below.
    */

    }
    add_action('wp_head', 'addtohead_cf');
    /**
    * add script to head using echo
    */
    function addtohead_cf() {
    echo "<script></script>";
    }
    add_action( 'wp_head', 'addtohead_cf' );

    A link to the code in pastebin.

Frequently Asked WordPress + Google Analytics Questions

These are just a few questions we frequently receive from clients on Click Analytics.

What is the difference between Google Search Console vs. Google Analytics?

Google Analytics is user-oriented tracking, providing information related to those who visit and interact with your website. Google Search Console is search-engine focused analytics, providing tools and insights that can help site owners improve visibility and presence.

Can I use multiple Google Analytics plugins?

If you use multiple Google Analytics plugins, you will compromise your data integrity. Adding multiple tracking plugins will skew your data and provide inflated stats.

Why do you place Google Analytics in the header section of WordPress?

The Benefit of Adding Analytics Code into the Header is because JavaScript snippets are triggered in the order they are embedded in your page.

If you have an issues, please email or DM me on Twitter.


Published on: 2021-02-12
Updated on: 2021-02-27

Isaac Adams-Hands

Isaac Adams-Hands is an SEO Director, Full Stack Developer, and InfoSec enthusiast. He received his Bachelor’s Degree from the University of Western Sydney before joining various marketing positions in search portals, higher education, and addiction recovery marketing agencies.