Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* initial commit

  • Loading branch information...
commit 75534eb66c8ad8040296d599bd35da09ea8f66ca 0 parents
Igor Starikov authored
3  .gitignore
... ... @@ -0,0 +1,3 @@
  1 +*.pyc
  2 +.project
  3 +.pydevproject
27 LICENSE
... ... @@ -0,0 +1,27 @@
  1 +Copyright (c) 2010, django-sitemetrics project
  2 +All rights reserved.
  3 +
  4 +Redistribution and use in source and binary forms, with or without modification,
  5 +are permitted provided that the following conditions are met:
  6 +
  7 + 1. Redistributions of source code must retain the above copyright notice,
  8 + this list of conditions and the following disclaimer.
  9 +
  10 + 2. Redistributions in binary form must reproduce the above copyright
  11 + notice, this list of conditions and the following disclaimer in the
  12 + documentation and/or other materials provided with the distribution.
  13 +
  14 + 3. Neither the name of the django-sitemetrics project nor the names of its
  15 + contributors may be used to endorse or promote products derived from
  16 + this software without specific prior written permission.
  17 +
  18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22 +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25 +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 README.md
Source Rendered
... ... @@ -0,0 +1,44 @@
  1 +SiteMetrics for Django
  2 +=======================================
  3 +http://github.com/idlesign/django-sitemetrics
  4 +
  5 +### What's that ###
  6 +
  7 +django-sitemetrics is a reusable application for Django 1.1+ that offers easy integration with different site metrics service providers.
  8 +
  9 +### Currently supported site metrics providers ###
  10 +
  11 +1. **Yandex Metrika** — http://metrika.yandex.ru/
  12 +Provider alias: 'yandex'
  13 +2. **Google Analytics** — http://www.google.com/analytics/
  14 +Provider alias: 'google'
  15 +
  16 +
  17 +### How to use ###
  18 +
  19 +1. Add the 'sitemetrics' application to 'INSTALLED_APPS' in your settings file (usually 'settings.py')
  20 +2. Add '{% load sitemetrics %}' tag to the top of a template (usually base template, e.g. 'base.html')
  21 +
  22 +Then you have two options that add metrics client code to your page:
  23 +
  24 ++ Use so-called 'four arguments' sitemetrics tag notation:
  25 +**{% sitemetrics by google for "UA-000000-0" %}**
  26 +Here: 'google' — provider alias; 'UA-000000-0' — keycode argument.
  27 +That's how you put Google Analytics client code (with 'UA-000000-0' keycode) into page.
  28 +
  29 ++ Use so-called 'no arguments' sitemetrics tag notation:
  30 +**{% sitemetrics %}**
  31 +That's how you put all client codes registered and active for the current site.
  32 +Client codes are registered with sites through Django Admin site interface.
  33 +*** 'Admin site' and 'Sites' from Django contrib are required for this option to work.**
  34 +*** './manage.py syncdb' is required just once for this option to work (it installs sitemetrics table into database).**
  35 +
  36 +### How to add site metrics providers ###
  37 +
  38 +1. Edit 'sitemetrics/models.py'. Add new provider alias and name into KEYCODE_PROVIDERS list.
  39 +2. Edit 'sitemetrics/templates/sitemetrics/sitemetrics.tpl'. Add another 'ifequal' clause with 'keycode.provider' check against the alias of your new provider.
  40 +
  41 +### Translating django-sitemetrics ###
  42 +
  43 +You can translate application into your language if it is supported by Django.
  44 +For translation tips refer to Django documentation: http://docs.djangoproject.com/en/1.1/topics/i18n/localization/
28 setup.py
... ... @@ -0,0 +1,28 @@
  1 +import os
  2 +from setuptools import setup, find_packages
  3 +from sitemetrics import VERSION
  4 +
  5 +f = open(os.path.join(os.path.dirname(__file__), 'README'))
  6 +readme = f.read()
  7 +f.close()
  8 +
  9 +setup(
  10 + name='django-sitemetrics',
  11 + version=".".join(map(str, VERSION)),
  12 + description='This reusable Django app can help you to site metrics (Yandex Metrika, Google Analytics)',
  13 + long_description=readme,
  14 + author="Igor 'idle sign' Starikov",
  15 + author_email='idlesign@yandex.ru',
  16 + url='http://github.com/idlesign/django-sitemetrics',
  17 + packages=find_packages(),
  18 + classifiers=[
  19 + 'Development Status :: 4 - Beta',
  20 + 'Environment :: Web Environment',
  21 + 'Framework :: Django',
  22 + 'Intended Audience :: Developers',
  23 + 'License :: OSI Approved :: BSD License',
  24 + 'Operating System :: OS Independent',
  25 + 'Programming Language :: Python',
  26 +
  27 + ],
  28 +)
1  sitemetrics/__init__.py
... ... @@ -0,0 +1 @@
  1 +VERSION = (0, 1, 0)
11 sitemetrics/admin.py
... ... @@ -0,0 +1,11 @@
  1 +from sitemetrics.models import Keycode
  2 +from django.contrib import admin
  3 +
  4 +class KeycodeAdmin(admin.ModelAdmin):
  5 + list_display = ('site', 'provider', 'keycode', 'active',)
  6 + list_editable = ('active',)
  7 + search_fields = ['keycode']
  8 + list_filter = ['site', 'provider', 'active']
  9 + ordering = ['site']
  10 +
  11 +admin.site.register(Keycode, KeycodeAdmin)
BIN  sitemetrics/locale/ru/LC_MESSAGES/django.mo
Binary file not shown
58 sitemetrics/locale/ru/LC_MESSAGES/django.po
... ... @@ -0,0 +1,58 @@
  1 +# SiteMetrics reusable Django application russian translation file.
  2 +# Copyright (C) 2010, Igor Starikov aka idle sign
  3 +# This file is distributed under the same license as the Django package.
  4 +# idle sign <idlesign@yandex.ru>, 2010.
  5 +#
  6 +msgid ""
  7 +msgstr ""
  8 +"Project-Id-Version: SiteMetrics\n"
  9 +"Report-Msgid-Bugs-To: \n"
  10 +"POT-Creation-Date: 2010-05-12 11:49+0700\n"
  11 +"PO-Revision-Date: 2010-05-12 11:50+0700\n"
  12 +"Last-Translator: Igor 'idle sign' Starikov <idlesign@yandex.ru>\n"
  13 +"Language-Team: \n"
  14 +"MIME-Version: 1.0\n"
  15 +"Content-Type: text/plain; charset=UTF-8\n"
  16 +"Content-Transfer-Encoding: 8bit\n"
  17 +"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"
  18 +"X-Poedit-Language: Russian\n"
  19 +"X-Poedit-Country: RUSSIAN FEDERATION\n"
  20 +"X-Poedit-SourceCharset: utf-8\n"
  21 +
  22 +#: models.py:6
  23 +msgid "Site"
  24 +msgstr "Сайт"
  25 +
  26 +#: models.py:6
  27 +msgid "Site for which metrics keycode is registered."
  28 +msgstr "Cайт, на который зарегистрирован ключ."
  29 +
  30 +#: models.py:7
  31 +msgid "Provider"
  32 +msgstr "Сервис"
  33 +
  34 +#: models.py:7
  35 +msgid "Metrics service provider name."
  36 +msgstr "Сервис, предоставляющий метрику."
  37 +
  38 +#: models.py:8
  39 +#: models.py:11
  40 +msgid "Keycode"
  41 +msgstr "Ключ"
  42 +
  43 +#: models.py:8
  44 +msgid "Keycode or identifier given by metrics service provider for site(s)."
  45 +msgstr "Ключ (идентификатор) для сайта, выданный сервисом метрики."
  46 +
  47 +#: models.py:9
  48 +msgid "Active"
  49 +msgstr "Используется"
  50 +
  51 +#: models.py:9
  52 +msgid "Whether this keycode is available to use."
  53 +msgstr "Следует ли использовать ключ."
  54 +
  55 +#: models.py:12
  56 +msgid "Keycodes"
  57 +msgstr "Ключи"
  58 +
19 sitemetrics/models.py
... ... @@ -0,0 +1,19 @@
  1 +from django.db import models
  2 +from django.contrib.sites.models import Site
  3 +from django.utils.translation import ugettext_lazy as _
  4 +
  5 +class Keycode(models.Model):
  6 + KEYCODE_PROVIDERS = (
  7 + ('yandex', 'Yandex Metrika'),
  8 + ('google', 'Google Analytics'),
  9 + )
  10 + site = models.ForeignKey(Site, verbose_name=_('Site'), help_text=_('Site for which metrics keycode is registered.'))
  11 + provider = models.CharField(_('Provider'), max_length=50, choices=KEYCODE_PROVIDERS, help_text=_('Metrics service provider name.'))
  12 + keycode = models.CharField(_('Keycode'), max_length=80, help_text=_('Keycode or identifier given by metrics service provider for site(s).'))
  13 + active = models.BooleanField(_('Active'), default=True, help_text=_('Whether this keycode is available to use.'))
  14 + class Meta:
  15 + verbose_name = _('Keycode')
  16 + verbose_name_plural = _('Keycodes')
  17 +
  18 + def __unicode__(self):
  19 + return u'Keycode by %s for %s' % (self.provider, self.keycode)
11 sitemetrics/templates/sitemetrics/sitemetrics.tpl
... ... @@ -0,0 +1,11 @@
  1 +{% for keycode in keycodes %}
  2 + {% ifequal keycode.provider "yandex" %}
  3 + <script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript"></script>
  4 + <div style="display:none;"><script type="text/javascript">try { var yaCounter{{ keycode.keycode }} = new Ya.Metrika({{ keycode.keycode }}); } catch(e){}</script></div>
  5 + <noscript><div style="position:absolute"><img src="//mc.yandex.ru/watch/{{ keycode.keycode }}" alt="" /></div></noscript>
  6 + {% endifequal %}
  7 + {% ifequal keycode.provider "google" %}
  8 + <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>
  9 + <script type="text/javascript">try{var pageTracker = _gat._getTracker("{{ keycode.keycode }}");pageTracker._trackPageview();} catch(err) {}</script>
  10 + {% endifequal %}
  11 +{% endfor %}
0  sitemetrics/templatetags/__init__.py
No changes.
53 sitemetrics/templatetags/sitemetrics.py
... ... @@ -0,0 +1,53 @@
  1 +import urllib, hashlib
  2 +from django import template
  3 +from django.db import models
  4 +from django.contrib.sites.models import Site
  5 +
  6 +register = template.Library()
  7 +keycode = models.get_model('sitemetrics', 'Keycode')
  8 +
  9 +@register.tag
  10 +def sitemetrics(parser, token):
  11 + """
  12 + Parses sitemetrics tag.
  13 +
  14 + Two notation types are possible:
  15 + 1. No arguments:
  16 + {% sitemetrics %}
  17 + Used to render all metric counters registered and active for current site.
  18 + This requires 'Admin site' and 'Sites' from Django contrib.
  19 +
  20 + 2. Four arguments:
  21 + {% sitemetrics by yandex for "138500" %}
  22 + Used to render custom metric counter with custom counter-id.
  23 + This is a simple template tag with no special requirements.
  24 +
  25 + """
  26 + tokens = token.split_contents()
  27 + tokensNum = len(tokens)
  28 +
  29 + if tokensNum == 1:
  30 + # Notation Type 1
  31 + currentSite = Site.objects.get_current()
  32 + keycodes = currentSite.keycode_set.filter(active=True)
  33 + elif tokensNum == 5:
  34 + # Notation Type 2
  35 + if tokens[1] == 'by' and tokens[3] == 'for':
  36 + keycodes = [{ 'provider':tokens[2], 'keycode':tokens[4][1:-1] },]
  37 + else:
  38 + raise template.TemplateSyntaxError, "A five argument notation of %r tag should look like {%% sitemetrics by yandex for \"138500\" %%}." % tokens[0]
  39 + else:
  40 + raise template.TemplateSyntaxError, "%r tag requires four or no arguments. E.g. {%% sitemetrics by yandex for \"138500\" %%} or {%% sitemetrics %%}." % tokens[0]
  41 +
  42 + return sitemetricsNode(keycodes)
  43 +
  44 +class sitemetricsNode(template.Node):
  45 + """Renders specified site metric counter from template."""
  46 +
  47 + def __init__(self, keycodes):
  48 + self.keycodes = keycodes
  49 + self.template = template.loader.get_template('sitemetrics/sitemetrics.tpl')
  50 +
  51 + def render(self, context):
  52 + myContext = template.Context({'keycodes': self.keycodes,})
  53 + return self.template.render(myContext)

0 comments on commit 75534eb

Please sign in to comment.
Something went wrong with that request. Please try again.