Home Blog Django Googlytics
Post
Cancel

Django Googlytics

I present to you django-googlytics, yeay!!

I needed to add the google analytics for a django project, but none of the already existing solution was up to date or good enough for my usage. So I decided to wrote googlytics by merging a couple of good answers from this StackOverflow Q&A. The result is the following context_processor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def googlytics(request):
    """Return google analytics code."""
    context = {'googlytics_code': ''}

    ignore = (getattr(settings, 'GOOGLE_ANALYTICS_IGNORE_ADMIN', False)
              and request.user.is_authenticated()
              and request.user.is_staff)

    if not ignore and settings.GOOGLE_ANALYTICS_KEY:
        snippet = render_to_string('googlytics/googlytics.html',
                                   {'google_analytics_key':
                                    settings.GOOGLE_ANALYTICS_KEY})
        context['googlytics_code'] = snippet

    return context

Simple, settable, documented (Readme file), tested, installable from pip and up to date with the google analytics snippet :)

Other alternatives didn’t work for me, but here’s a list at best of knowledge.

This post is licensed under CC BY 4.0 by the author.

My Experience of Django vs. Rails

An Echo-Sphere present

Comments powered by Disqus.