PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ga.en.rst

https://bitbucket.org/lbesson/web-sphinx
ReStructuredText | 86 lines | 56 code | 30 blank | 0 comment | 0 complexity | 5d5b8ea4a653673977e0c975d9064fa4 MD5 | raw file
  1. .. meta::
  2. :description lang=en: How to use Google Analytics in a Sphinx project
  3. :description lang=fr: Utiliser Google Analytics dans un projet Sphinx
  4. #################################################
  5. How to use Google Analytics in a Sphinx project
  6. #################################################
  7. .. warning:: I no longer advise to use Google Analytics, and I have entirely removed it from my webpages, and I will no longer update these tutorials. As they would still be available from Google Cache or Archive.org websites, I prefer to keep this page online.
  8. .. note:: This short page is focusing on *Google Analytics*
  9. For more information, it might be useful to consult first one of these pages:
  10. * `en.wikipedia.org/wiki/Google_Analytics <https://en.wikipedia.org/wiki/Google_Analytics>`_,
  11. * `www.google.com/analytics <https://www.google.com/analytics/>`_.
  12. .. note:: This short page is also focusing on Sphinx
  13. Therefore, for more information, it might be useful to have in mind how Sphinx works,
  14. and in particular how to use HTML templating support `sphinx-doc.org/templating.html <http://sphinx-doc.org/templating.html>`_.
  15. « Vanilla » Google Analytics code
  16. ---------------------------------
  17. The default code to add in every page is the following piece of JavaScript:
  18. .. code-block:: 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-38514290-15', 'ga-beacon.appspot.com');
  24. ga('send', 'pageview');
  25. The first and intuitive solution is, *of course*, to modify the main *template* document
  26. (usually, it should be ``_templates/layout.html`` ou ``.templates/layout.html``)
  27. and to add it this code, between two tags ``<script type="text/javascript">`` and ``</script>``.
  28. If possible, you have to add this piece of code at *the end of the page*, to reduce the page's loading time.
  29. A smaller piece of code
  30. -----------------------
  31. *(And also easier to maintain !)*
  32. You just have to create a small ``ga.js`` script contenaning
  33. the previous piece of code, and add it somewhere on your website.
  34. For instance: `ga.js <http://perso.crans.org/besson/_static/ga.js>`_.
  35. Then you have to load it in every webpage with one-line of ``HTML`` code
  36. at the end of the file (in the ``<bottom>`` paragraph or at the end of ``<body>``) :
  37. .. code-block:: html
  38. <script type="text/javascript" async src="http://perso.crans.org/besson/_static/ga.js"></script>
  39. Moreover, the ``async`` tag will improve the speed of the page, by
  40. loading asynchronously the ``ga.js`` script. Awesome!
  41. How and where adding it
  42. -----------------------
  43. For example, you can take a look at my `layout.html <https://bitbucket.org/lbesson/web-sphinx/src/master/.templates/layout.html#cl-290>`_.
  44. ------------------------------------------------------------------------------
  45. Other solutions ?
  46. -----------------
  47. Sphinx has an extension for Google Analytics, see `on the list of extensions here <http://sphinx-doc.org/latest/develop.html#extensions>`_ (`this folder of the sphinx-contrib repository <https://bitbucket.org/birkenfeld/sphinx-contrib/src/default/googleanalytics/>`_).
  48. It's quick and simple, and working well, and for example I am using it for small Sphinx projects, see for examples: `<infoMP/TPs/solution>`_, `<infoMP/DSs/solution>`_ and `<infoMP/TDs/solution>`_.
  49. .. seealso::
  50. GA-Beacon (`<beacon.html>`_)
  51. How to use a *one-pixel* image to use Google Analytics
  52. to measure the audience of a page, or an email.
  53. Statistics for Google Analytics (`<stats-google-analytics.en.html>`_)
  54. A small sum-up of the use of Google Analytics on this website (from the last 3 years),
  55. and some plots to illustrate the visits and visitors this website got.
  56. .. (c) Lilian Besson, 2011-2021, https://bitbucket.org/lbesson/web-sphinx/