feat!: IGoogleAnalytics interface
This commit is contained in:
parent
0055c3e063
commit
9f1310af20
|
@ -6,5 +6,6 @@ syntax: glob
|
|||
*~
|
||||
build/
|
||||
dist/
|
||||
.mypy_cache/
|
||||
credentials.json
|
||||
token.dat
|
||||
token.dat
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue