ckanext-googleanalytics_v2..../ckanext/googleanalytics/plugin/__init__.py

166 lines
5.4 KiB
Python
Raw Normal View History

2019-11-22 15:59:22 +01:00
from __future__ import absolute_import
2019-11-22 16:02:26 +01:00
from future import standard_library
2019-11-22 17:08:02 +01:00
2019-11-22 16:02:26 +01:00
standard_library.install_aliases()
from builtins import str
from builtins import range
2019-11-22 15:59:22 +01:00
import ast
import logging
import ckanext.googleanalytics.commands as commands
import paste.deploy.converters as converters
import ckan.lib.helpers as h
import ckan.plugins as p
import ckan.plugins.toolkit as tk
2019-12-06 12:28:13 +01:00
import urllib.parse
2019-11-22 15:59:22 +01:00
from ckan.exceptions import CkanVersionException
import threading
2019-12-06 12:28:13 +01:00
import requests
2019-11-22 15:59:22 +01:00
log = logging.getLogger(__name__)
try:
tk.requires_ckan_version("2.9")
except CkanVersionException:
2019-12-06 12:28:13 +01:00
from ckanext.googleanalytics.plugin.pylons_plugin import GAMixinPlugin
2019-11-22 15:59:22 +01:00
else:
from ckanext.googleanalytics.plugin.flask_plugin import GAMixinPlugin
class GoogleAnalyticsException(Exception):
pass
class AnalyticsPostThread(threading.Thread):
"""Threaded Url POST"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
# grabs host from queue
data_dict = self.queue.get()
2019-11-22 16:02:26 +01:00
data = urllib.parse.urlencode(data_dict)
2019-11-22 15:59:22 +01:00
log.debug("Sending API event to Google Analytics: " + data)
# send analytics
2019-12-06 12:28:13 +01:00
res = requests.post(
2019-11-22 15:59:22 +01:00
"http://www.google-analytics.com/collect",
data,
2019-12-06 12:28:13 +01:00
timeout=10,
2019-11-22 15:59:22 +01:00
)
# signals to queue job is done
self.queue.task_done()
class GoogleAnalyticsPlugin(GAMixinPlugin, p.SingletonPlugin):
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IConfigurer, inherit=True)
p.implements(p.ITemplateHelpers)
def configure(self, config):
"""Load config settings for this extension from config file.
See IConfigurable.
"""
if "googleanalytics.id" not in config:
msg = "Missing googleanalytics.id in config"
raise GoogleAnalyticsException(msg)
self.googleanalytics_id = config["googleanalytics.id"]
self.googleanalytics_domain = config.get(
"googleanalytics.domain", "auto"
)
self.googleanalytics_fields = ast.literal_eval(
config.get("googleanalytics.fields", "{}")
)
googleanalytics_linked_domains = config.get(
"googleanalytics.linked_domains", ""
)
self.googleanalytics_linked_domains = [
x.strip() for x in googleanalytics_linked_domains.split(",") if x
]
if self.googleanalytics_linked_domains:
self.googleanalytics_fields["allowLinker"] = "true"
self.googleanalytics_javascript_url = h.url_for_static(
"/scripts/ckanext-googleanalytics.js"
)
# If resource_prefix is not in config file then write the default value
# to the config dict, otherwise templates seem to get 'true' when they
# try to read resource_prefix from config.
if "googleanalytics_resource_prefix" not in config:
config[
"googleanalytics_resource_prefix"
] = commands.DEFAULT_RESOURCE_URL_TAG
self.googleanalytics_resource_prefix = config[
"googleanalytics_resource_prefix"
]
self.show_downloads = converters.asbool(
config.get("googleanalytics.show_downloads", True)
)
self.track_events = converters.asbool(
config.get("googleanalytics.track_events", False)
)
self.enable_user_id = converters.asbool(
config.get("googleanalytics.enable_user_id", False)
)
if not converters.asbool(config.get("ckan.legacy_templates", "false")):
p.toolkit.add_resource(
"../fanstatic_library", "ckanext-googleanalytics"
)
# spawn a pool of 5 threads, and pass them queue instance
for i in range(5):
t = AnalyticsPostThread(self.analytics_queue)
t.setDaemon(True)
t.start()
def update_config(self, config):
"""Change the CKAN (Pylons) environment configuration.
See IConfigurer.
"""
if converters.asbool(config.get("ckan.legacy_templates", "false")):
p.toolkit.add_template_directory(config, "../legacy_templates")
p.toolkit.add_public_directory(config, "../legacy_public")
else:
p.toolkit.add_template_directory(config, "../templates")
def get_helpers(self):
"""Return the CKAN 2.0 template helper functions this plugin provides.
See ITemplateHelpers.
"""
return {"googleanalytics_header": self.googleanalytics_header}
def googleanalytics_header(self):
"""Render the googleanalytics_header snippet for CKAN 2.0 templates.
This is a template helper function that renders the
googleanalytics_header jinja snippet. To be called from the jinja
templates in this extension, see ITemplateHelpers.
"""
if self.enable_user_id and tk.c.user:
self.googleanalytics_fields["userId"] = str(tk.c.userobj.id)
data = {
"googleanalytics_id": self.googleanalytics_id,
"googleanalytics_domain": self.googleanalytics_domain,
"googleanalytics_fields": str(self.googleanalytics_fields),
"googleanalytics_linked_domains": self.googleanalytics_linked_domains,
}
return p.toolkit.render_snippet(
"googleanalytics/snippets/googleanalytics_header.html", data
)