Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mkamrani
Created May 30, 2023 01:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkamrani/a159516f73db0e8adf10e5d88ec2a370 to your computer and use it in GitHub Desktop.
Save mkamrani/a159516f73db0e8adf10e5d88ec2a370 to your computer and use it in GitHub Desktop.
Add Google Analytics to all HTML files in a directory
#!/bin/bash
# From: dotenx.com
# description:
# Add Google Analytics to all HTML files in a directory. Google Analytics code should be in a file called google-analytics in the same directory as this script.
# usage:
# ./add-gtag.sh directory
# Tested on Mac-zsh
directory="$1"
if [ -d "$directory" ]; then
for file in "$directory"/*.html; do
if [[ -f "$file" ]]; then
sed -i.bak '/<head>/r google-analytics' $file
fi
done
else
echo "Directory not found."
fi
rm $directory/*.bak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment