How to install Google Analytics and Disqus in Ghost in a better way

Include Diqus and Google Analytics is very easy, just copy and past each script in default.hbs each one in their position.

But it's not the better way to keep the code simple and well structured.

Disqus and Google Analytics are both script, you can move each script in a new file: google_analytics.js and disqus.js in the content/themes/casper/assets/js/ folder of the theme you are using, and then load the google_analytics.js script from default.hbs and disqus.js script from post.hbs.

$ cd assets/js
$ touch google_analytics.js disqus.js

Now copy and paste each script inside the respective file.

file: google_analytics.js

(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', 'your-tracking-code', 'auto');  
ga('send', 'pageview');  

file: disqus.js

var disqus_shortname = 'your-shortname'; // required: replace example with your forum shortname  
var disqus_identifier = '{{post.id}}';

/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();

Do not forget to replace the Google Analytics tracking code and Disqus shortname with your.

Now in default.hbs load the google_analytics.js file just before the closed </head> tag

{{! Ghost outputs important style and meta data with this tag }}
    {{ghost_head}}

<script src="{{asset "js/google_analytics.js"}}"></script>  
</head>  

In post.hbs just before the closed </main> tag, copy and past the following code

    <div id="disqus_thread"></div>
    <script src="{{asset "js/disqus.js"}}"></script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    </article>

</main>  
Show comment counts

To show comments count for each post, edit post.hbs file and find the post-meta class <section class="post-meta"> and change it to match the following code

<section class="post-meta">  
    <time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time> {{tags prefix=" on "}} <a href="{{url}}#disqus_thread">Comments</a>
</section>