Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Liveinternet #223

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions analytical/templatetags/liveinternet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from django.template import Library, Node

from analytical.utils import is_internal_ip, disable_html

LIVEINTERNET_WITH_IMAGE = """
<a href="https://www.liveinternet.ru/click"
target="_blank"><img id="licnt515E" width="31" height="31" style="border:0"
title="LiveInternet"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7"
alt=""/></a><script>(function(d,s){d.getElementById("licnt515E").src=
"https://counter.yadro.ru/hit?t50.6;r"+escape(d.referrer)+
((typeof(s)=="undefined")?"":";s"+s.width+"*"+s.height+"*"+
(s.colorDepth?s.colorDepth:s.pixelDepth))+";u"+escape(d.URL)+
";h"+escape(d.title.substring(0,150))+";"+Math.random()})
(document,screen)</script>
"""

LIVEINTERNET_CODE = """
<script>
new Image().src = "https://counter.yadro.ru/hit?r"+
escape(document.referrer)+((typeof(screen)=="undefined")?"":
";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?
screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+
";h"+escape(document.title.substring(0,150))+
";"+Math.random();
</script>
"""
LIVEINTERNET_IMAGE = """
<a href="https://www.liveinternet.ru/click"
target="_blank"><img src="https://counter.yadro.ru/logo?50.6"
title="LiveInternet"
alt="" style="border:0" width="31" height="31"/>
</a>
"""

register = Library()


@register.tag
def liveinternet(parser, token):
"""
Body Liveinternet, full image and code template tag.

Render the body Javascript code and image for Liveinternet.
"""
return LiveInternetNode(LIVEINTERNET_WITH_IMAGE, 'liveinternet_with_image')


@register.tag
def liveinternet_code(parser, token):
"""
Top Liveinternet,code template tag.

Render the top Javascript code for Liveinternet.
"""
return LiveInternetNode(LIVEINTERNET_CODE, 'liveinternet_code')


@register.tag
def liveinternet_img(parser, token):
"""
Body Liveinternet image template tag.

Render the body Javascript code for Liveinternet.
"""
return LiveInternetNode(LIVEINTERNET_IMAGE, 'liveinternet_image')


class LiveInternetNode(Node):
def __init__(self, key, name):
self.key = key
self.name = name

def render(self, context):
if is_internal_ip(context):
return disable_html(self.key, self.name)
return LIVEINTERNET_CODE
61 changes: 61 additions & 0 deletions docs/services/liveinternet.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
==================================
Liveinternet -- traffic analysis
==================================


`Liveinternet`_ is an analytics tool like as google analytics.

.. _`Liveinternet`: https://www.liveinternet.ru/code/


.. yandex-metrica-installation:

Installation
============

To start using the Liveinternet integration, you must have installed the
django-analytical package and have added the ``analytical`` application
to :const:`INSTALLED_APPS` in your project :file:`settings.py` file.
See :doc:`../install` for details.

Next you need to add the Liveinternet template tag to your templates.

The Liveinternet counter code is inserted into templates using a template
tag. Load the :mod:`liveinternet` template tag library and insert the
:ttag:`liveinternet` tag. To display as a single image combining a counter
and the LiveInternet logo::

{% load liveinternet %}
<html>
<head>
...
{% liveinternet %}
</head>
...

In the form of two images, one of which is a counter (transparent GIF size 1x1),
odi1n marked this conversation as resolved.
Show resolved Hide resolved
and the other is the LiveInternet logo. This placement method will allow you to
insert the code of the invisible counter at the beginning of the page, and the
logo - where the design and content of the page allows. ::

{% load liveinternet %}
<html>
<head>
...
{% liveinternet_code %}
</head>
<body>
...
{% liveinternet_img %}
...
</body>


Internal IP addresses
---------------------

Usually you do not want to track clicks from your development or
internal IP addresses. It takes the value of
:const:`ANALYTICAL_INTERNAL_IPS` by default (which in turn is
:const:`INTERNAL_IPS` by default). See :ref:`identifying-visitors` for
important information about detecting the visitor IP address.
58 changes: 58 additions & 0 deletions tests/unit/test_tag_liveinternet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Tests for the LiveInternet template tags and filters.
"""

from django.http import HttpRequest
from django.template import Context
from django.test.utils import override_settings

from analytical.templatetags.liveinternet import (LiveInternetNode,
LIVEINTERNET_WITH_IMAGE,
LIVEINTERNET_CODE,
LIVEINTERNET_IMAGE)
from utils import TagTestCase


class LiveInternetTagTestCase(TagTestCase):
"""
Tests for the ``liveinternet`` template tag.
"""

def test_render_liveinternet(self):
response = self.render_tag('liveinternet', 'liveinternet')
assert '<a href="https://www.liveinternet.ru/click"' in response

def test_render_liveinternet_code(self):
response = self.render_tag('liveinternet', 'liveinternet_code')
assert 'new Image().src = "https://counter.yadro.ru/hit?r"' in response

def test_render_liveinternet_img(self):
response = self.render_tag('liveinternet', 'liveinternet_img')
assert '<a href="https://www.liveinternet.ru/click"' in response

@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
def test_render_liveinternet_render_ip(self):
req = HttpRequest()
req.META['REMOTE_ADDR'] = '1.1.1.1'
context = Context({'request': req})
r = LiveInternetNode(LIVEINTERNET_WITH_IMAGE, 'liveinternet_with_image').render(context)
assert r.startswith('<!-- liveinternet disabled on internal IP address')
assert r.endswith('-->')

@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
def test_render_liveinternet_code_render_ip(self):
req = HttpRequest()
req.META['REMOTE_ADDR'] = '1.1.1.1'
context = Context({'request': req})
r = LiveInternetNode(LIVEINTERNET_CODE, 'liveinternet_code').render(context)
assert r.startswith('<!-- liveinternet_code disabled on internal IP address')
assert r.endswith('-->')

@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
def test_render_liveinternet_img_render_ip(self):
req = HttpRequest()
req.META['REMOTE_ADDR'] = '1.1.1.1'
context = Context({'request': req})
r = LiveInternetNode(LIVEINTERNET_IMAGE, 'liveinternet_image').render(context)
assert r.startswith('<!-- liveinternet_img disabled on internal IP address')
assert r.endswith('-->')