How to Add Tracking Code in the WordPress Website Head Section

There are several ways of adding tracking code, and in this article, we will list some. Hopefully you find this helpful.

Adding Tracking Code Options

Option 1, use the plugin:

  • You can use Insert Headers and Footers plugin, and the first step is to install it on the website.
  • Once you activate the plugin go to Settings >> Insert Headers and Footers
  • From there, just add your code to the Scripts in the Header Box.
  • If needed you can also add code to the Footer in the box bellow
How to add tracking code in the WordPress website head section: WordPress Settings with arrow pointing indicating Scripts in Header

Option 2 – add it under header.php file.

  • You can find this file under wp-content/themes/your-theme-name/header.php
  • Copy the code you need and paste it just above the closing of the </head> tag.
How to add tracking code in the WordPress website head section: WordPress Appearance with arrow indicating Theme Editor, arrow indicating code, arrow indicating Theme Header

What if you need to place a tracking code only on some specific pages?

You can add a snippet to the functions.php, this file is located under wp-content/themes/your-theme-name/functions.php

You can use the snippet below, and just add your tracking code under the echo, and add the ID of the page where you want the script to load.

if (is_page(add_here_page_id_of_your_page){

function custom_head_script() {

echo “Place your code here and be sure to escape any qotes with backslash”;

}

add_action(‘wp_head’, ‘custom_head_script’);

}

With this last option, you should be extra careful as it can easily break your site. Remember to create a backup before you start with the work, and if possible give it a try on staging site first.

That should be it, and if you need any help, just reach out to support@valet.io or contact us here.