Skip to content

Commit a9f794e

Browse files
feat: add yandex metrika
1 parent 2c6d01e commit a9f794e

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.conf import settings
2+
3+
4+
def metrika(request):
5+
"""
6+
Add yandex metrika counter id from settings, so "metrika.html" template can use it.
7+
"""
8+
return {
9+
"YANDEX_METRIKA": settings.YANDEX_METRIKA
10+
}

adminpage/adminpage/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def compose_base_url(schema, hostname, port) -> str:
172172
'django.template.context_processors.request',
173173
'django.contrib.auth.context_processors.auth',
174174
'django.contrib.messages.context_processors.messages',
175+
'adminpage.context_processors.metrika',
175176
],
176177
},
177178
},
@@ -188,6 +189,8 @@ def compose_base_url(schema, hostname, port) -> str:
188189

189190
AUTH_USER_MODEL = 'accounts.User'
190191

192+
YANDEX_METRIKA = os.getenv("YANDEX_METRIKA", None)
193+
191194
# Authentication
192195
OAUTH_CLIENT_ID = os.getenv("oauth_appID", "?")
193196
OAUTH_CLIENT_SECRET = os.getenv("oauth_shared_secret", "?")

adminpage/sport/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<title>{% block title %} {% endblock %}</title>
99
<link rel="stylesheet" href="{% static "sport/css/header.css" %}">
1010
<script src="{% static "sport/js/header.js" %}"></script>
11+
{% include "metrika.html" %}
1112
{% block extrahead %} {% endblock %}
1213
</head>
1314
<body>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% if YANDEX_METRIKA %}
2+
3+
<!-- Yandex.Metrika counter -->
4+
<script type="text/javascript">
5+
(function(m,e,t,r,i,k,a){
6+
m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
7+
m[i].l=1*new Date();
8+
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
9+
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)
10+
})(window, document,'script','https://mc.yandex.ru/metrika/tag.js?id={{ YANDEX_METRIKA }}', 'ym');
11+
12+
ym({{ YANDEX_METRIKA }}, 'init', {ssr:true, webvisor:true, trackHash:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true});
13+
</script>
14+
<noscript><div><img src="https://mc.yandex.ru/watch/{{ YANDEX_METRIKA }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
15+
<!-- /Yandex.Metrika counter -->
16+
17+
<script>
18+
// Attach id and email to Metrika session
19+
ym({{ YANDEX_METRIKA }}, "userParams", {'UserID': {{ user.id }}, 'email': '{{ user.email }}' } );
20+
</script>
21+
22+
{% endif %}

adminpage/templates/admin/base_site.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% load static %}
44

55
{% block extrahead %}
6+
{% include "metrika.html" %}
67
<script type="text/javascript" src="{% static "admin/js/vendor/jquery/jquery.min.js" %}"></script>
78
<script type="text/javascript" src="{% static "admin/js/jquery.init.js" %}"></script>
89

0 commit comments

Comments
 (0)