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

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

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 = '{{}}';

(function() {
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = '//' + disqus_shortname + '';
    (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 }}

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

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="">comments powered by Disqus.</a></noscript>
    <a href="" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

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>