feat!: IGoogleAnalytics interface
This commit is contained in:
parent
0055c3e063
commit
9f1310af20
|
@ -6,5 +6,6 @@ syntax: glob
|
||||||
*~
|
*~
|
||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
|
.mypy_cache/
|
||||||
credentials.json
|
credentials.json
|
||||||
token.dat
|
token.dat
|
|
@ -58,12 +58,6 @@ def measurement_protocol_client_secret():
|
||||||
return tk.config.get("googleanalytics.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():
|
def account():
|
||||||
return tk.config.get("googleanalytics.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
|
import requests
|
||||||
from six.moves.urllib.parse import urlencode
|
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__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -11,6 +13,10 @@ EVENT_API = "CKAN API Request"
|
||||||
|
|
||||||
|
|
||||||
def send_event(data):
|
def send_event(data):
|
||||||
|
for p in PluginImplementations(interfaces.IGoogleAnalytics):
|
||||||
|
if p.googleanalytics_skip_event(data):
|
||||||
|
return
|
||||||
|
|
||||||
if isinstance(data, MeasurementProtocolData):
|
if isinstance(data, MeasurementProtocolData):
|
||||||
if data["event"] != EVENT_API:
|
if data["event"] != EVENT_API:
|
||||||
log.warning("Only API event supported by Measurement Protocol at the moment")
|
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):
|
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(
|
log.debug(
|
||||||
"Sending API event to Google Analytics using the Measurement Protocol: %s",
|
"Sending API event to Google Analytics using the Measurement Protocol: %s",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[metadata]
|
[metadata]
|
||||||
name = ckanext-googleanalytics
|
name = ckanext-googleanalytics
|
||||||
version = 2.1.0
|
version = 2.2.0
|
||||||
description = Add GA tracking and reporting to CKAN instance
|
description = Add GA tracking and reporting to CKAN instance
|
||||||
long_description = file: README.md
|
long_description = file: README.md
|
||||||
long_description_content_type = text/markdown
|
long_description_content_type = text/markdown
|
||||||
|
|
Loading…
Reference in New Issue