Skip to content

Commit

Permalink
feat(js): Only load GA/Disqus when they are setted. Close #45
Browse files Browse the repository at this point in the history
  • Loading branch information
oswaldoacauan committed May 26, 2014
1 parent 1b7936a commit 0872d9d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
40 changes: 28 additions & 12 deletions src/assets/js/scripts.js
Expand Up @@ -39,9 +39,11 @@

_prismHandler();

// Reset DISQUS
// DISQUS Handlers
// =================
var _resetDisqus = function() {
var _disqusHandler = function() {
if(!GHOSTIUM.haveDisqus) return;

if(typeof DISQUS === 'object' && $('#disqus_thread').length) {
DISQUS.reset({
reload: true,
Expand All @@ -52,7 +54,12 @@
}
};

var _loadDisqusCounter = function() {
var _disqusCounterHandler = function() {
if(!GHOSTIUM.haveDisqus) {
$('[data-disqus-identifier]').parent('li').remove();
return;
}

if(typeof DISQUSWIDGETS === 'object') {
var newScript = document.createElement("script"),
countScript = $html.find('head script[src*="disqus.com/count-data.js"]').remove(),
Expand All @@ -76,6 +83,19 @@
}
};

_disqusCounterHandler()

// GA Handler
// =================
var _gaHandler = function() {
if(!GHOSTIUM.haveGA) return;

if(typeof ga === 'function') {
ga('set', 'location', window.location.href);
ga('send', 'pageview');
}
}

// PJax bindings
// =================
if ($.support.pjax) {
Expand All @@ -85,19 +105,15 @@
});

$document.on('pjax:end', function() {
if(typeof ga === 'function') {
ga('set', 'location', window.location.href);
ga('send', 'pageview');
}

_resetDisqus();
_loadDisqusCounter();
_disqusHandler();
_gaHandler();
_disqusCounterHandler();
_prismHandler();

$('[data-load-image]', $content).each(function() {
ImageLoader.load($(this));
});

_prismHandler();
NProgress.done();
});

Expand Down Expand Up @@ -214,7 +230,7 @@
// Fix DISQUS iframe does not resize on mobile orientation change
// =================
$window.on('orientationchange', function(e) {
_resetDisqus();
_disqusHandler();
});

});
Expand Down
39 changes: 25 additions & 14 deletions src/default.hbs
Expand Up @@ -43,6 +43,14 @@
<script src="/assets/_components/modernizr/modernizr.js"></script>
<!-- endbuild -->

<script type="text/javascript">
{{> custom/config}}
window.GHOSTIUM = {};
GHOSTIUM.haveGA = typeof ga_ua !== 'undefined' && ga_ua !== 'UA-XXXXX-X';
GHOSTIUM.haveDisqus = typeof disqus_shortname !== 'undefined' && disqus_shortname !== 'example';
</script>

{{ghost_head}}
</head>
<body class="{{body_class}}">
Expand Down Expand Up @@ -97,21 +105,24 @@
<!-- endbuild -->

<script type="text/javascript">
{{> custom/config}}
(function(g,h,o,s,t,z){g.GoogleAnalyticsObject=s;g[s]||(g[s]=
function(){(g[s].q=g[s].q||[]).push(arguments)});g[s].s=+new Date;
t=h.createElement(o);z=h.getElementsByTagName(o)[0];
t.src='//www.google-analytics.com/analytics.js';
z.parentNode.insertBefore(t,z)}(window,document,'script','ga'));
ga('create',ga_ua);ga('send','pageview');
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
if(GHOSTIUM.haveGA) {
(function(g,h,o,s,t,z){g.GoogleAnalyticsObject=s;g[s]||(g[s]=
function(){(g[s].q=g[s].q||[]).push(arguments)});g[s].s=+new Date;
t=h.createElement(o);z=h.getElementsByTagName(o)[0];
t.src='//www.google-analytics.com/analytics.js';
z.parentNode.insertBefore(t,z)}(window,document,'script','ga'));
ga('create',ga_ua);ga('send','pageview');
}
if(GHOSTIUM.haveDisqus) {
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
}
</script>
</body>
</html>
24 changes: 13 additions & 11 deletions src/post.hbs
Expand Up @@ -91,18 +91,20 @@
</section>

<script type="text/javascript">
{{> custom/config}}
{{#post}}
if(GHOSTIUM.haveDisqus) {
{{#post}}
var disqus_identifier = '{{id}}';
{{/post}}
{{/post}}
if(typeof DISQUS !== 'object') {
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
if(typeof DISQUS !== 'object') {
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
}
} else {
document.querySelector('.post-comments').remove();
}
</script>

0 comments on commit 0872d9d

Please sign in to comment.