This post has been updated in January 2023 to change our stance on this.
Due to complications with GA4, we recommend use the Google Site Kit plugin to set up and manage your Google Analytics tracking.
Ignore the rest of this tutorial and follow the Site Kit setup instructions.
Do not use a plugin to add Google Analytics tracking.
Seeing your traffic stats in WordPress is entirely unnecessary. You should be monitoring your search traffic by logging in once per month in order to answer very specific questions. Watching daily and weekly numbers does nothing but give you anxiety.
Genesis has made it very easy to add scripts to the header and footer areas of your site. One common reason you'd want to do this is to get Google Analytics working on your WordPress site.
Jump to:
Getting the tracking code (Universal Analytics)
If you haven't signed up for Google Analytics, go ahead and do that. We won't cover that here, because there's 1000 articles already written about it in Google.
Once signed up, head to Settings > Property > Tracking Info > Tracking Code
Then grab the gtag.js tracking code:
Inserting the tracking code
Use the Admin > Snippets > Add New (Code Snippets plugin) to create a new snippet, with the following code:
/*
* we want to use code snippets because the customizer is unreliable
* note: Google Analytics is not GDPR compliant
*/
add_action( 'wp_head', 'custom_google_analytics' );
function custom_google_analytics() {
?>
<!-- delete this line and insert the complete Google Analytics code here -->
<?php
}
You're all set to start tracking your site metrics! Google can take up to 24 hours to start processing data, but there should be an option on the page to check it's been implemented right away.
Remember to flush your cache if you have this in place.
GA4 setup + install
The GA4 tracking code can be installed alongside the Universal Analytics tracking code:
Pagespeed optimization
We highly recommend everyone have WP Rocket for pagespeeed purposes.
Make sure you have WP Rocket > File Optimization > Load Javascript Deferred > enabled
You'll want to add the following to your WP Rocket > Preload > Prefetch DNS Requests
//google-analytics.com
//www.google-analytics.com
//ssl.google-analytics.com
Filtering FBCLID
A number of services add tracking parameters to outbound links, including Google Adwords, Facebook, and newsletter. Even the "Customize" screen generates unique URLs that clutter up your Google Analytics.
Go to Google Analytics > Admin > View > View Settings > Exclude URL Query Parameters
Here's a starting list that you'll want to expand on: gclid,epik,fbclid,customize_changeset_uuid
Enable Site Search Tracking
You'll want to enable site search tracking to see what your visitors are looking for once they land on your site:
Optimizing with Google Analytics
This post on optimizing your site with Google Analytics from Tom Crowe Digital has some good tips.
GDPR violation
According to a recent ruling from the French data protection authority (CNIL), Google Analytics violates GDPR regulations and should not be served to European citizens.
At this time we're unaware of any solution which automatically excludes European users.
Troubleshooting
If you need further troubleshooting, we recommend hiring Alison at Digital-ade.