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
|
|
|
|
|
|
|
|
from flask import Blueprint
|
|
|
|
|
|
|
|
import ckan.logic as logic
|
|
|
|
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
|
|
|
|
|
|
|
|
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:
|
|
|
|
function = logic.get_action(logic_function)
|
|
|
|
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"]
|
2019-11-22 15:59:22 +01:00
|
|
|
_post_analytics(g.user, "CKAN API Request", logic_function, "", id)
|
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(
|
2019-12-09 14:23:33 +01:00
|
|
|
"/api/action/<logic_function>", methods=["GET", "POST"], view_func=action,
|
2019-11-22 15:59:22 +01:00
|
|
|
)
|
|
|
|
ga.add_url_rule(
|
|
|
|
u"/<int(min=3, max={0}):ver>/action/<logic_function>".format(
|
|
|
|
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"):
|
|
|
|
_post_analytics(
|
|
|
|
g.user,
|
|
|
|
"CKAN Resource Download Request",
|
|
|
|
"Resource",
|
|
|
|
"Download",
|
|
|
|
resource_id,
|
|
|
|
)
|
|
|
|
return resource.download(package_type, id, resource_id, filename)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
):
|
|
|
|
|
|
|
|
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"):
|
|
|
|
data_dict = {
|
|
|
|
"v": 1,
|
|
|
|
"tid": tk.config.get("googleanalytics.id"),
|
|
|
|
"cid": hashlib.md5(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,
|
|
|
|
}
|
|
|
|
GoogleAnalyticsPlugin.analytics_queue.put(data_dict)
|