A client wanted to be sure GTM was installed across their whole site, but tag coverage was spotty. In some cases, only GA4 was installed. The client’s site is primarily made up of static HTML files, meaning that to go in and change the code would be very tedious, especially knowing they had more than 950 such files on their server (not counting a few Node.js projects in subdirectories).
This problem was posed to me because of my previous experience bulk editing some static HTML sites: https://mcfxsolutions.webpagefxdev.com/using-a-shell-script-to-install-gtm-on-an-html-only-site/
However, the situation was a little more complex this time for a few reasons:
The first thing I did was get all my files together, which turned out to be more difficult than I expected. If I could zip up the whole site and download it, I could use some shell commands to remove every non-HTML file from the unzipped archive; but because of the sheer amount of images and other miscellaneous files, downloading all that in the first place would have taken ages. Instead, I looked through every main directory on the website and, if it didn’t have static HTML files, I’d ignore it. I downloaded the remaining directories.
Now I had a bunch of directories, but they didn’t necessarily only contain static HTML files. I’d need to remove every non-HTML file, and for that, I’d need a shell command. I popped open TeamAI and started asking questions.
After a little refinement, TeamAI gave me: find . -type f ! -name "*.htm" -delete
This script found every file that did not end in “.htm” and deleted it. Great! I now had 955 files to work with.
Now came the real challenge: I would need to remove every instance of GA4 that was present on any of these 955 files. Like last time, I attempted to use sed. Sed, by way of explanation, is a UNIX command that handles stream editing (short for stream editor, if you’re wondering about the name). At first, I tried to delete the whole script, which necessitated some multi-line string manipulation. As I quickly found out, though, sed (as well as a similar command, awk) is not very good at multi-line string replacement. I would either have to spend even more time trying to make it work like this, remove the scripts line-by-line, or find a third way. The first option seemed mostly fruitless, and the second one would be impossible (considering the GA4 install script has a line that just says “<script>”, this method would just remove all script tags — not good!), so I decided on finding another way.
I realized that only one line in the GA4 install script matters when it comes to installing GA4: the gtag('config', ...) call. Without this command, gtag.js is set up but no Google Tag is ever added to the page. The code on either side of it is harmless (it doesn’t matter if it’s there or not; it won’t cause any issues), so I could leave it alone.
When I realized that, I asked TeamAI for a new script, and it gave me this:
#!/bin/bash
log_file="error.log"
find . -type f -name "*.htm" -print0 | while IFS= read -r -d $'\0' file; do
echo "Processing file: $file"
# Use sed to delete the line directly, with escaping
if ! sed -i "" "/gtag\\('config', 'G-Z0C9Y9EZ60'\\);/d" "$file" 2>> "$log_file"; then
echo "Error processing file: $file" >> "$log_file"
fi
done
echo "Script complete."
This script does the following:
gtag('config', ...) call, replace it with an empty stringAt the end of it, there was no trace of the GA4 data stream ID on any of the pages. Now I just had to add GTM on any of the pages that didn’t have it. I used a similar script provided by TeamAI here:
#!/bin/bash
# The script will find all .htm files recursively in the current directory.
# For each file that does not contain "GTM-MNG4TV2S", it will insert
# the provided script tag after the opening <head> tag.
# You can modify this variable to change the starting directory.
SEARCH_DIR="."
# The script tag to be inserted into the files.
SCRIPT_TAG="<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-MNG4TV2S');</script>"
# Find all .htm files, excluding those containing the GTM string.
# Then, insert the script tag after the <head> tag.
find "$SEARCH_DIR" -type f -name "*.htm" -print0 | while IFS= read -r -d $'\0' file; do
if ! grep -q "GTM-MNG4TV2S" "$file"; then
sed -i -e "/<head>/a\\
$SCRIPT_TAG" "$file"
fi
done
I used grep afterwards to make sure that no files had the GA4 data stream ID, and that every file [that had a <head> tag] had the GTM container ID. (Grep is a command that searches for text; sed is like grep’s kid that can replace as well as find.)
After making sure everything was correct, I zipped up the files and sent them over to V for uploading.
Used a few shell scripts to remove and add code to 930 static HTML files:
# Find all .htm files
find . -type f ! -name "*.htm" -delete
# Remove GA4
#!/bin/bash
log_file="error.log"
find . -type f -name "*.htm" -print0 | while IFS= read -r -d $'\0' file; do
echo "Processing file: $file"
# Use sed to delete the line directly, with escaping
if ! sed -i "" "/gtag\\('config', 'G-Z0C9Y9EZ60'\\);/d" "$file" 2>> "$log_file"; then
echo "Error processing file: $file" >> "$log_file"
fi
done
echo "Script complete."
# Add GTM
#!/bin/bash
# The script will find all .htm files recursively in the current directory.
# For each file that does not contain "GTM-MNG4TV2S", it will insert
# the provided script tag after the opening <head> tag.
# You can modify this variable to change the starting directory.
SEARCH_DIR="."
# The script tag to be inserted into the files.
SCRIPT_TAG="<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-MNG4TV2S');</script>"
# Find all .htm files, excluding those containing the GTM string.
# Then, insert the script tag after the <head> tag.
find "$SEARCH_DIR" -type f -name "*.htm" -print0 | while IFS= read -r -d $'\0' file; do
if ! grep -q "GTM-MNG4TV2S" "$file"; then
sed -i -e "/<head>/a\\
$SCRIPT_TAG" "$file"
fi
done