How to Integrate Google Analytics 4 (GA4) with Your Ghost CMS Website

Tracking website performance is crucial for understanding your audience and optimizing your content strategy. Google Analytics 4 (GA4) is Google's latest analytics platform, offering a more event-driven approach to data collection. This guide will walk you through the process of adding GA4 tracking to your Ghost CMS website.


Understanding GA4 and Ghost Integration

Ghost CMS is designed for speed and simplicity. While it doesn't have a built-in plugin system for analytics, it provides a straightforward way to inject custom code into your site's header or footer, which is exactly what we'll use for GA4.

You'll need your GA4 Measurement ID, which typically starts with G-XXXXXXXXXX.


Step-by-Step Guide to Integrating GA4

There are two primary methods to add your GA4 tracking code to Ghost:

  1. Using Ghost's Code Injection Feature (Recommended): This is the easiest and most common method, as it doesn't require direct theme file modification.
  2. Editing Your Theme Files Directly (Advanced): This method offers more control but requires theme file access and re-uploading.

Let's dive into the recommended method first.


This method is ideal for most users as it's simple and doesn't require touching your theme's files directly.

1. Obtain Your GA4 Measurement ID

If you haven't already, set up a Google Analytics 4 property for your website.

  • Go to Google Analytics.
  • Select your GA4 property.
  • Navigate to Admin (the gear icon at the bottom left).
  • In the "Property" column, click on Data Streams.
  • Click on your web data stream (your website URL).
  • You'll find your Measurement ID (e.g., G-XXXXXXXXXX) at the top right of the "Web stream details" page. Copy this ID.

2. Access Ghost's Code Injection

Ghost provides a dedicated section to inject code across your entire site.

  • Log in to your Ghost Admin panel.
  • Go to Settings (the gear icon in the bottom left sidebar).
  • Click on Code injection under the "Advanced" section.

3. Inject the GA4 Tracking Code

You'll paste the GA4 JavaScript snippet into the "Site Header" section.

  • Save the changes.

In the "Site Header" textarea, paste the following code, replacing G-XXXXXXXXXX with your actual GA4 Measurement ID:HTML

<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>

Method 2: Editing Your Theme Files Directly (Advanced)

This method gives you slightly more control and can be useful if you have specific theme-related reasons, but it requires downloading, modifying, and re-uploading your theme.

1. Obtain Your GA4 Measurement ID

  • Follow Step 1 from Method 1 to get your G-XXXXXXXXXX Measurement ID.

2. Download Your Ghost Theme

  • Log in to your Ghost Admin panel.
  • Go to Settings > Design & branding > Customize.
  • Scroll down to the "Advanced" section and click Download theme.
  • Unzip the downloaded file to access its contents.

3. Edit the default.hbs File

The default.hbs file is the main template that all other pages inherit from. We'll place the GA4 code here so it appears on every page.

  • Open the default.hbs file (usually found in the root of your theme folder or within a partials directory) in a code editor.
  • Locate the closing </head> tag.
  • Save the default.hbs file.

Just before the </head> tag, paste the GA4 tracking code. Replace G-XXXXXXXXXX with your actual Measurement ID:HTML

<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>
</head> ```

4. Re-upload Your Modified Theme

  • Zip your entire theme folder back up. Ensure the .zip file contains the theme's root directory, not just the contents of the directory.
  • Go back to Settings > Design & branding in Ghost.
  • Click Upload theme and select your new zipped theme file. Ghost will prompt you to activate it. Confirm the activation.

Verify Your GA4 Installation

After implementing either method, it's crucial to verify that GA4 is correctly receiving data from your Ghost site.

  1. Visit Your Website: Open your Ghost website in a new browser tab.
  2. Check Google Analytics Realtime Report:
    • Go to your Google Analytics 4 property.
    • Navigate to Reports > Realtime.
    • You should see active users on your site, including yourself. If you see data flowing in, your GA4 tracking is successfully installed!

Important Considerations

  • Cookie Consent: Depending on your region (e.g., GDPR in Europe, CCPA in California), you might need to implement a cookie consent banner on your Ghost site. GA4, by default, sets cookies. Ghost doesn't have a built-in solution for this, so you'll need to use a third-party service or implement a custom solution.
  • Ad Blockers: Be aware that some visitors might use ad blockers or privacy extensions that prevent GA4 scripts from loading, which can result in underreported data.
  • Debugging: If you don't see data in GA4, use the Google Tag Assistant Chrome extension or GA4's DebugView (under Admin > DebugView) to troubleshoot your implementation.
  • Measurement ID: Always double-check that you've correctly pasted your GA4 Measurement ID (G-XXXXXXXXXX) into the code.

By following these steps, you'll successfully integrate Google Analytics 4 with your Ghost CMS website, allowing you to gain valuable insights into your site's performance and user behavior.

Read more

Ghost CMS vs WordPress: A Deep Technical Comparison for Developers and Publishers

The choice between Ghost CMS and WordPress extends far beyond surface-level features. For developers, system administrators, and technically-minded publishers, understanding the fundamental architectural differences, performance characteristics, and technical trade-offs between these platforms is crucial for making informed decisions. This comprehensive analysis examines both platforms through a technical lens, exploring everything