feat!: IGoogleAnalytics interface

This commit is contained in:
Sergey Motornyuk 2022-12-03 17:29:06 +02:00
parent 0055c3e063
commit 9f1310af20
5 changed files with 18 additions and 16 deletions

3
.gitignore vendored
View File

@ -6,5 +6,6 @@ syntax: glob
*~
build/
dist/
.mypy_cache/
credentials.json
token.dat
token.dat

View File

@ -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")

View File

@ -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

View File

@ -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",

View File

@ -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