add_google_analytics.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. # Copyright 2018 The gRPC Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script finds all html files in the current directory, and adds the
  16. # GA tracking snippet to them.
  17. read -r -d '' SNIPPET << EOF
  18. <script type="text/javascript">
  19. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  20. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  21. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  22. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  23. ga('create', 'UA-60127042-1', 'auto');
  24. ga('send', 'pageview');
  25. </script>
  26. EOF
  27. S=$(echo -n "$SNIPPET" | tr '\n' ' ')
  28. while IFS= read -r -d '' M
  29. do
  30. if grep -q "i,s,o,g,r,a,m" "$M"; then
  31. :
  32. else
  33. sed -i "s_</head>_${S}</head>_" "$M"
  34. fi
  35. done < <(find . -name \*.html -print0)