Last active
December 24, 2015 05:09
-
-
Save JCotton1123/6748919 to your computer and use it in GitHub Desktop.
Add Google analytics snippet to files with the ability to filter on file extension and mime type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is the snippet that will be added to each file | |
includeGAString="<!--#include virtual=\"/includes/ga.inc\" -->" | |
#includeGAString="<script> | |
# (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
# (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
# m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
# })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
# | |
# ga('create', 'UA-XXXXXXX-XX', 'example.com'); | |
# ga('send', 'pageview'); | |
# | |
#</script>" | |
## Functions | |
processFile() { | |
local file="$1" | |
filterFile "$file" | |
case $? in | |
1) | |
[ $v -eq 1 ] && echo "Skipping $file due to mime type" | |
return 1 | |
;; | |
2) | |
[ $v -eq 1 ] && echo "Skipping $file due to extention" | |
return 2 | |
;; | |
?) | |
;; | |
esac | |
[ $v -eq 1 ] && echo "Updating $file" | |
if [ $trialRun -eq 0 ]; then | |
sed -i "s/\(\s*\)<body[^>]*>/&\n\1$includeGAString\n/g" "$file" | |
fi | |
return 0 | |
} | |
filterFile() { | |
local file="$1" | |
# Mime check | |
fileMimeType=$(file -ib "$file") | |
if [ "$includeFilesWithMimeType" != "" ]; then | |
fileMimeType=$(file -ib "$file") | |
passableMimeType=0 | |
for mimeType in $includeFilesWithMimeType; do | |
`echo $fileMimeType | grep "$mimeType" >/dev/null` | |
if [ $? -eq 0 ]; then | |
passableMimeType=1 | |
break; | |
fi | |
done | |
if [ $passableMimeType -eq 0 ]; then | |
return 1 | |
fi | |
fi | |
# Check file's extension | |
fileExt=$(echo "${file##*.}") | |
if [ "$includeFilesWithExt" != "" ]; then | |
passableExt=0 | |
for ext in $includeFilesWithExt; do | |
if [ "$fileExt" == "$ext" ]; then | |
passableExt=1 | |
break; | |
fi | |
done | |
if [ $passableExt -eq 0 ]; then | |
return 2 | |
fi | |
fi | |
if [ "$excludeFilesWithExt" != "" ]; then | |
for ext in $excludeFilesWithExt; do | |
if [ "$fileExt" == "$ext" ]; then | |
return 2 | |
fi | |
done | |
fi | |
listOfMimeTypes+=("$fileMimeType") | |
listOfExts+=("$fileExt") | |
return 0 | |
} | |
sedEscape(){ | |
echo $1 | sed -e 's/[]\/()$*.^|[]/\\&/g' | |
} | |
uniqArray(){ | |
arry=("$@") | |
echo $(printf "%s\n" "${arry[@]}" | sort -u) | |
} | |
usage() { | |
echo -e "usage: add-ga.sh [-hmtxv] [dir]" | |
echo -e " -i, Only process files with one of the supplied extensions. ex: \"html html\"" | |
echo -e " -m, Only process files with one of the supplied mime types. ex: \"text/html text/plain\"" | |
echo -e " -t, Trial run. Don't actually insert the GA snippet." | |
echo -e " -x, Exclude files with any of the supplied extensions. ex: \"xml dita\"" | |
echo -e " -v, Verbose. Print list of files that will be updated or skipped" | |
} | |
## Main | |
trialRun=0 | |
includeFilesWithExt="" | |
excludeFilesWithExt="" | |
includeFilesWithMimeType="" | |
v=0 | |
while getopts ":hi:m:tvx:" option; do | |
case $option in | |
h) | |
usage | |
exit 0 | |
;; | |
i) | |
includeFilesWithExt=$OPTARG | |
;; | |
m) | |
includeFilesWithMimeType=$OPTARG | |
;; | |
t) | |
trialRun=1 | |
;; | |
x) | |
excludeFilesWithExt=$OPTARG | |
;; | |
v) | |
v=1 | |
;; | |
?) | |
usage | |
exit 1 | |
esac | |
done | |
shift $(($OPTIND -1)) | |
fileDirectoryFilter=$@ | |
if [ -z $fileDirectoryFilter ]; then | |
usage | |
exit 1 | |
fi | |
listOfExts=() | |
listOfMimeTypes=() | |
fileCountSkippedExt=0 | |
fileCountSkippedMimeType=0 | |
fileCountUpdated=0 | |
includeGAString=$(sedEscape "$includeGAString") | |
# Fix looping for filenames with spaces | |
# Use newline as delimiter | |
oIFS=$IFS | |
nIFS=$(echo -en "\n\b") | |
IFS=$nIFS | |
for file in $(grep -R -l "<body" "$fileDirectoryFilter"); do | |
IFS=$oIFS | |
processFile "$file" | |
case $? in | |
0) | |
let "fileCountUpdated+=1" | |
;; | |
1) | |
let "fileCountSkippedMimeType+=1" | |
;; | |
2) | |
let "fileCountSkippedExt+=1" | |
;; | |
esac | |
IFS=$nIFS | |
done | |
# Print stats | |
echo "Files Updated: $fileCountUpdated" | |
echo "Files excluded by ext: $fileCountSkippedExt" | |
echo "Files excluded by mime-type: $fileCountSkippedMimeType" | |
echo -n "Extensions: " | |
uniqArray "${listOfExts[@]}" | |
echo -n "Mime Types: " | |
uniqArray "${listOfMimeTypes[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment