Learning about the people who come to your website can be a very useful thing. It can help you realize what type of content your audience is looking for, and in turn help you better serve their needs. And while not every website is the type that requires this kind of data, if you’re running a more complex site, or just want to know more about your users, you might be interested in adding Google Analytics to your website.
What is Google Analytics?
By adding a bit of code to your website, you can allow Google Analytics to track and log detailed information about every visitor. Adding the Google Analytics tracking code to your website is pretty straightforward to do. And while there are plugins out there to help with this like Monster Analytics, the simplest way is to add a small function to your child theme.
If you aren’t sure what a WordPress child theme is or why it’s so important, we wrote a quick article about it called “Why having a child theme is the best thing you’ll ever do for your WordPress website”. As a sidenote, every theme from Mint Themes automatically creates and installs a child theme for you.
Add Google Analytics tracking code to your child theme
Back to the topic at hand, by adding the Google Analytics code to your child theme you won’t require any additional plugins. This can save you money, especially if you don’t require the additional complexity covered by the plugin mentioned above. If you’re just getting started, or you’re not a data-mining expert, the following code is the surest way to get going with Google Analytics quickly:
- Log into your Google Analytics account at https://analytics.google.com/analytics/web/
- After logging in, if you haven’t already created an “account” for your website in Google Analytics, click on the “Admin” tab from the left side menu and click “Create Account”
- Once your account is created, go to “Admin” > “Tracking Info” > “Tracking Code”. Keep this window/tab open so you can copy the tracking code later.
- In your WordPress dashboard, with your child theme activated, go to “Appearance” > “Editor”, and make sure you are editing the “functions.php” file.
- Add this code to it:
- Replace line 10 with the code you will copy from Step 3 above.
- Save the functions.php file.
- That’s it! You’ve now added the Google Analytics Tracking Code to every page of your website.
Photo by Neven Krcmarek on Unsplash