• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Lack of interest A better template for Google Analytics, current cannot be extended

Rigel Kentaurus

Well-known member
#1
The template used for Google Analytics, while good, is not extensible. A better one would be this

Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
 
    var _gaq = [['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}']];
    <xen:hook name="google_analytics_before" />   
    _gaq.push(['_trackPageview']);
    <xen:hook name="google_analytics_after" />
 
    !function(d, t)
    {
        var g = d.createElement(t),
            s = d.getElementsByTagName(t)[0];   
        g.async = true;
        g.src = ('https:' == d.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g, s);
    }
    (document, 'script');
 
    </script><xen:comment><!--Adapted from http://mathiasbynens.be/notes/async-analytics-snippet--></xen:comment></xen:if>
This template adds two hooks before and after _trackPageview, this is necessary because there are some operations that need to be done before trackPageView (like custom vars for segments), and some that can be done after (like pageSpeed)