Skip to content

Commit 5f293f2

Browse files
authored
Remove Google Analytics features (#1697)
1 parent 4fd379b commit 5f293f2

13 files changed

+46
-395
lines changed

home/models.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ class SiteSettings(BaseSetting):
630630
help_text=_('When selecting this option, untranslated pages'
631631
' will not be visible to the front end user'
632632
' when viewing a child language of the site'))
633-
# TODO: GA, FB analytics should be global.
634633
fb_analytics_app_id = models.CharField(
635634
verbose_name=_('Facebook Analytics App ID'),
636635
max_length=25,
@@ -639,6 +638,10 @@ class SiteSettings(BaseSetting):
639638
help_text=_(
640639
"The tracking ID to be used to view Facebook Analytics")
641640
)
641+
642+
# Begin GA settings
643+
# These are now obsolete and should be removed once it is deemed safe for these
644+
# fields to be removed.
642645
local_ga_tag_manager = models.CharField(
643646
verbose_name=_('Local GA Tag Manager'),
644647
max_length=255,
@@ -675,6 +678,8 @@ class SiteSettings(BaseSetting):
675678
"Global GA tracking code to be used"
676679
" to view analytics on more than one site globally")
677680
)
681+
# End GA settings
682+
678683
social_media_link = StreamField(
679684
[('social_media_link', SocialMediaLinkBlock())],
680685
null=True,
@@ -691,7 +696,10 @@ class SiteSettings(BaseSetting):
691696
registration_survey = models.ForeignKey('questionnaires.Survey', null=True,
692697
blank=True,
693698
on_delete=models.SET_NULL)
699+
700+
# Obsolete - Web Light service discontinued Dec 2022
694701
opt_in_to_google_web_light = models.BooleanField(default=False)
702+
695703
mtm_container_id = models.CharField(
696704
verbose_name=_("Matomo Tag Manager container ID"),
697705
max_length=255,
@@ -719,20 +727,6 @@ class SiteSettings(BaseSetting):
719727
],
720728
heading="Facebook Analytics Settings",
721729
),
722-
MultiFieldPanel(
723-
[
724-
FieldPanel('local_ga_tag_manager'),
725-
FieldPanel('global_ga_tag_manager'),
726-
],
727-
heading="GA Tag Manager Settings",
728-
),
729-
MultiFieldPanel(
730-
[
731-
FieldPanel('local_ga_tracking_code'),
732-
FieldPanel('global_ga_tracking_code'),
733-
],
734-
heading="GA Tracking Code Settings",
735-
),
736730
MultiFieldPanel(
737731
[
738732
MultiFieldPanel(
@@ -769,12 +763,6 @@ class SiteSettings(BaseSetting):
769763
],
770764
heading="Registration Settings",
771765
),
772-
MultiFieldPanel(
773-
[
774-
FieldPanel('opt_in_to_google_web_light'),
775-
],
776-
heading="Opt in to Google web light",
777-
),
778766
MultiFieldPanel(
779767
[
780768
FieldPanel(

home/templatetags/generic_components.py

-45
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django import template
44
from django.conf import settings
55
from django.urls import reverse
6-
from google_analytics import CAMPAIGN_TRACKING_PARAMS
76

87
import iogt.iogt_globals as globals_
98

@@ -83,47 +82,3 @@ def language_picker_style():
8382
theme_settings = globals_.theme_settings
8483
return f"color:{theme_settings.language_picker_font_color};background-color:" \
8584
f"{theme_settings.language_picker_background_color}"
86-
87-
88-
@register.simple_tag(takes_context=True)
89-
def google_analytics(context, tracking_code=None, debug=False):
90-
if not tracking_code:
91-
try:
92-
assert settings.GOOGLE_ANALYTICS['google_analytics_id']
93-
except KeyError:
94-
return ''
95-
# attempt get the request from the context
96-
request = context.get('request', None)
97-
if request is None:
98-
raise RuntimeError("Request context required")
99-
# intialise the parameters collection
100-
params = {}
101-
# collect the campaign tracking parameters from the request
102-
for param in CAMPAIGN_TRACKING_PARAMS.values():
103-
value = request.GET.get(param, None)
104-
if value:
105-
params[param] = value
106-
# pass on the referer if present
107-
referer = request.META.get('HTTP_REFERER', None)
108-
if referer:
109-
params['r'] = referer
110-
# remove collected parameters from the path and pass it on
111-
path = request.get_full_path()
112-
parsed_url = urlparse(path)
113-
query = parse_qs(parsed_url.query, keep_blank_values=True)
114-
for param in params:
115-
if param in query:
116-
del query[param]
117-
query = urlencode(query, doseq=True)
118-
new_url = parsed_url._replace(query=query)
119-
params['p'] = new_url.geturl()
120-
params['tracking_code'] = tracking_code or settings.GOOGLE_ANALYTICS[
121-
'google_analytics_id']
122-
# append the debug parameter if requested
123-
if debug:
124-
params['utmdebug'] = 1
125-
# build and return the url
126-
url = reverse('google-analytics')
127-
if params:
128-
url += '?&' + urlencode(params)
129-
return url

home/test.py

+34-107
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
from urllib.parse import parse_qs
1+
from unittest.mock import patch
22

33
from django.conf import settings
44
from django.db.utils import IntegrityError
5-
from django.template import Context
6-
from django.test import TestCase, RequestFactory
5+
from django.test import TestCase
76
from django.urls import reverse
7+
from iogt_users.factories import GroupFactory, UserFactory
88
from rest_framework import status
99
from wagtail.models import PageViewRestriction, Site
10-
from unittest.mock import patch
10+
from wagtail_factories import SiteFactory
1111

12-
from home.models import SVGToPNGMap
13-
from home.templatetags.generic_components import google_analytics
14-
from iogt_users.factories import UserFactory, GroupFactory
1512
from home.factories import ArticleFactory, HomePageFactory
16-
from wagtail_factories import SiteFactory
13+
from home.models import SVGToPNGMap
1714

1815

1916
class PageViewGroupPermissionTests(TestCase):
2017
def setUp(self):
2118
self.user = UserFactory()
2219

2320
Site.objects.all().delete()
24-
self.site = SiteFactory(site_name='IoGT', port=8000, is_default_site=True)
21+
self.site = SiteFactory(site_name="IoGT", port=8000, is_default_site=True)
2522
self.home_page = HomePageFactory(parent=self.site.root_page)
2623

2724
self.group_restricted_article = ArticleFactory(parent=self.home_page)
2825
view_restriction = PageViewRestriction.objects.create(
29-
page=self.group_restricted_article, restriction_type=PageViewRestriction.GROUPS)
26+
page=self.group_restricted_article,
27+
restriction_type=PageViewRestriction.GROUPS,
28+
)
3029

31-
self.allowed_group = GroupFactory(name='Allowed group')
30+
self.allowed_group = GroupFactory(name="Allowed group")
3231
view_restriction.groups.add(self.allowed_group)
3332

3433
def test_group_limited_article_without_login_redirects_to_login_page(self):
3534
response = self.client.get(self.group_restricted_article.url)
3635
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
3736
self.assertEqual(
38-
f'{reverse("account_login")}?next={self.group_restricted_article.url}', response.url)
37+
f'{reverse("account_login")}?next={self.group_restricted_article.url}',
38+
response.url,
39+
)
3940

4041
def test_group_limited_article_without_group_returns_403(self):
41-
self.client.login(username=self.user.username, password='test@123')
42+
self.client.login(username=self.user.username, password="test@123")
4243
response = self.client.get(self.group_restricted_article.url)
4344
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
4445

4546
def test_group_limited_article_with_group_user_returns_200(self):
4647
self.user.groups.add(self.allowed_group)
47-
self.client.login(username=self.user.username, password='test@123')
48+
self.client.login(username=self.user.username, password="test@123")
4849
response = self.client.get(self.group_restricted_article.url)
4950
self.assertEqual(response.status_code, status.HTTP_200_OK)
5051

5152

5253
class SVGToPNGMapTests(TestCase):
5354

5455
def setUp(self) -> None:
55-
self.svg_path = 'static/icons/search.svg'
56+
self.svg_path = "static/icons/search.svg"
5657

5758
def test_create_png_if_not_found(self):
5859
png = SVGToPNGMap.get_png_image(self.svg_path)
59-
expected_path_regex = ''.join([
60-
settings.MEDIA_ROOT,
61-
r"/svg-to-png-maps/svg-to-png.*\.png"
62-
])
60+
expected_path_regex = "".join(
61+
[settings.MEDIA_ROOT, r"/svg-to-png-maps/svg-to-png.*\.png"]
62+
)
6363
self.assertRegex(png.path, expected_path_regex)
6464
self.assertGreater(png.size, 0)
6565

@@ -69,27 +69,25 @@ def test_create_png_if_not_found(self):
6969
def test_ignore_duplicates(self):
7070
png = SVGToPNGMap.get_png_image(self.svg_path)
7171
duplicate = {
72-
'svg_path': self.svg_path,
73-
'fill_color': None,
74-
'stroke_color': None,
75-
'png_image_file': png
72+
"svg_path": self.svg_path,
73+
"fill_color": None,
74+
"stroke_color": None,
75+
"png_image_file": png,
7676
}
7777
SVGToPNGMap.objects.create(**duplicate)
7878
SVGToPNGMap.objects.create(**duplicate)
7979
count = SVGToPNGMap.objects.filter(
80-
svg_path=self.svg_path,
81-
fill_color=None,
82-
stroke_color=None
80+
svg_path=self.svg_path, fill_color=None, stroke_color=None
8381
).count()
8482
self.assertEquals(count, 2)
8583
png_2 = SVGToPNGMap.get_png_image(self.svg_path, None, None)
8684
self.assertEquals(png, png_2)
8785

88-
@patch.object(SVGToPNGMap, 'create')
86+
@patch.object(SVGToPNGMap, "create")
8987
def test_get_png_must_not_fail(self, create):
90-
create.side_effect = Exception('boom')
91-
png = SVGToPNGMap.get_png_image(self.svg_path)
92-
self.assertIsNone(png)
88+
create.side_effect = Exception("boom")
89+
png = SVGToPNGMap.get_png_image(self.svg_path)
90+
self.assertIsNone(png)
9391

9492
def test_uniqueness_unspecified_stroke_and_fill(self):
9593
SVGToPNGMap.create(self.svg_path)
@@ -102,87 +100,16 @@ def test_uniqueness_no_stroke_and_fill(self):
102100
SVGToPNGMap.create(self.svg_path, None, None)
103101

104102
def test_uniqueness_fill_no_stroke(self):
105-
SVGToPNGMap.create(
106-
self.svg_path,
107-
fill_color='#a1b2c3',
108-
stroke_color=None
109-
)
103+
SVGToPNGMap.create(self.svg_path, fill_color="#a1b2c3", stroke_color=None)
110104
with self.assertRaises(IntegrityError):
111-
SVGToPNGMap.create(
112-
self.svg_path,
113-
fill_color='#a1b2c3',
114-
stroke_color=None
115-
)
105+
SVGToPNGMap.create(self.svg_path, fill_color="#a1b2c3", stroke_color=None)
116106

117107
def test_uniqueness_stroke_no_fill(self):
118-
SVGToPNGMap.create(self.svg_path, fill_color=None, stroke_color='#fff')
108+
SVGToPNGMap.create(self.svg_path, fill_color=None, stroke_color="#fff")
119109
with self.assertRaises(IntegrityError):
120-
SVGToPNGMap.create(
121-
self.svg_path,
122-
fill_color=None,
123-
stroke_color='#fff'
124-
)
110+
SVGToPNGMap.create(self.svg_path, fill_color=None, stroke_color="#fff")
125111

126112
def test_uniqueness_stroke_and_fill(self):
127-
SVGToPNGMap.create(
128-
self.svg_path,
129-
fill_color='#555',
130-
stroke_color='#666'
131-
)
113+
SVGToPNGMap.create(self.svg_path, fill_color="#555", stroke_color="#666")
132114
with self.assertRaises(IntegrityError):
133-
SVGToPNGMap.create(
134-
self.svg_path,
135-
fill_color='#555',
136-
stroke_color='#666'
137-
)
138-
139-
140-
class GoogleAnalyticsTagsTestCase(TestCase):
141-
def setUp(self):
142-
self.request_factory = RequestFactory()
143-
144-
def test_query_param_without_value(self):
145-
request = self.request_factory.get('/en/?test')
146-
context = Context({'request': request})
147-
148-
rendered_template = google_analytics(context, tracking_code='my-code')
149-
150-
parsed_qs = parse_qs(rendered_template)
151-
self.assertEqual(parsed_qs['tracking_code'][0], "my-code")
152-
self.assertEqual(parsed_qs['p'][0], "/en/?test=")
153-
154-
def test_query_param_with_value(self):
155-
request = self.request_factory.get('/en/?test=abc')
156-
context = Context({'request': request})
157-
158-
rendered_template = google_analytics(context, tracking_code='my-code')
159-
160-
parsed_qs = parse_qs(rendered_template)
161-
self.assertEqual(parsed_qs['tracking_code'][0], "my-code")
162-
self.assertEqual(parsed_qs['p'][0], "/en/?test=abc")
163-
164-
def test_query_param_with_multiple_values_with_same_key(self):
165-
request = self.request_factory.get('/en/?test=abc&test=xyz')
166-
context = Context({'request': request})
167-
168-
rendered_template = google_analytics(context, tracking_code='my-code')
169-
170-
parsed_qs = parse_qs(rendered_template)
171-
self.assertEqual(parsed_qs['tracking_code'][0], "my-code")
172-
self.assertEqual(parsed_qs['p'][0], "/en/?test=abc&test=xyz")
173-
174-
def test_query_param_with_utm(self):
175-
request = self.request_factory.get(
176-
'/en/?utm_content=content&utm_term=term&utm_source=source&utm_medium=medium&utm_campaign=campaign')
177-
context = Context({'request': request})
178-
179-
rendered_template = google_analytics(context, tracking_code='my-code')
180-
181-
parsed_qs = parse_qs(rendered_template)
182-
self.assertEqual(parsed_qs['tracking_code'][0], "my-code")
183-
self.assertEqual(parsed_qs['p'][0], "/en/")
184-
self.assertEqual(parsed_qs['utm_content'][0], "content")
185-
self.assertEqual(parsed_qs['utm_term'][0], "term")
186-
self.assertEqual(parsed_qs['utm_source'][0], "source")
187-
self.assertEqual(parsed_qs['utm_medium'][0], "medium")
188-
self.assertEqual(parsed_qs['utm_campaign'][0], "campaign")
115+
SVGToPNGMap.create(self.svg_path, fill_color="#555", stroke_color="#666")

iogt/middleware.py

-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@
1717
import iogt.iogt_globals as globals_
1818

1919

20-
class CacheControlMiddleware:
21-
def __init__(self, get_response):
22-
self.get_response = get_response
23-
24-
def __call__(self, request):
25-
response = self.get_response(request)
26-
27-
if SiteSettings.for_request(request).opt_in_to_google_web_light:
28-
response['Cache-Control'] = 'no-transform'
29-
30-
return response
31-
32-
3320
class LocaleMiddleware(DjangoLocaleMiddleware):
3421
def _get_language_from_request(self, request, check_path=False):
3522
if check_path:

iogt/settings/base.py

-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
'health_check.storage',
8585
'health_check.contrib.migrations',
8686
'rest_framework_simplejwt',
87-
'google_analytics',
8887
'django_filters',
8988
'drf_yasg',
9089
'webpush',
@@ -115,7 +114,6 @@
115114
'iogt.middleware.CustomRedirectMiddleware',
116115
'iogt_users.middlewares.RegistrationSurveyRedirectMiddleware',
117116
'external_links.middleware.RewriteExternalLinksMiddleware',
118-
'iogt.middleware.CacheControlMiddleware',
119117
'iogt.middleware.GlobalDataMiddleware',
120118
'wagtailcache.cache.FetchFromCacheMiddleware',
121119
]

iogt/templates/base.html

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<!DOCTYPE html>
66
<html class="no-js" lang="en">
77
<head>
8-
{% include 'google_analytics_tag_manager.html' %}
98
<meta charset="utf-8"/>
109
<meta name="vapid-key" content="{{ vapid_public_key }}">
1110
<title>
@@ -50,7 +49,6 @@
5049

5150
{% get_current_language_bidi as LANGUAGE_BIDI %}
5251
<body class="{% if LANGUAGE_BIDI %}rtl{% endif %} {% block body_class %}{% endblock %}">
53-
{% include 'google_analytics_pixel_tracking.html' %}
5452
{% wagtailuserbar %}
5553

5654
<main id="app">

0 commit comments

Comments
 (0)