Ghost
Last updated: July 8, 2025

How to Add Analytics Code or Any Custom Script to the Header

Table of Contents

Adding analytics or other third-party scripts (like Google Analytics, Facebook Pixel, Hotjar, or custom JavaScript/CSS) to your Ghost website is easy and doesn’t require editing the theme files directly.

Follow these steps to safely inject custom code into the header of your Ghost site:


Step 1: Log in to Your Ghost Admin

First, log in to your Ghost Admin panel. This is usually accessible at:

Once logged in, click on the Settings icon located in the left sidebar.

Ghost Settings Panel


Step 2: Go to Code Injection Settings

In the Settings panel, scroll down to the Advanced section. Click on Code Injection.

This area allows you to safely inject custom code into your site’s <head> and <footer> sections without modifying your theme files.

Code Injection Panel


Step 3: Paste Your Script into the Header Field

Under the Header section, paste your Google Analytics code, tracking scripts, meta tags, custom CSS, or any other header-level scripts.

This code will be automatically added to the <head> section of every page on your site.


Example: Google Analytics Script

Here’s a sample script you can paste into the Header field to enable Google Analytics:

<!-- Google Analytics -->
<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-X"
></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() {
    dataLayer.push(arguments);
  }
  gtag("js", new Date());

  gtag("config", "UA-XXXXXXX-X");
</script>

Replace UA-XXXXXXX-X with your actual Google Analytics Tracking ID.