Extract config
This commit is contained in:
parent
8c6c173583
commit
0055c3e063
|
@ -8,7 +8,7 @@ import logging
|
|||
import click
|
||||
import ckan.model as model
|
||||
|
||||
from . import dbutil, utils
|
||||
from . import dbutil, config
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
PACKAGE_URL = "/dataset/" # XXX get from routes...
|
||||
|
@ -57,7 +57,7 @@ def load(credentials, start_date):
|
|||
else:
|
||||
query = "ga:pagePath=~%s,ga:pagePath=~%s" % (
|
||||
PACKAGE_URL,
|
||||
utils.config_prefix(),
|
||||
config.prefix(),
|
||||
)
|
||||
packages_data = get_ga_data(service, profile_id, query_filter=query)
|
||||
save_ga_data(packages_data)
|
||||
|
@ -125,7 +125,7 @@ def internal_save(packages_data, summary_date):
|
|||
AND t2.tracking_date >= t1.tracking_date - %s
|
||||
) + t1.count
|
||||
WHERE t1.running_total = 0 AND tracking_type = 'resource';"""
|
||||
engine.execute(sql, utils.config_recent_view_days())
|
||||
engine.execute(sql, config.recent_view_days())
|
||||
|
||||
# update summary totals for pages
|
||||
sql = """UPDATE tracking_summary t1
|
||||
|
@ -145,7 +145,7 @@ def internal_save(packages_data, summary_date):
|
|||
WHERE t1.running_total = 0 AND tracking_type = 'page'
|
||||
AND t1.package_id IS NOT NULL
|
||||
AND t1.package_id != '~~not~found~~';"""
|
||||
engine.execute(sql, utils.config_recent_view_days())
|
||||
engine.execute(sql, config.recent_view_days())
|
||||
|
||||
|
||||
def bulk_import(service, profile_id, start_date=None):
|
||||
|
@ -196,7 +196,7 @@ def get_ga_data_new(service, profile_id, start_date=None, end_date=None):
|
|||
packages = {}
|
||||
query = "ga:pagePath=~%s,ga:pagePath=~%s" % (
|
||||
PACKAGE_URL,
|
||||
utils.config_prefix(),
|
||||
config.prefix(),
|
||||
)
|
||||
metrics = "ga:uniquePageviews"
|
||||
sort = "-ga:uniquePageviews"
|
||||
|
@ -246,7 +246,7 @@ def save_ga_data(packages_data):
|
|||
ever = visits.get("ever", 0)
|
||||
matches = RESOURCE_URL_REGEX.match(identifier)
|
||||
if matches:
|
||||
resource_url = identifier[len(utils.config_prefix()):]
|
||||
resource_url = identifier[len(config.prefix()):]
|
||||
resource = (
|
||||
model.Session.query(model.Resource)
|
||||
.autoflush(True)
|
||||
|
@ -318,7 +318,7 @@ def get_ga_data(service, profile_id, query_filter):
|
|||
{'identifier': {'recent':3, 'ever':6}}
|
||||
"""
|
||||
now = datetime.datetime.now()
|
||||
recent_date = now - datetime.timedelta(utils.config_recent_view_days())
|
||||
recent_date = now - datetime.timedelta(config.recent_view_days())
|
||||
recent_date = recent_date.strftime("%Y-%m-%d")
|
||||
floor_date = datetime.date(2005, 1, 1)
|
||||
packages = {}
|
||||
|
|
|
@ -10,7 +10,7 @@ from pylons import config as pylonsconfig
|
|||
from ckan.lib.cli import CkanCommand
|
||||
import ckan.model as model
|
||||
|
||||
from . import dbutil, utils
|
||||
from . import dbutil, config
|
||||
|
||||
log = logging.getLogger("ckanext.googleanalytics")
|
||||
PACKAGE_URL = "/dataset/" # XXX get from routes...
|
||||
|
@ -58,8 +58,8 @@ class LoadAnalytics(CkanCommand):
|
|||
self._load_config()
|
||||
self.CONFIG = pylonsconfig
|
||||
|
||||
self.resource_url_tag = utils.config_prefix()
|
||||
self.recent_view_days = utils.config_recent_view_days()
|
||||
self.resource_url_tag = config.prefix()
|
||||
self.recent_view_days = config.recent_view_days()
|
||||
|
||||
# funny dance we need to do to make sure we've got a
|
||||
# configured session
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
import ast
|
||||
import logging
|
||||
|
||||
from werkzeug.utils import import_string
|
||||
|
||||
import ckan.plugins.toolkit as tk
|
||||
|
||||
CONFIG_HANDLER_PATH = "googleanalytics.download_handler"
|
||||
|
||||
DEFAULT_RESOURCE_URL_TAG = "/downloads/"
|
||||
DEFAULT_RECENT_VIEW_DAYS = 14
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def tracking_id():
|
||||
return tk.config["googleanalytics.id"]
|
||||
|
||||
|
||||
def download_handler():
|
||||
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))
|
||||
|
||||
return handler
|
||||
|
||||
|
||||
|
||||
def tracking_mode():
|
||||
type_ = tk.config.get("googleanalytics.tracking_mode")
|
||||
if type_:
|
||||
return type_
|
||||
|
||||
id_ = tracking_id()
|
||||
|
||||
if id_.startswith("UA-"):
|
||||
return "ga"
|
||||
|
||||
if id_.startswith("G-"):
|
||||
return "gtag"
|
||||
|
||||
return "ga"
|
||||
|
||||
|
||||
def measurement_id():
|
||||
return tk.config.get("googleanalytics.measurement_protocol.id") or tracking_id()
|
||||
|
||||
|
||||
def measurement_protocol_client_id():
|
||||
return tk.config.get("googleanalytics.measurement_protocol.client_id")
|
||||
|
||||
|
||||
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")
|
||||
|
||||
|
||||
def profile_id():
|
||||
return tk.config.get("googleanalytics.profile_id")
|
||||
|
||||
|
||||
def credentials():
|
||||
return tk.config.get("googleanalytics.credentials.path")
|
||||
|
||||
|
||||
def domain():
|
||||
return tk.config.get("googleanalytics.domain", "auto")
|
||||
|
||||
|
||||
def fields():
|
||||
fields = ast.literal_eval(tk.config.get("googleanalytics.fields", "{}"))
|
||||
|
||||
if linked_domains():
|
||||
fields["allowLinker"] = "true"
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
def linked_domains():
|
||||
googleanalytics_linked_domains = tk.config.get(
|
||||
"googleanalytics.linked_domains", ""
|
||||
)
|
||||
return [x.strip() for x in googleanalytics_linked_domains.split(",") if x]
|
||||
|
||||
|
||||
def enable_user_id():
|
||||
return tk.asbool(tk.config.get("googleanalytics.enable_user_id", False))
|
||||
|
||||
|
||||
def prefix():
|
||||
return tk.config.get(
|
||||
"googleanalytics_resource_prefix", DEFAULT_RESOURCE_URL_TAG
|
||||
)
|
||||
|
||||
|
||||
def recent_view_days():
|
||||
return tk.asint(
|
||||
tk.config.get(
|
||||
"googleanalytics.recent_view_days", DEFAULT_RECENT_VIEW_DAYS
|
||||
)
|
||||
)
|
|
@ -12,7 +12,7 @@ from paste.util.multidict import MultiDict
|
|||
from ckan.controllers.api import ApiController
|
||||
|
||||
import ckan.plugins.toolkit as tk
|
||||
from ckanext.googleanalytics import utils
|
||||
from ckanext.googleanalytics import config
|
||||
|
||||
|
||||
log = logging.getLogger("ckanext.googleanalytics")
|
||||
|
@ -32,7 +32,7 @@ class GAApiController(ApiController):
|
|||
):
|
||||
data_dict = {
|
||||
"v": 1,
|
||||
"tid": utils.config_id(),
|
||||
"tid": config.tracking_id(),
|
||||
"cid": hashlib.md5(user).hexdigest(),
|
||||
# customer id should be obfuscated
|
||||
"t": "event",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from apiclient.discovery import build
|
||||
from oauth2client.service_account import ServiceAccountCredentials
|
||||
|
||||
from . import utils
|
||||
from ckanext.googleanalytics import utils, config
|
||||
|
||||
|
||||
def init_service(credentials_file):
|
||||
|
@ -26,7 +26,7 @@ def get_profile_id(service):
|
|||
If not user configured, the first account is used
|
||||
"""
|
||||
|
||||
profile_id = utils.config_profile_id()
|
||||
profile_id = config.profile_id()
|
||||
if profile_id:
|
||||
return profile_id
|
||||
|
||||
|
@ -34,8 +34,8 @@ def get_profile_id(service):
|
|||
|
||||
if not accounts.get("items"):
|
||||
return None
|
||||
accountName = utils.config_account()
|
||||
webPropertyId = utils.config_id()
|
||||
accountName = config.account()
|
||||
webPropertyId = config.tracking_id()
|
||||
for acc in accounts.get("items"):
|
||||
if not accountName or acc.get("name") == accountName:
|
||||
accountId = acc.get("id")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
import ckan.plugins.toolkit as tk
|
||||
from ckanext.googleanalytics import utils
|
||||
from ckanext.googleanalytics import config
|
||||
|
||||
|
||||
def get_helpers():
|
||||
|
@ -12,7 +13,7 @@ def get_helpers():
|
|||
|
||||
def googleanalytics_resource_prefix():
|
||||
|
||||
return utils.config_prefix()
|
||||
return config.prefix()
|
||||
|
||||
|
||||
def googleanalytics_header():
|
||||
|
@ -24,16 +25,16 @@ def googleanalytics_header():
|
|||
|
||||
"""
|
||||
|
||||
fields = utils.config_fields()
|
||||
fields = config.fields()
|
||||
|
||||
if utils.config_enable_user_id() and tk.c.user:
|
||||
if config.enable_user_id() and tk.c.user:
|
||||
fields["userId"] = str(tk.c.userobj.id)
|
||||
|
||||
data = {
|
||||
"googleanalytics_id": utils.config_id(),
|
||||
"googleanalytics_domain": utils.config_domain(),
|
||||
"googleanalytics_id": config.tracking_id(),
|
||||
"googleanalytics_domain": config.domain(),
|
||||
"googleanalytics_fields": str(fields),
|
||||
"googleanalytics_linked_domains": utils.config_linked_domains(),
|
||||
"googleanalytics_linked_domains": config.linked_domains(),
|
||||
}
|
||||
return tk.render_snippet(
|
||||
"googleanalytics/snippets/googleanalytics_header.html", data
|
||||
|
@ -41,4 +42,4 @@ def googleanalytics_header():
|
|||
|
||||
|
||||
def googleanalytics_tracking_mode():
|
||||
return utils.config_tracking_mode()
|
||||
return config.tracking_mode()
|
||||
|
|
|
@ -4,7 +4,7 @@ import ckan.plugins.toolkit as tk
|
|||
from ckan.logic import validate
|
||||
|
||||
from . import schema
|
||||
from .. import utils
|
||||
from .. import utils, config
|
||||
from ..model import PackageStats, ResourceStats
|
||||
from ..ga_auth import init_service, get_profile_id
|
||||
|
||||
|
@ -56,7 +56,7 @@ def resource_stats_show(context, data_dict):
|
|||
def event_report(context, data_dict):
|
||||
tk.check_access("sysadmin", context, data_dict)
|
||||
|
||||
se = init_service(utils.config_credentials())
|
||||
se = init_service(config.credentials())
|
||||
filters = []
|
||||
if "action" in data_dict:
|
||||
filters.append(
|
||||
|
|
|
@ -6,7 +6,7 @@ import importlib
|
|||
|
||||
import ckan.plugins as plugins
|
||||
import ckan.plugins.toolkit as tk
|
||||
from ckanext.googleanalytics import utils
|
||||
from ckanext.googleanalytics import config
|
||||
|
||||
from ckan.controllers.package import PackageController
|
||||
from routes.mapper import SubMapper
|
||||
|
@ -149,7 +149,7 @@ def _post_analytics(
|
|||
):
|
||||
data_dict = {
|
||||
"v": 1,
|
||||
"tid": utils.config_id(),
|
||||
"tid": config.tracking_id(),
|
||||
"cid": hashlib.md5(tk.c.user).hexdigest(),
|
||||
# customer id should be obfuscated
|
||||
"t": "event",
|
||||
|
|
|
@ -1,102 +1,15 @@
|
|||
import ast
|
||||
import json
|
||||
import logging
|
||||
|
||||
import requests
|
||||
from six.moves.urllib.parse import urlencode
|
||||
import ckantoolkit as tk
|
||||
from ckanext.googleanalytics import config
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_RESOURCE_URL_TAG = "/downloads/"
|
||||
DEFAULT_RECENT_VIEW_DAYS = 14
|
||||
EVENT_API = "CKAN API Request"
|
||||
|
||||
|
||||
def config_id():
|
||||
return tk.config["googleanalytics.id"]
|
||||
|
||||
|
||||
def config_tracking_mode():
|
||||
type_ = tk.config.get("googleanalytics.tracking_mode")
|
||||
if type_:
|
||||
return type_
|
||||
|
||||
id_ = config_id()
|
||||
|
||||
if id_.startswith("UA-"):
|
||||
return "ga"
|
||||
|
||||
if id_.startswith("G-"):
|
||||
return "gtag"
|
||||
|
||||
return "ga"
|
||||
|
||||
|
||||
def config_measurement_protocol_client_id():
|
||||
return tk.config.get("googleanalytics.measurement_protocol.client_id")
|
||||
|
||||
|
||||
def config_measurement_protocol_client_secret():
|
||||
return tk.config.get("googleanalytics.measurement_protocol.client_secret")
|
||||
|
||||
|
||||
def config_measurement_protocol_api_whitelist():
|
||||
return tk.aslist(
|
||||
tk.config.get("googleanalytics.measurement_protocol.api_tracking_whitelist")
|
||||
)
|
||||
|
||||
|
||||
def config_account():
|
||||
return tk.config.get("googleanalytics.account")
|
||||
|
||||
|
||||
def config_profile_id():
|
||||
return tk.config.get("googleanalytics.profile_id")
|
||||
|
||||
|
||||
def config_credentials():
|
||||
return tk.config.get("googleanalytics.credentials.path")
|
||||
|
||||
|
||||
def config_domain():
|
||||
return tk.config.get("googleanalytics.domain", "auto")
|
||||
|
||||
|
||||
def config_fields():
|
||||
fields = ast.literal_eval(tk.config.get("googleanalytics.fields", "{}"))
|
||||
|
||||
if config_linked_domains():
|
||||
fields["allowLinker"] = "true"
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
def config_linked_domains():
|
||||
googleanalytics_linked_domains = tk.config.get(
|
||||
"googleanalytics.linked_domains", ""
|
||||
)
|
||||
return [x.strip() for x in googleanalytics_linked_domains.split(",") if x]
|
||||
|
||||
|
||||
def config_enable_user_id():
|
||||
return tk.asbool(tk.config.get("googleanalytics.enable_user_id", False))
|
||||
|
||||
|
||||
def config_prefix():
|
||||
return tk.config.get(
|
||||
"googleanalytics_resource_prefix", DEFAULT_RESOURCE_URL_TAG
|
||||
)
|
||||
|
||||
|
||||
def config_recent_view_days():
|
||||
return tk.asint(
|
||||
tk.config.get(
|
||||
"googleanalytics.recent_view_days", DEFAULT_RECENT_VIEW_DAYS
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def send_event(data):
|
||||
if isinstance(data, MeasurementProtocolData):
|
||||
if data["event"] != EVENT_API:
|
||||
|
@ -118,7 +31,7 @@ class SafeJSONEncoder(json.JSONEncoder):
|
|||
|
||||
|
||||
def _mp_api_handler(data_dict):
|
||||
whitelist = set(config_measurement_protocol_api_whitelist())
|
||||
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",
|
||||
|
@ -133,11 +46,11 @@ def _mp_api_handler(data_dict):
|
|||
resp = requests.post(
|
||||
"https://www.google-analytics.com/mp/collect",
|
||||
params={
|
||||
"api_secret": config_measurement_protocol_client_secret(),
|
||||
"measurement_id": config_id()
|
||||
"api_secret": config.measurement_protocol_client_secret(),
|
||||
"measurement_id": config.measurement_id()
|
||||
},
|
||||
data=json.dumps({
|
||||
"client_id": config_measurement_protocol_client_id(),
|
||||
"client_id": config.measurement_protocol_client_id(),
|
||||
"non_personalized_ads": False,
|
||||
"events":[{
|
||||
"name": data_dict["action"],
|
||||
|
|
|
@ -5,14 +5,13 @@ import logging
|
|||
import six
|
||||
|
||||
from flask import Blueprint
|
||||
from werkzeug.utils import import_string
|
||||
|
||||
import ckan.plugins.toolkit as tk
|
||||
import ckan.views.api as api
|
||||
import ckan.views.resource as resource
|
||||
|
||||
from ckan.common import g
|
||||
from . import utils
|
||||
from ckanext.googleanalytics import utils, config
|
||||
|
||||
CONFIG_HANDLER_PATH = "googleanalytics.download_handler"
|
||||
|
||||
|
@ -54,12 +53,7 @@ ga.add_url_rule(
|
|||
|
||||
|
||||
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))
|
||||
handler = config.download_handler()
|
||||
if not handler:
|
||||
log.debug("Use default CKAN callback for resource.download")
|
||||
handler = resource.download
|
||||
|
@ -95,8 +89,8 @@ def _post_analytics(
|
|||
|
||||
from ckanext.googleanalytics.plugin import GoogleAnalyticsPlugin
|
||||
|
||||
if tk.config.get("googleanalytics.id"):
|
||||
if utils.config_measurement_protocol_client_id() and event_type == utils.EVENT_API:
|
||||
if config.tracking_id():
|
||||
if config.measurement_protocol_client_id() and event_type == utils.EVENT_API:
|
||||
data_dict = utils.MeasurementProtocolData({
|
||||
"event": event_type,
|
||||
"object": request_obj_type,
|
||||
|
@ -107,7 +101,7 @@ def _post_analytics(
|
|||
else:
|
||||
data_dict = utils.UniversalAnalyticsData({
|
||||
"v": 1,
|
||||
"tid": tk.config.get("googleanalytics.id"),
|
||||
"tid": config.tracking_id(),
|
||||
"cid": hashlib.md5(six.ensure_binary(tk.c.user)).hexdigest(),
|
||||
# customer id should be obfuscated
|
||||
"t": "event",
|
||||
|
|
Loading…
Reference in New Issue