Skip to content

Commit 0186a62

Browse files
renzonrenzon
authored andcommitted
Implemented PostHog identification on frontend
close #2991
1 parent eb6ac82 commit 0186a62

9 files changed

Lines changed: 191 additions & 76 deletions

File tree

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ django-two-factor-auth = "*"
3535
django-recaptcha = "*"
3636
django-pagarme = "*"
3737
django-redis = "*"
38+
posthog = "*"
3839

3940
[dev-packages]
4041
faker = "*"

Pipfile.lock

Lines changed: 139 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import posthog
12
import pytest
23
from faker import Faker
34
from model_bakery import baker
@@ -249,4 +250,12 @@ def turn_ssl_rediret_off_for_tests(settings):
249250
settings.SECURE_SSL_REDIRECT = False
250251

251252

253+
@pytest.fixture(autouse=True)
254+
def turn_posthog_off_for_tests(settings):
255+
"""
256+
There is no need to place secure=True in all client requests
257+
"""
258+
posthog.disabled = True
259+
260+
252261
pytest_plugins = ['pythonpro.modules.tests.test_topics_view']

contrib/env-sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PAGARME_CRYPTO_KEY=
1010

1111
CHAVE_PAGARME_CRIPTOGRAFIA_PUBLICA=ak_test_6yd4kbaJrWzdn61m4De5yzn7jZuTt9
1212
CHAVE_PAGARME_API_PRIVADA=
13+
POSTHOG_API_URL=
14+
POSTHOG_API_KEY=
15+
1316

1417

1518
# Amazon S3 configuration

pythonpro/analytics/apps.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import posthog
12
from django.apps import AppConfig
3+
from django.conf import settings
24

35

46
class AnalyticsConfig(AppConfig):
5-
name = 'analytics'
7+
name = 'pythonpro.analytics'
8+
verbose_name = 'Analytics'
9+
10+
def ready(self):
11+
posthog.api_key = settings.POSTHOG_API_KEY
12+
posthog.api_host = settings.POSTHOG_API_URL
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.conf import settings
2+
3+
4+
def posthog_configurations(request):
5+
return {
6+
'POSTHOG_API_KEY': settings.POSTHOG_API_KEY,
7+
'POSTHOG_API_URL': settings.POSTHOG_API_URL,
8+
}

pythonpro/core/templates/core/base.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ <h3 class="mt-3">Entre em contato</h3>
137137
{% endblock footer %}
138138

139139
{% block bottom_scripts %}{% endblock %}
140+
{% if user.is_authenticated %}
141+
<script type="application/javascript">
142+
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
143+
posthog.init('{{POSTHOG_API_KEY}}',{
144+
api_host:'{{POSTHOG_API_URL}}',
145+
loaded: function(posthog) {
146+
posthog.identify('{{ user.id }}');
147+
posthog.people.set({email: '{{ user.email }}'})
148+
}
149+
})
150+
</script>
140151

152+
{% else %}
153+
<script type="application/javascript">
154+
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
155+
posthog.init('{{POSTHOG_API_KEY}}',{api_host:'{{POSTHOG_API_URL}}'})
156+
</script>
157+
{% endif %}
141158
</body>
142159
</html>

pythonpro/pages/tests/test_bootcamp_vip_landing_page.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from unittest import mock
22

33
import pytest
4-
54
from django.urls import reverse
65

7-
from pythonpro.django_assertions import dj_assert_contains, dj_assert_not_contains
6+
from pythonpro.django_assertions import dj_assert_contains
87

98

109
@pytest.fixture
@@ -45,9 +44,6 @@ def test_should_load_name_and_email_from_GET_when_user_is_logged(client_with_use
4544
dj_assert_contains(resp, 'moacir@python.pro.br')
4645
dj_assert_contains(resp, '(11) 99999-9999')
4746

48-
dj_assert_not_contains(resp, logged_user.first_name)
49-
dj_assert_not_contains(resp, logged_user.email)
50-
5147

5248
@pytest.fixture
5349
def create_or_update_with_no_role(mocker):

pythonpro/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
'pythonpro.core.context_processors.global_settings',
144144
'pythonpro.modules.context_processors.global_settings',
145145
'pythonpro.cohorts.context_processors.global_settings',
146+
'pythonpro.analytics.context_processors.posthog_configurations',
146147
],
147148
},
148149
},
@@ -308,3 +309,7 @@
308309

309310
# Hotzapp configuration
310311
HOTZAPP_API_URL = config('HOTZAPP_API_URL')
312+
313+
# PostHog configuration
314+
POSTHOG_API_URL = config('POSTHOG_API_URL')
315+
POSTHOG_API_KEY = config('POSTHOG_API_KEY')

0 commit comments

Comments
 (0)