-
-
Save drakakisgeo/56ae5fbfdb564cf1fdd0 to your computer and use it in GitHub Desktop.
Add Google Analytics and FB tracking to html files
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/sh | |
FBCODE_ID='xxxxxxxxxx' | |
GACODE='xxxxxxxx' | |
FBBLOCK=$(cat <<EOF | |
<!-- Facebook Pixel Code --> | |
<script> | |
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? | |
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; | |
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; | |
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, | |
document,'script','\/\/connect.facebook.net\/en_US\/fbevents.js'); | |
fbq('init', '${FBCODE_ID}'); | |
fbq('track', "PageView"); | |
<\/script> | |
<noscript><img height="1" width="1" style="display:none" | |
src="https:\/\/www.facebook.com\/tr?id=${FBCODE_ID}&ev=PageView&noscript=1" | |
\/><\/noscript> | |
<!-- End Facebook Pixel Code --> | |
EOF | |
) | |
GABLOCK=$(cat <<EOF | |
<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', '${GACODE}', 'auto'); | |
ga('send', 'pageview'); | |
<\/script> | |
EOF | |
) | |
####################################################3 | |
REPLACER=`echo $FBBLOCK $GABLOCK` | |
for file in $(find . -name '*.html' -or -name '*.htm' -or -name '*.php'); | |
do | |
if ! grep -q ${GACODE} "$file"; then | |
sed -e "s/<\/head>/${REPLACER}\\n\\r<\/head>/ig" $file > /tmp/tempfile.tmp | |
sudo mv /tmp/tempfile.tmp $file | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment