Need to confirm if your Google Analytics 4 (GA4) tag is present across all your PHP pages?
gtag('config', 'G-XXXXXXX')
in the HTML output🚀 Perfect for developers, SEOs, and site admins who need to ensure tracking consistency without browser automation.
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)
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.
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.')
if (
f_extension == '.php'
and not f.startswith('link-')
and not f.startswith('top-link-')
):
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)
Author
🎥 Join me live on YouTubePassionate 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.