From 9f1310af20b9dd0bf8eab43021bda89a0a2f7705 Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Sat, 3 Dec 2022 17:29:06 +0200 Subject: [PATCH] feat!: IGoogleAnalytics interface --- .gitignore | 3 ++- ckanext/googleanalytics/config.py | 6 ------ ckanext/googleanalytics/interfaces.py | 8 ++++++++ ckanext/googleanalytics/utils.py | 15 +++++++-------- setup.cfg | 2 +- 5 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 ckanext/googleanalytics/interfaces.py diff --git a/.gitignore b/.gitignore index 32940b0..43979e7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ syntax: glob *~ build/ dist/ +.mypy_cache/ credentials.json -token.dat \ No newline at end of file +token.dat diff --git a/ckanext/googleanalytics/config.py b/ckanext/googleanalytics/config.py index 6970fb8..adf17f4 100644 --- a/ckanext/googleanalytics/config.py +++ b/ckanext/googleanalytics/config.py @@ -58,12 +58,6 @@ def measurement_protocol_client_secret(): return tk.config.get("googleanalytics.measurement_protocol.client_secret") -def measurement_protocol_api_whitelist(): - return tk.aslist( - tk.config.get("googleanalytics.measurement_protocol.api_tracking_whitelist") - ) - - def account(): return tk.config.get("googleanalytics.account") diff --git a/ckanext/googleanalytics/interfaces.py b/ckanext/googleanalytics/interfaces.py new file mode 100644 index 0000000..64d1d11 --- /dev/null +++ b/ckanext/googleanalytics/interfaces.py @@ -0,0 +1,8 @@ +from ckan.plugins import Interface + + +class IGoogleAnalytics(Interface): + def googleanalytics_skip_event(self, data): + """Decide if sending data to GA must be skipped. + """ + return False diff --git a/ckanext/googleanalytics/utils.py b/ckanext/googleanalytics/utils.py index 1ebd115..d21ce20 100644 --- a/ckanext/googleanalytics/utils.py +++ b/ckanext/googleanalytics/utils.py @@ -3,7 +3,9 @@ import logging import requests from six.moves.urllib.parse import urlencode -from ckanext.googleanalytics import config +from ckan.plugins import PluginImplementations +from ckanext.googleanalytics import config, interfaces + log = logging.getLogger(__name__) @@ -11,6 +13,10 @@ EVENT_API = "CKAN API Request" def send_event(data): + for p in PluginImplementations(interfaces.IGoogleAnalytics): + if p.googleanalytics_skip_event(data): + return + if isinstance(data, MeasurementProtocolData): if data["event"] != EVENT_API: log.warning("Only API event supported by Measurement Protocol at the moment") @@ -31,13 +37,6 @@ class SafeJSONEncoder(json.JSONEncoder): def _mp_api_handler(data_dict): - whitelist = set(config.measurement_protocol_api_whitelist()) - if whitelist and data_dict["action"] not in whitelist: - log.debug( - "Skip sending %s API action to Google Analytics because it is not whitelisted", - data_dict["action"] - ) - return log.debug( "Sending API event to Google Analytics using the Measurement Protocol: %s", diff --git a/setup.cfg b/setup.cfg index 1eb73be..9705d99 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = ckanext-googleanalytics -version = 2.1.0 +version = 2.2.0 description = Add GA tracking and reporting to CKAN instance long_description = file: README.md long_description_content_type = text/markdown