Search code examples
google-analyticsapostrophe-cms

Best place to add analytics in ApostropheCMS


I am trying to get tracking in place and was wondering what will be the best place to add the JS code. Also need to make sure that is it not getting minified?


Solution

  • I would suggest extending outerLayout.html in lib/modules/apostrophe-templates/views/outerLayout.html. In this template, you can modify the extraHead block like so:

    {% extends "outerLayoutBase.html" %}
    
    {% block extraHead %}
      <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', '...']);
        _gaq.push(['_trackPageview']);
        (function() {
          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
      </script>
    {% endblock %}
    

    This is the block where I typically put things like favicon definitions, open graph meta tags, remote font loading, and all that good stuff.