Skip to content

Commit 29b5aab

Browse files
committed
remove deprecated Google Analytics support and clean up related code
1 parent c74e044 commit 29b5aab

File tree

7 files changed

+16
-206
lines changed

7 files changed

+16
-206
lines changed

biothings/web/analytics/channels.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import aiohttp
21
import asyncio
3-
import certifi
42
import logging
53
import ssl
64

5+
import aiohttp
6+
import certifi
7+
78
from biothings.utils import serializer
89
from biothings.web.analytics.events import Event, Message
910

@@ -34,30 +35,6 @@ async def send_request(self, session, url, event):
3435
pass
3536

3637

37-
class GAChannel(Channel):
38-
def __init__(self, tracking_id, uid_version=1):
39-
self.tracking_id = tracking_id
40-
self.uid_version = uid_version
41-
self.url = "http://www.google-analytics.com/batch"
42-
43-
async def handles(self, event):
44-
return isinstance(event, Event)
45-
46-
async def send(self, event):
47-
events = event.to_GA_payload(self.tracking_id, self.uid_version)
48-
async with aiohttp.ClientSession() as session:
49-
# The pagination of 20 is defined according to the context of the current application
50-
# Usually, each client request is going to make just 1 request to the GA API.
51-
# However, it's possible to collect data to GA in other parts of the application.
52-
for i in range(0, len(events), 20):
53-
data = "\n".join(events[i : i + 20])
54-
await self.send_request(session, self.url, data)
55-
56-
async def send_request(self, session, url, data):
57-
async with session.post(url, data=data) as _:
58-
pass
59-
60-
6138
class GA4Channel(Channel):
6239
def __init__(self, measurement_id, api_secret, uid_version=1):
6340
self.measurement_id = measurement_id
@@ -102,3 +79,6 @@ async def send_request(self, session, url, data):
10279
# If max retries reached without success, raise an exception
10380
logging.error("GA4Channel: Maximum retries reached. Unable to complete request.")
10481
raise Exception("GA4Channel: Maximum retries reached. Unable to complete request.")
82+
raise Exception("GA4Channel: Maximum retries reached. Unable to complete request.")
83+
raise Exception("GA4Channel: Maximum retries reached. Unable to complete request.")
84+
raise Exception("GA4Channel: Maximum retries reached. Unable to complete request.")

biothings/web/analytics/events.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import hashlib
2-
3-
# import smtplib
42
import uuid
53
from collections import UserDict
6-
from email.mime.multipart import MIMEMultipart
7-
from email.mime.text import MIMEText
84
from ipaddress import IPv4Address, IPv6Address, ip_address
95
from pprint import pformat
106
from random import randint
117
from typing import Union
12-
from urllib.parse import urlencode
138

149

1510
class Event(UserDict):
@@ -77,38 +72,8 @@ def _cid(self, version):
7772
elif version == 2:
7873
return self._cid_v2()
7974

80-
# this is a required GA field
8175
raise ValueError("CID Version.")
8276

83-
def to_GA_payload(self, tracking_id, cid_version=1):
84-
# by default implements
85-
# a GA PageView hit-type
86-
87-
# In the future, consider adding additional
88-
# keys as cutomized dimensions or metrics.
89-
90-
payload = {
91-
"v": 1, # protocol version
92-
"t": "pageview",
93-
"tid": tracking_id,
94-
"cid": self._cid(cid_version),
95-
"uip": self.user_ip,
96-
"dh": self.host,
97-
"dp": self.path,
98-
}
99-
100-
# add document referer
101-
if isinstance(self.referer, str):
102-
if len(self.referer) <= 2048: # GA Limit
103-
payload["dr"] = self.referer
104-
105-
# add user_agent
106-
if self.user_agent:
107-
payload["ua"] = self.user_agent
108-
109-
# this also escapes payload vals
110-
return [urlencode(payload)]
111-
11277
def to_GA4_payload(self, measurement_id, cid_version=1):
11378
# Document about page_view event: https://support.google.com/analytics/answer/9964640#pageviews&zippy=%2Cin-this-article
11479
# GA4 does not support [Document path as UA](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dp)
@@ -154,32 +119,6 @@ class GAEvent(Event):
154119
# "value": "60"
155120
# }
156121

157-
def to_GA_payload(self, tracking_id, cid_version=1):
158-
payloads = super().to_GA_payload(tracking_id, cid_version)
159-
if self.get("category") and self.get("action"):
160-
payloads.append(
161-
urlencode(
162-
_clean(
163-
{
164-
"v": 1, # protocol version
165-
"t": "event",
166-
"tid": tracking_id,
167-
"cid": self._cid(cid_version),
168-
"ec": self["category"],
169-
"ea": self["action"],
170-
"el": self.get("label", ""),
171-
"ev": self.get("value", ""),
172-
}
173-
)
174-
)
175-
)
176-
for event in self.get("__secondary__", []):
177-
event["__request__"] = self["__request__"]
178-
payloads.extend(event.to_GA_payload(tracking_id, cid_version)[1:])
179-
# ignore the first event (pageview)
180-
# which is already generated once
181-
return payloads
182-
183122
def to_GA4_payload(self, measurement_id, cid_version=1):
184123
payloads = super().to_GA4_payload(measurement_id, cid_version)
185124
if self.get("category") and self.get("action"):

biothings/web/analytics/notifiers.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import asyncio
2-
32
from collections import defaultdict
3+
44
from tornado.web import RequestHandler
5-
from biothings.web.analytics.channels import GA4Channel, GAChannel, SlackChannel
5+
6+
from biothings.web.analytics.channels import GA4Channel, SlackChannel
67

78

89
class Notifier:
@@ -11,13 +12,6 @@ def __init__(self, settings):
1112

1213
if hasattr(settings, "SLACK_WEBHOOKS"):
1314
self.channels.append(SlackChannel(getattr(settings, "SLACK_WEBHOOKS")))
14-
if getattr(settings, "GA_ACCOUNT", None):
15-
self.channels.append(
16-
GAChannel(
17-
getattr(settings, "GA_ACCOUNT"),
18-
getattr(settings, "GA_UID_GENERATOR_VERSION", 1),
19-
)
20-
)
2115
if getattr(settings, "GA4_MEASUREMENT_ID", None):
2216
self.channels.append(
2317
GA4Channel(
@@ -53,3 +47,6 @@ def on_finish(self):
5347
asyncio.run_coroutine_threadsafe(notifier.broadcast(self.event), asyncio.get_event_loop())
5448
else: # need to initialize a notifier
5549
raise NotImplementedError()
50+
raise NotImplementedError()
51+
raise NotImplementedError()
52+
raise NotImplementedError()

biothings/web/settings/default.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@
209209
# Sentry project address
210210
SENTRY_CLIENT_KEY = ""
211211

212-
# Google Analytics Account ID
213-
GA_ACCOUNT = ""
214-
215212
# *****************************************************************************
216213
# Endpoints Specifics & Others
217214
# *****************************************************************************

config.py.example

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ GA4_UID_GENERATOR_VERSION = 1
110110
# Analytics Settings
111111
# *****************************************************************************
112112

113-
# Google Analytics Account ID
114-
115-
GA_ACCOUNT = 'UA-123123-1'
116-
117113
# Google Measurement ID
118114
GA4_MEASUREMENT_ID = 'G-KXzzzzLBN'
119115

tests/web/analytics/test_channels.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import aiohttp
21
import asyncio
3-
import pytest
2+
from unittest.mock import patch
43

4+
import aiohttp
5+
import pytest
56
from aioresponses import aioresponses
67

78
from biothings.utils import serializer
8-
from biothings.web.analytics.channels import SlackChannel, GA4Channel, GAChannel
9+
from biothings.web.analytics.channels import GA4Channel, SlackChannel
910
from biothings.web.analytics.events import GAEvent, Message
10-
from unittest.mock import patch
1111

1212

1313
@pytest.mark.asyncio
@@ -32,34 +32,6 @@ async def test_send_Slack():
3232
await channel.send(message)
3333

3434

35-
@pytest.mark.asyncio
36-
async def test_send_GA():
37-
event = GAEvent(
38-
{
39-
"__request__": {
40-
"user_agent": "Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1",
41-
"referer": None,
42-
"user_ip": "127.0.0.1",
43-
"host": "example.org",
44-
"path": "/",
45-
},
46-
"category": "test",
47-
"action": "play",
48-
"label": "sample.mp4",
49-
"value": 60,
50-
}
51-
)
52-
channel = GAChannel("G-XXXXXX", 2)
53-
assert await channel.handles(event)
54-
55-
with aioresponses() as responses:
56-
# Mock the URL to return a 200 OK response
57-
responses.post(channel.url, status=200)
58-
59-
# If the function completes without raising an exception, the test will pass
60-
await channel.send(event)
61-
62-
6335
@pytest.mark.asyncio
6436
async def test_send_GA4():
6537
event = GAEvent(

tests/web/analytics/test_events.py

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,6 @@
22

33
from biothings.web.analytics.events import Event, GAEvent
44

5-
# validator
6-
# https://ga-dev-tools.web.app/hit-builder/
7-
8-
9-
def test_pageview_1():
10-
event = Event(
11-
dict(
12-
__request__={
13-
"user_agent": "Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1",
14-
"referer": "https://example.com/",
15-
"user_ip": "127.0.0.1",
16-
"host": "example.org",
17-
"path": "/",
18-
}
19-
)
20-
)
21-
print(event.to_GA_payload("UA-000000-2"))
22-
print(event.to_GA_payload("UA-000000-2", 2))
23-
24-
25-
def test_pageview_2():
26-
event = Event(
27-
dict(
28-
__request__={
29-
"user_agent": None,
30-
"referer": None,
31-
"user_ip": "127.0.0.1",
32-
"host": "example.org",
33-
"path": "/404.html",
34-
}
35-
)
36-
)
37-
print(event.to_GA_payload("UA-000000-2"))
38-
print(event.to_GA_payload("UA-000000-2", 2))
39-
40-
41-
def test_event_1():
42-
event = GAEvent(
43-
{
44-
"__request__": {
45-
"user_agent": "Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1",
46-
"referer": "https://example.com/",
47-
"user_ip": "127.0.0.1",
48-
"host": "example.org",
49-
"path": "/",
50-
},
51-
"category": "video",
52-
"action": "play",
53-
"label": "sample.mp4",
54-
"value": 60,
55-
}
56-
)
57-
print(event.to_GA_payload("UA-000000-2"))
58-
print(event.to_GA_payload("UA-000000-2", 2))
59-
60-
61-
def test_event_2():
62-
event = GAEvent(
63-
{
64-
"__request__": {
65-
"user_agent": "Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1",
66-
"referer": "https://example.com/",
67-
"user_ip": "127.0.0.1",
68-
"host": "example.org",
69-
"path": "/",
70-
}
71-
}
72-
)
73-
print(event.to_GA_payload("UA-000000-2"))
74-
print(event.to_GA_payload("UA-000000-2", 2))
75-
765

776
def test_pageview_ga4_1():
787
event = Event(

0 commit comments

Comments
 (0)