Saurabh Web Solutions Subscribe
WordPress

How to Add Google Analytics, AdSense and Verification Codes to WordPress (Safely)

1 free file — jump to downloads
How to Add Google Analytics, AdSense and Verification Codes to WordPress (Safely) - cover image
On this page
  1. Know what kind of code you have
  2. Method 1: A header and footer code plugin (simplest)
  3. Method 2: Google Site Kit or your SEO plugin
  4. Method 3: A child theme (for developers)
  5. Adding Google Analytics 4 step by step
  6. Adding the AdSense code
  7. Adding a Search Console verification tag
  8. How to check any code is really on your site
  9. Common mistakes
  10. Download the files

Google Analytics, AdSense, Search Console verification, the Meta Pixel and chat widgets all ask you to "paste this code into the head of your website". On WordPress, the place most tutorials point to, the theme's header.php file, is the worst choice: the next theme update silently deletes your code, and your analytics stop recording without any warning.

Here are three safe ways to add Google Analytics to WordPress, plus AdSense and verification codes, and how to confirm each one is working.

Know what kind of code you have

  • Head code: Google Analytics (gtag.js), Google Tag Manager's main script, the AdSense script, Search Console verification meta tags, Pinterest and Bing verification.
  • Body code: the Google Tag Manager <noscript> part, which goes right after the opening <body> tag.
  • Footer code: chat widgets, WhatsApp buttons and scripts that should load last.

A code-snippet plugin stores your code in the database and adds it to every page, whichever theme you use. The free SWS Site Fixer plugin (download below) includes this:

  1. Install and activate the plugin.
  2. Open Site Fixer > Header & Footer.
  3. Paste head codes (Analytics, AdSense, verification tags) into Header code.
  4. Paste the GTM noscript part into Body code and chat widgets into Footer code.
  5. Save, then test (see below).

Because the code is stored outside the theme, you can switch or update themes and it stays in place.

Method 2: Google Site Kit or your SEO plugin

Google's official Site Kit plugin connects Analytics, Search Console and AdSense by signing in with your Google account, with no code to copy. Many SEO plugins also have a field for Search Console and other verification codes. This is a good choice if you want reports inside the WordPress dashboard.

Important: use only one method per code. Adding Google Analytics through Site Kit and a header plugin counts every visit twice and makes your reports wrong.

Method 3: A child theme (for developers)

If you prefer code, add a function to a child theme's functions.php that hooks into wp_head. A child theme is not overwritten when the parent theme updates. Never edit the parent theme's files directly.

Adding Google Analytics 4 step by step

  1. In Google Analytics, open Admin > Data streams and select your web stream (or create one).
  2. Copy your Measurement ID (starts with G-) and the Google tag code under "View tag instructions".
  3. Paste the full tag into your header code field using Method 1, or connect with Site Kit (Method 2).
  4. Open your site in a private window and check Reports > Realtime; you should see yourself as an active user within a minute or two.

Adding the AdSense code

  1. In AdSense, open Ads > Get code (or the site's code snippet) and copy the script that contains your ca-pub- publisher ID.
  2. Paste it into the header code field once.
  3. Add the ads.txt line AdSense gives you: create a plain text file called ads.txt in your site's root folder with that line, or use a plugin that manages it.
  4. Choose Auto ads or create ad units in AdSense.
  5. Set up the consent message for European visitors under Privacy & messaging.

Before applying, make sure your site has original, helpful content plus About, Contact and Privacy Policy pages. Thin sites are a common reason for AdSense rejection.

Adding a Search Console verification tag

Search Console's HTML tag method gives you a meta tag like <meta name="google-site-verification" content="...">. Paste the whole tag into the header code field, save, then click Verify in Search Console. Keep the tag in place permanently; removing it can unverify the property later. More in getting your WordPress site on Google.

How to check any code is really on your site

  • Open your homepage, right-click and choose View page source.
  • Press Ctrl+F (Cmd+F on Mac) and search for your ID: G-, ca-pub- or google-site-verification.
  • If you use a caching plugin, clear the cache first, or you may see an old version of the page.

Common mistakes

  • Pasting code into header.php of the parent theme.
  • Adding the same tracking code through two plugins.
  • Putting head code in a text widget, where it ends up in the body or gets stripped.
  • Forgetting cookie consent for European visitors.
  • Copying "smart quotes" from a document, which breaks the code. Copy directly from Google.

Download the free plugin below to manage all your header and footer codes in one place. Keep the site fast while adding scripts with our WordPress speed guide.

Download the files

Free to use in your own and client projects · no sign-up · tested before upload. Always try files on a staging site first and keep a backup.

Download not working? Tell me and I'll fix it.

Frequently asked questions

Where should Google Analytics code go in WordPress?

The Google tag (gtag.js) belongs inside the head section of every page. Use a header-code plugin, your SEO or analytics plugin, or Google Site Kit rather than editing theme files, so the code survives theme updates.

Why did my tracking code disappear?

If you pasted it into a theme file such as header.php, a theme update replaces that file and removes your code. Switching themes removes it too. Store the code in a plugin or child theme instead.

Can I add the AdSense code more than once?

The main AdSense script only needs to be on each page once. Adding it several times, for example through a theme option and a plugin, can cause errors. Individual ad units are separate and can appear multiple times.

Do I need a cookie consent banner for Google Analytics and AdSense?

For visitors in the EEA, UK and Switzerland, Google requires a consent solution for personalized ads, and privacy laws apply to analytics cookies too. AdSense offers a Google-certified consent message under Privacy & messaging.

How do I check my Google Analytics code is working?

Open your site in a private window, then check the Realtime report in Google Analytics. You can also view the page source and search for your measurement ID, which starts with G-.