There are a couple of ways to add Google Analytics to WordPress using the code snippet. Sometimes the easiest solution is to just add another plugin. While that may do exactly what you want, it can also come with more ads on your dashboard but more importantly, you can’t be sure what it’s really doing.
So, with that in mind, I am going to walk you through the quickest way to add Google Analytics to your theme.
Where to start
We’re going to add our Google Analytics tracking code to your themes functions.php file. We will create a new function that contains the code snippet provided by Google.
- You can either open your themes functions file through the admin (Appearance -> Theme Editor) or open the file using a text/code editor. If you’re not a developer/familiar with SSH/FTP then the first option may be for you, otherwise just reach out to someone who can help with this part.
- Next, just copy the snippet below and paste it into the end end of that file.
<php add_action('wp_head','insert_google_analytics', 20); function insert_google_analytics() { ?> <!-- Paste Your Google Analytics Snippet Here --> <!-- See https://www.aarononeill.com/how-to-add-google-analytics-to-wordpress/ for instructions --> <?php }
- Now all you need to do is save your changes and that’s it!
Now, not to contradict myself…
I have made this available as a plugin which you can download on the WordPress plugins page. As above, it’s very light and very simple so feel free to download it and see how I have put it together.