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?
Asked
Active
Viewed 366 times
2 Answers
2
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.

Alex Gilbert
- 143
- 5
-
looks like this works for me. It will be nice to see the JS code at the end of the page instead of the beginning. – Parik Tiwari Nov 30 '16 at 03:39
-
The end of the page is easy too. Take a look at the blocks in `outerLayoutBase` that you can override, notably `afterMain` and `extraBody`. – Tom Boutell Dec 03 '16 at 17:07
0
FYI - may be a good idea to do the analytics ID (etc) as a global so your user can set it there self. Note that this will impact performance a little as it'll be requested for each page, but it can be really useful. And don't forget to wrap the script in {% if data.global.analyticsID %}
!

bionara
- 228
- 1
- 10