Pop Up Image

Get the Full PDF guide to creating an online store with WooCommerce (88 pages) for FREE

A comprehensive database for everything WordPress related.

TOP
WPKlik Logo Newsletter

Sign up and receive a free copy of How to Create an online Store with WooCommerce (full guide)

Install-google-analytics-code-in-wordpress

How to Install Google Analytics Code in WordPress Site

First of all, you need to copy the Tracking code (gtag.js) from your Google Analytics account. You can find Tracking code for your site at your Google Analytics account by going to Admin >> Property >> Tracking Info >>Tracking Code.

install-google-analytics

It should look like the code below, just with numbers instead of XXXXXXXX.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); 
gtag('config', 'UA-XXXXXXXX-1');
</script>

Essentially, there are a few ways to go about this; this article will cover the “no plugin” approach, where you connect your site to Google Analytics by implementing a simple line of code. Admittedly, using plugins is the go-to practice for most people today, and is a more straightforward process for someone new to WordPress or code in general. For more information on that method and some of the most notable Google Analytics plugins, you can check out our article about The Best Google Analytics Plugins for WordPress.

That being said, implementing Google Analytics manually through code is not as hard as it might sound. The fact of the matter is that it might be less reliable than plugins for some users, as there’s a chance that the code you integrate with your theme might disappear if you switch or update your theme, and this is something you should be mindful of.

Here are a couple of ways to install Google Analytics by just using code:

Adding code in the header.php file

You can locate and edit the header.php file in your WordPress theme folder. You should copy your Google Analytics tracking code and paste it in the header.php file somewhere between <head> and </head> tag, save your changes and upload the file to your server.

Step 1: In Left menu, under Appearance, click on Editor

When you login in the WordPress admin area, find Appearance tab in the left menu. Click on Appearance tab and click on the Editor item in the submenu list.

Install Google Analytics Code

Step 2: Open “header.php” to edit the file

When you open Editor, in the right menu you will find “header.php” file. Click on it to edit the file.

Google Analytics Code

Step 3: Find <head> in Editor

Google recommends to add Analytics code snippet just under the <head> tag. However it is also OK to add code anywhere between <head> and </head> tag.

In the central column find <head> tag. It should look similar to the image below depending on the theme you are using.

Google-Analytics-Code-wordpress

Step 4: Paste Google Analytics code right under <head> and click Update

Paste Google Analytics code just under the <head> tag. Once you do that, your code should look similar to the image below. Don’t forget to change XXXXXXXX with your Google Analytics tracking ID.

After you paste the code, click on Update File button on the bottom of the page.

Google Analytics wordpress

Your code is now installed, and you should be able to see the traffic on the site in your Google Analytics account.

Add Google Analytics code to functions.php

There’s also an option to add your tracking code to the functions.php file. This way, the code will be added to all the pages on your WordPress site automatically. Here’s how you do that:

Step 1: In Left menu, under Appearance, click on Editor

Install Google Analytics Code

Step 2: CTRL+F and find “functions.php” in the right menu

theme functions

Step 3: Paste this code to functions.php

Don’t forget to change XXXXXXXX with your Google Analytics tracking ID.

add_action('wp_head','wpklik_add_googleanalytics'); 
function wpklik_add_googleanalytics() { ?> 
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); 
gtag('config', 'UA-XXXXXXXX-1');
</script> 
<?php }

Step 4: Update your functions.php file

Now you will be able to monitor the data you collect and apply the newly gained knowledge to your site.

We hope that you found this article to be helpful. If you liked it, please feel free to check out some of these articles as well!

Newsletter

WordPress perfection at your fingertips.

If you enjoyed this article, feel free to subscribe to our newsletter using the form below. You can also follow us on Facebook and Twitter and subscribe to our YouTube channel for WordPress video tutorials.

Leave a Reply