Verify Google Analytics 4 (GA4) Tracking Code on Your Website Using BeautifulSoup


Check for Google Analytics Tag using Python BeautifulSoup

TL;DR

Need to confirm if your Google Analytics 4 (GA4) tag is present across all your PHP pages?

  • Scan a single URL or a full local directory of PHP files
  • Check for presence of gtag('config', 'G-XXXXXXX') in the HTML output
  • Highlight pages missing the GA4 snippet
  • Easily adapt the script to check for any custom string or script

🚀 Perfect for developers, SEOs, and site admins who need to ensure tracking consistency without browser automation.

🔍 Check GA4 Tracking Code for a Single URL

This Python script checks whether the GA4 tracking code gtag('config') is present in a single webpage using requests and BeautifulSoup.

import requests
from bs4 import BeautifulSoup

# Replace with the URL you want to check
link = "https://www.example.com/python/bs4.php"
response = requests.get(link)
soup = BeautifulSoup(response.text, 'html.parser')

html_content = soup.prettify()

if "gtag('config', 'G-ABCDEFGHVG')" in html_content:
    print("GA4 OK")
else:
    print("GA4 Not Found:", link)

🛠️ Check GA4 Tracking Code Across Local Files

Use this Python script to verify if gtag('config') is present in your local directory URLs. It loops through PHP files, fetches their live counterparts, and flags missing GA4 code.


More about os library
import requests
from bs4 import BeautifulSoup
import os

# source directory list to get list of files inside the directory
l1 = ['javascript_tutorial','php_tutorial','html_tutorial','sql_tutorial','python']
l1 = ['javascript_tutorial'] # start with one directory or remove this line for all
for d in l1:
    path = "C:\\xampp\\htdocs\\plus2net\\" + d + "\\"  # full path to directory
    link1 = "https://www.example.com/" + d + "/" # path up to directory level
    files = os.listdir(path) # List of files in the directory

    for f in files: # list of files looping
        f_name, f_extension = os.path.splitext(path + f)
        if (f_extension == '.php'): # To check only .php file extensions
            link = link1 + f # full url by adding file name to path
            response = requests.get(link)
            soup = BeautifulSoup(response.text, 'html.parser')
            # Convert the full HTML to string
            html_content = soup.prettify()
            # Check for GA4 config string (use this for any other matching string)
            if "gtag('config', 'G-ABCDEFGHXVG')" not in html_content:
                print("GA4 Not Found : ", link)

print('All checks completed.')

Expanding if condition check with more file types

        if (
            f_extension == '.php'
            and not f.startswith('link-')
            and not f.startswith('top-link-')
        ):

Checking all files by using list from Sitemap xml file

This Python script reads a sitemap XML file and extracts all the URLs listed within it by handling XML namespaces. It then sends an HTTP request to each page and parses the HTML content using BeautifulSoup. The script checks whether each page includes a specific GA4 tracking code (gtag('config', 'G-DXKVCW4XVG')) and flags any URLs where it is missing. This is useful for verifying analytics installation or any other required snippet across multiple pages on a website.
import requests
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup

sitemap_url = 'https://www.example.com/dir_name/sitemap.xml'

try:
    # Fetch the sitemap content
    response = requests.get(sitemap_url)
    response.raise_for_status()

    # Parse the XML with namespace
    root = ET.fromstring(response.content)

    # Define the namespace
    ns = {'ns': 'https://www.sitemaps.org/schemas/sitemap/0.9'}

    # Find all <loc> tags using the namespace
    urls = [loc.text for loc in root.findall('.//ns:loc', ns)]

    print("Total URLs found:", len(urls))

    for url in urls:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # Convert full HTML to string
        html_content = soup.prettify()

        # Check for GA4 config (or any specific text)
        if "gtag('config', 'G-DXKVCW4XVG')" not in html_content:
            print("GA4 Not Found : ", url)

except Exception as e:
    print("Error:", e)

Conclusion

By automating GA4 tracking code detection using Python and BeautifulSoup, you can quickly validate whether your web pages are properly tagged. This is especially useful for developers and marketers managing large websites, ensuring no page goes untracked by Google Analytics. Whether you're auditing a single page or an entire local directory of PHP files, this approach makes the process efficient and scalable.

You can reuse this script to detect any custom string or snippet across your pages by modifying the match condition.

Creating Sitemap xml file from a directory
Reading Sitemap xml file and fetching details to store

How to Track Traffic from ChatGPT and Other AI Tools in GA4

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer