Gathering analytics on your website is important. Yes, WordPress sites come with Jetpack Site Stats nearly out of the box, but Jetpack only gives you a superficial look at user engagement with your website.
If you’re interested deriving detailed insights from your data, like tracking the effectiveness and value of your communications campaigns; segmenting your data to see who is visiting your site and converting into sales or ad revenue; or determining whether users are visiting your site from mobile, desktop, tablet, etc., then you will need something more robust. For most, and especially for beginners, Google Analytics is the best (and free) solution.
There are a variety of plugins you could use to install your code, but with every plugin comes the need for updates, and the possibility that it conflicts with other feature on your site. The fact is that you do not need a plugin because you can do this yourself!
Create an Account
Your obvious first step is to create an account with Google Analytics, providing the details for your site. Google will ask you a few things off the bat:
- Classic or Universal Analytics: I recommend going with Universal at this point. It is the future of Google Analytics, so you might as well get started with it off the bat.
- Account Name: This is essentially a “folder” in your analytics management where you could group more than one site. If you only foresee yourself having one site, this isn’t overly important.
- Website Name: This doesn’t have to exactly match the name of your website in WordPress.
- Website URL: Pretty self-explanatory.
It asks a few other things, but you can figure them out, I promise!
Find Your Snippet
Once your account is established, you’ll automatically be taken to a screen displaying your tracking code. If you already had an account and simply need to find your code, sign in to analytics and click Admin.
Then, use the dropdowns provided to choose the account and property (site) for which you want to install analytics. Finally, in the property column, click .js Tracking Info >> Tracking Code. This will reveal a screen with your tracking code for you to copy.
Place the Snippet
Now that you’ve found the snippet, you need to get it in place on WordPress. In a new window, open your WordPress Dashboard, then:
- In the left navigation, click Appearance >> Editor.
- In the editor, click the template on the right side of the page for your header, called Header in almost all themes.
- Do a Control +f (Command + f on Macs) and search for </head> on the page. This is the closing tag of your template’s HTML header. Your analytics snippet should be included immediately before this closing tag.
- Now, simply copy the snippet you found in Google Analytics and paste it into your WordPress editor before that </head> tag. The snippet you paste in should start with <script>, and end with </script>.
At this point you can click Update File, but I recommend you continue on the the next step.
Add Comments (Recommended)
To easily locate your analytics snippet in the future, you should add some comments to the HTML before and after the snippet to define where it begins and ends.
To do this:
- Copy and paste <!– Google Analytics Snippet Start –> before the opening <script> tag.
- Copy and paste <!– Google Analytics Snippet End –> between the closing </script> tag and the closing </head> tag.
Next time you look at your header file you’ll be able to do a Control + f to search for “Google Analytics,” and you’ll go right to your snippet!
Verify Your Snippet
Finally, to be thorough, it is always a good idea to test your implementation of new code. Some common problems that might arise:
- The misplacement of the snippet within the header.
- Your theme may have multiple templates with different headers. While you might have the snippet in place on your homepage, it may not be on your posts pages, in which case you’re only collecting partial data.
I recommend is a simple-to-use Chrome plugin from Google itself, called Tag Assistant. Open Chrome and install the plugin. At the top right of your browser you will see a new tag icon, as below
Now, visit your website. Depending on the status of your snippet, the tag will change colors, and display a number. The number corresponds to the number of Google snippets detected on the page, and should likely read “1” for you. The colors mean the following:
[tabgroup][tab title=”Green” ]Your snippet is installed and working great![/tab][tab title=”Blue” ]Your snippet is installed, but your could make some small changes to make it more efficient.[/tab][tab title=”Red” ]There are critical issues with your installation, likely preventing it from accurately tracking traffic to your site.[/tab][/tabgroup]
Now, just click around your website to ensure that your new analytics installation is working well for your across all types of pages on your WordPress site! If you encounter a situation where Tag Assistant turns blue or red, click the tag icon to get details about what might be amiss.