ckanext-googleanalytics/ckanext/googleanalytics/views.py

122 lines
3.6 KiB
Python
Raw Normal View History

2019-11-22 15:59:22 +01:00
# -*- coding: utf-8 -*-
2019-12-09 14:23:33 +01:00
2019-11-22 15:59:22 +01:00
import hashlib
2019-12-09 14:23:33 +01:00
import logging
2020-01-15 09:47:22 +01:00
import six
2019-12-09 14:23:33 +01:00
from flask import Blueprint
from werkzeug.utils import import_string
2019-12-09 14:23:33 +01:00
import ckan.plugins.toolkit as tk
2019-11-22 15:59:22 +01:00
import ckan.views.api as api
import ckan.views.resource as resource
2019-12-09 14:23:33 +01:00
2019-11-22 15:59:22 +01:00
from ckan.common import g
from . import utils
2019-11-22 15:59:22 +01:00
CONFIG_HANDLER_PATH = "googleanalytics.download_handler"
2019-11-22 15:59:22 +01:00
log = logging.getLogger(__name__)
2019-12-09 14:23:33 +01:00
ga = Blueprint("google_analytics", "google_analytics")
2019-11-22 15:59:22 +01:00
def action(logic_function, ver=api.API_MAX_VERSION):
try:
2022-05-30 10:12:52 +02:00
function = tk.get_action(logic_function)
2019-11-22 15:59:22 +01:00
side_effect_free = getattr(function, "side_effect_free", False)
request_data = api._get_request_data(try_url_params=side_effect_free)
if isinstance(request_data, dict):
id = request_data.get("id", "")
if "q" in request_data:
id = request_data["q"]
if "query" in request_data:
2019-12-09 14:23:33 +01:00
id = request_data[u"query"]
_post_analytics(g.user, utils.EVENT_API, logic_function, "", id, request_data)
2019-11-22 16:02:26 +01:00
except Exception as e:
2019-11-22 15:59:22 +01:00
log.debug(e)
pass
return api.action(logic_function, ver)
ga.add_url_rule(
"/api/action/<logic_function>",
methods=["GET", "POST"],
view_func=action,
2019-11-22 15:59:22 +01:00
)
ga.add_url_rule(
"/api/<int(min=3, max={0}):ver>/action/<logic_function>".format(
2019-11-22 15:59:22 +01:00
api.API_MAX_VERSION
),
2019-12-09 14:23:33 +01:00
methods=["GET", "POST"],
2019-11-22 15:59:22 +01:00
view_func=action,
)
def download(id, resource_id, filename=None, package_type="dataset"):
handler_path = tk.config.get(CONFIG_HANDLER_PATH)
if handler_path:
handler = import_string(handler_path, silent=True)
else:
handler = None
log.warning(("Missing {} config option.").format(CONFIG_HANDLER_PATH))
if not handler:
log.debug("Use default CKAN callback for resource.download")
handler = resource.download
2019-11-22 15:59:22 +01:00
_post_analytics(
g.user,
"CKAN Resource Download Request",
"Resource",
"Download",
resource_id,
)
return handler(
package_type=package_type,
id=id,
resource_id=resource_id,
filename=filename,
)
2019-11-22 15:59:22 +01:00
ga.add_url_rule(
2019-12-09 14:23:33 +01:00
"/dataset/<id>/resource/<resource_id>/download", view_func=download
2019-11-22 15:59:22 +01:00
)
ga.add_url_rule(
2019-12-09 14:23:33 +01:00
"/dataset/<id>/resource/<resource_id>/download/<filename>",
2019-11-22 15:59:22 +01:00
view_func=download,
)
def _post_analytics(
user, event_type,
request_obj_type, request_function,
request_id, request_payload=None
2019-11-22 15:59:22 +01:00
):
from ckanext.googleanalytics.plugin import GoogleAnalyticsPlugin
2019-12-09 14:23:33 +01:00
2019-11-22 15:59:22 +01:00
if tk.config.get("googleanalytics.id"):
if utils.config_measurement_protocol_client_id() and event_type == utils.EVENT_API:
data_dict = utils.MeasurementProtocolData({
"event": event_type,
2022-08-24 01:42:27 +02:00
"object": request_obj_type,
"function": request_function,
"id": request_id,
"payload": request_payload,
})
else:
data_dict = utils.UniversalAnalyticsData({
"v": 1,
"tid": tk.config.get("googleanalytics.id"),
"cid": hashlib.md5(six.ensure_binary(tk.c.user)).hexdigest(),
# customer id should be obfuscated
"t": "event",
"dh": tk.request.environ["HTTP_HOST"],
"dp": tk.request.environ["PATH_INFO"],
"dr": tk.request.environ.get("HTTP_REFERER", ""),
"ec": event_type,
"ea": request_obj_type + request_function,
"el": request_id,
})
2019-11-22 15:59:22 +01:00
GoogleAnalyticsPlugin.analytics_queue.put(data_dict)