Add Google Analytics 4 to WordPress
Add the Google Analytics 4 (gtag.js) tag to the head of every page on your WordPress site without an analytics plugin.
<!-- Google tag (gtag.js). Replace G-XXXXXXXXXX with your Measurement ID in both places. -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push( arguments ); }
gtag( 'js', new Date() );
gtag( 'config', 'G-XXXXXXXXXX' );
</script>
What it does
Google Analytics 4 needs a short tag in the head of every page. You don't need a whole plugin for that.
Replace G-XXXXXXXXXX with your Measurement ID in both places. You'll find it in Google Analytics under Admin, Data streams.
Good to know
- In the EU and UK you need visitors' consent before loading analytics. In Snipfire Pro, set this snippet's cookie consent category to Statistics, so it only loads after the visitor agrees.
- Don't add it if another plugin, such as Site Kit, already adds the Google tag. You'd count every visit twice.