Set up Google Analytics in WordPress using the Code Snippets Plugin

If you’re serious about your business you should be monitoring the traffic to your website. One of the most comprehensive tools for this is Google Analytics.

Currently there are two ways of doing this – using the old Universal Analytics tag or the newer GA4 tag. This article assume that you have already created a tag and provides instructions on adding it to your website.

The process is the same whether it’s a GA4 or a UA tag, however there is one extra step required for UA tags in order to ensure the data you collect protects private information of visitors to your site, which is important for compliance with GDPR.

How to Set up Google Analytics Property on your WordPress Website using Code Snippets

Time 10 minutes

  1. Create your Analytics Property

    The steps for creating an Analytics property are not included here and it is assumed that you have already completed this step.

  2. Install the Code Snippets Plugin

    Visit the Code Snippets page and click Download to download the plugin files:
    Install the Code Snippets plugin.

  3. Create a New Snippet for your GA Property’s Tag

    Open the Snippets plugin.
    Click Add New.
    Give your new snippet a label. We’ve used “Update Header Code” as ours as this will be used a generic snippet for adding tracking codes – such as the Facebook Pixel or Google Tag Manager – to the header, not just GA.
    Paste the base code as shown above. You can copy and paste this from here.
    Select Run snippet everywhere.
    Save your snippet.

    This will be used to edit the functions.php file and will be used to insert the code into your site’s header.

  4. Find Your Global Site Tag JavaScript Code

    – Open Google Analytics. If you have more than one account, select the Account you want to use and and the appropriate property.
    – Click the Admin wheel (at the bottom of the left menu sidebar) and the select Setup Assistant.
    – Click on Tag installation and select Web from the Data Streams list.

    You’ll see information about the tag to use on your website, including the Stream URL (your website address), the Stream Name (this will match the Property settings) and the Measurement ID.

    – Scroll down to the Tagging Instructions section and select Global site tag (gtag.js) and copy the code.

  5. Copy the Tag Code into your Snippet

    – Copy the code in this tag to your clipboard.
    – Go back to your WordPress website and open the Snippets list.
    – Open the Header and Footer snippet that we created earlier.

  6. Update and Save the Snippet

    With the code snippet open, replace the line of code that says <!– your code goes here –> with the code copied from Google Analytics.
    – Apply any formatting.
    – Add a Description (for your reference only).
    – Add any tags. In this example we’re using “google analytics”.
    – Click Save Changes and Activate.

    That’s it!

The final step is to go over to Google Analytics at the same time as visiting your site in another open tab or browser window.

Go to the Real Time view and you should see a dot on or around your location . This means that it’s working.

If you have a very busy site and can’t tell from the Real Time view (that’s where you want to be of course so, fantastic!) you can check using the SiteChecker Pro website’s Google Analytics Checker.

Just type (or paste) your URL into the field and click the blue arrow button. The result will be shown below.

Use this Code in the Code Snippet

add_action( 'wp_head', function(){
    <!-- your code goes here -->

I no longer offer tech support, preferring to focus on websites, SEO, and branding. However, I still love writing! If you like this post and want to support me, please click the button below!

More Like This

Do you need someone to maintain your WordPress site?

Whether you’re looking for a monthly maintenance plan or occassional updates, we can help.