1. google’s badly document analytics megafeature

    Google analytics is a hell of a lot more useful when you realise that you can insert completely arbitrary extra data into  page tracking using the barely-documented custom variable function. You can set this little magic guy in your javascript tracking code:

    _setVar(newVal)

    Sets or defines a custom visitor segment with the supplied string. You can use this value to provide additional segmentation on users to your website. For example, you could have a login page or a form that triggers a value based on a visitor’s input, such as a preference the visitor chooses, or a privacy option. This variable is then updated in the cookie for that visitor. When implemented on your site and data is collected via this method, the newly defined segment appears in the User Defined reports in the Visitors section of the Analytics Reports. Additionally, you can access the User Defined Value segment in the Content Detail report to see which percentage of visitors to a page belong to a particular segment that you define.

    Here’s a django templatetag to make use of that, for your convenience.

    from django import template
    from django.conf import settings
    
    from django.template import Context, loader
    
    register = template.Library()
    
    @register.simple_tag
    def analytics(google_analytics_code="", extra_var=""):
        if google_analytics_code.strip():
            t = loader.get_template ('analytics.html')
            c = Context({
                'google_analytics_code': google_analytics_code,
                'extra_var' : extra_var.strip(),
            })
            return t.render(c)
        else:
            return ""
    
    
    """
    in templates/analytics.html   paste the following
    
    <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
      var pageTracker = _gat._getTracker("{{ google_analytics_code }}");
      {% if extra_var %}
      pageTracker._setVar('{{ extra_var }}');
      {% endif %}
      pageTracker._trackPageview();
    </script>
    
    """