def setUp(self): """ Set up a PiwikTracker instance """ super(TrackerBaseTestCase, self).setUp() headers = { 'HTTP_USER_AGENT': self.get_random_ua(), 'HTTP_REFERER': 'http://referer%d.example.com/referer/' % random.randint(0, 99), 'REMOTE_ADDR': self.get_random_ip(), 'HTTP_ACCEPT_LANGUAGE': self.get_random_language(), 'QUERY_STRING': 'testrand=%d' % random.randint(0, 99), 'PATH_INFO': '/path%d/' % random.randint(0, 99), 'SERVER_NAME': 'action%d.example.com' % random.randint(0, 99), 'HTTPS': '', } self.request = FakeRequest(headers) self.pt = PiwikTracker(self.settings['PIWIK_SITE_ID'], self.request) self.pt.set_api_url(self.settings['PIWIK_TRACKING_API_URL']) self.pt.set_custom_variable( 1, 'testrun', self.get_unique_string(), )
def test_missing_api_url(self): try: pt = PiwikTracker(self.settings.PIWIK_SITE_ID, self.request) pt.do_track_page_view('fake title') invalid_config = True except ConfigurationError: invalid_config = False self.assertFalse(invalid_config)
def track(request, title): if PIWIK_SITE_ID and PIWIK_URL and PIWIK_AUTH_TOKEN: try: piwik = PiwikTracker(PIWIK_SITE_ID, request) piwik.set_api_url('%s/piwik.php' % PIWIK_URL) piwik.set_ip(get_client_ip(request)) piwik.set_token_auth(PIWIK_AUTH_TOKEN) piwik.do_track_page_view(title) except HTTPError: pass
def track_visit(request, page_title): """ Track visits in Piwik. """ piwik = PiwikTracker(app.config['PIWIK_SITE_ID'], FlaskRequest(request)) piwik.set_api_url(app.config['PIWIK_TRACKING_API_URL']) piwik.set_token_auth(app.config['PIWIK_TOKEN_AUTH']) piwik.do_track_page_view(page_title)
def redirection(request, short): """ This view redirects a user based on the short URL they requested. """ url = get_object_or_404( URL, short__iexact = short ) url.clicks = url.clicks + 1 url.save() """ Include server-side tracking because there is no template displayed to the user which would include javascript tracking. """ from piwikapi.tracking import PiwikTracker from django.conf import settings piwiktracker = PiwikTracker(settings.PIWIK_SITE_ID, request) piwiktracker.set_api_url(settings.PIWIK_URL) piwiktracker.do_track_page_view('Redirect to %s' % url.target) return redirect( url.target )
def track_request(user_agent: str, client_ip: str, referer: str, accept_language_header: str, server_name: str, url_path: str, query_string: str, is_https: bool, matomo_url: str, matomo_site_id: int, matomo_auth_token: str, app: Flask, is_debug: bool): try: accept_languages = AcceptLanguage.parse(accept_language_header) accept_language_code = "" if len(accept_languages): accept_language_code = accept_languages[0].language_code matomo_request = FakeRequest({ 'HTTP_USER_AGENT': user_agent, 'REMOTE_ADDR': client_ip, 'HTTP_REFERER': referer, 'HTTP_ACCEPT_LANGUAGE': accept_language_code, 'SERVER_NAME': server_name, 'PATH_INFO': url_path, 'QUERY_STRING': query_string, 'HTTPS': is_https }) piwiktracker = PiwikTracker(matomo_site_id, matomo_request) piwiktracker.set_api_url(f"{matomo_url}/matomo.php") piwiktracker.set_ip(client_ip) # Optional, to override the IP piwiktracker.set_token_auth(matomo_auth_token) # Optional, to override the IP piwiktracker.do_track_page_view('API') except: if is_debug: app.logger.error(traceback.format_exc()) # pylint: disable=no-member
def __init__(self, application, request, **kwargs): web.RequestHandler.__init__(self, application, request, **kwargs) self.request.remote_ip = self.request.headers.get("X-Forwarded-For", self.request.remote_ip) if self.application.enable_piwik: headers = { 'HTTP_USER_AGENT': self.request.headers.get("User-Agent", "Default"), 'REMOTE_ADDR': self.request.remote_ip, #'HTTP_REFERER': 'http://referer.com/somewhere/', 'HTTP_ACCEPT_LANGUAGE': 'en-US', 'SERVER_NAME': self.request.host, 'PATH_INFO': self.request.path, 'QUERY_STRING': self.request.query, 'HTTPS': False, } request = FakeRequest(headers) self.piwik = PiwikTracker(config.get("piwik", "app_id", 1), request) self.piwik.set_api_url(config.get("piwik", "app_url", "")) self.piwik.set_ip(self.request.remote_ip) # Optional, to override the IP self.piwik.set_token_auth(config.get("piwik", "token_auth", "")) # Optional, to override the IP self.tracked = False
def setUp(self): """ Set up a PiwikTracker instance """ super(TrackerBaseTestCase, self).setUp() headers = { 'HTTP_USER_AGENT': self.get_random_ua(), 'HTTP_REFERER': 'http://referer%d.example.com/referer/' % random.randint(0, 99), 'REMOTE_ADDR': self.get_random_ip(), 'HTTP_ACCEPT_LANGUAGE': self.get_random_language(), 'QUERY_STRING': 'testrand=%d' % random.randint(0, 99), 'PATH_INFO': '/path%d/' % random.randint(0, 99), 'SERVER_NAME': 'action%d.example.com' % random.randint(0, 99), 'HTTPS': '', } self.request = FakeRequest(headers) self.pt = PiwikTracker(self.settings.PIWIK_SITE_ID, self.request) self.pt.set_api_url(self.settings.PIWIK_TRACKING_API_URL) self.pt.set_custom_variable( 1, 'testrun', self.get_unique_string(), )
def post_dispatch(self, plugin_api, methodname, action_metadata): """ Sends the tracking information to the tracking backend """ if not self.is_tracking_allowed(methodname): return server_names = plugin_api.get_from_environ('HTTP_X_FORWARDED_HOST', '').split(', ') server_name = server_names[0] if server_names else '' https = plugin_api.get_from_environ('HTTP_X_FORWARDED_PROTOCOL', '') == 'https' remote_addrs = plugin_api.get_from_environ('HTTP_X_FORWARDED_FOR', plugin_api.get_from_environ('REMOTE_ADDR', '')).split(', ') remote_addr = remote_addrs[0] if remote_addrs else '' path_info = self.context_path.rstrip('/') + plugin_api.get_from_environ('PATH_INFO', '') headers = { 'HTTP_USER_AGENT': plugin_api.get_from_environ('HTTP_USER_AGENT', ''), 'REMOTE_ADDR': remote_addr, 'HTTP_REFERER': plugin_api.get_from_environ('HTTP_REFERER', ''), 'HTTP_ACCEPT_LANGUAGE': plugin_api.get_from_environ('HTTP_ACCEPT_LANGUAGE', ''), 'SERVER_NAME': server_name, 'PATH_INFO': path_info, 'QUERY_STRING': plugin_api.get_from_environ('QUERY_STRING', ''), 'HTTPS': https, } request = FakeRequest(headers) site_id = self.get_site_id(methodname) piwiktracker = PiwikTracker(site_id, request) piwiktracker.set_api_url(self.tracking_api_url) piwiktracker.set_ip(remote_addr) if self.is_rest_call(methodname): piwiktracker.set_custom_variable(1, 'source', 'rest-python', 'page') if self.is_authentication_required(): piwiktracker.set_token_auth(self.auth_token) piwiktracker.do_track_page_view("KonText")
class TrackerBaseTestCase(PiwikAPITestCase): """ This sets up a more or less random visitor In every test run all tests get the same testrun custom variable. """ def setUp(self): """ Set up a PiwikTracker instance """ super(TrackerBaseTestCase, self).setUp() headers = { 'HTTP_USER_AGENT': self.get_random_ua(), 'HTTP_REFERER': 'http://referer%d.example.com/referer/' % random.randint(0, 99), 'REMOTE_ADDR': self.get_random_ip(), 'HTTP_ACCEPT_LANGUAGE': self.get_random_language(), 'QUERY_STRING': 'testrand=%d' % random.randint(0, 99), 'PATH_INFO': '/path%d/' % random.randint(0, 99), 'SERVER_NAME': 'action%d.example.com' % random.randint(0, 99), 'HTTPS': '', } self.request = FakeRequest(headers) self.pt = PiwikTracker(self.settings.PIWIK_SITE_ID, self.request) self.pt.set_api_url(self.settings.PIWIK_TRACKING_API_URL) self.pt.set_custom_variable( 1, 'testrun', self.get_unique_string(), ) def get_title(self, title): """ Adds a timestamp to the action title" :param title: Action :type title: str :rtype: str """ now = datetime.datetime.now() return "%s %d:%d:%d" % (title, now.hour, now.minute, now.second) def get_random_ip(self): """ Returns an IP out of the test networks, see RFC 5735. Seemed to make sense to use such addresses for unit tests. :rtype: str """ test_networks = ( '192.0.2', '198.51.100', '203.0.113', ) return '%s.%d' % ( test_networks[random.randint(0, len(test_networks) - 1)], random.randint(1, 254), ) def get_random(self, choices): return choices[random.randint(0, len(choices) - 1)] def get_random_ua(self): """ Returns a random user agent string Only return Desktop UAs as Piwik doesn't like big resolutions on devices it thinks are mobile. :rtype: string """ uas = ( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like " "Gecko) Chrome/17.0.963.83 Safari/535.11", 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20100101 ', 'Firefox/10.0.3 Iceweasel/10.0.3', 'Opera/9.80 (Windows NT 6.1; WOW64; U; de) Presto/2.10.229 ' 'Version/11.62', 'Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 ' 'Firefox/11.0', #'Mozilla/5.0 (iPad; U; CPU iPhone OS 5_1 like Mac OS X; de_DE) ' # 'AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;' # 'FBAV/4.1.1;FBBV/4110.0;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone ' # 'OS;FBSV/5.1;FBSS/1; FBCR/;FBID/tablet;FBLC/de_DE;FBSF/1.0]', #'Mozilla/5.0 (Linux; U; Android 2.3.6; fr-fr; GT-N7000 Build/' # 'GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) ' # 'Version/4.0 Mobile Safari/533.1', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; de-de) ' 'AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 ' 'Safari/533.18.5', ) return self.get_random(uas) def get_random_language(self): """ Return a random language code """ langs = ( 'ar-EG', 'ar-TN', 'de', 'en', 'en-GB', 'en-US', 'es', 'es-AR', 'fr', 'in', 'it', 'jp', 'ms', 'pl', 'pt', 'pt-BR', 'ru', 'tn', 'zh-TW', 'zh-CN', ) return self.get_random(langs)
class MiniBaseHandler(web.RequestHandler, TrustIPAuth): def __init__(self, application, request, **kwargs): web.RequestHandler.__init__(self, application, request, **kwargs) self.request.remote_ip = self.request.headers.get("X-Forwarded-For", self.request.remote_ip) if self.application.enable_piwik: headers = { 'HTTP_USER_AGENT': self.request.headers.get("User-Agent", "Default"), 'REMOTE_ADDR': self.request.remote_ip, #'HTTP_REFERER': 'http://referer.com/somewhere/', 'HTTP_ACCEPT_LANGUAGE': 'en-US', 'SERVER_NAME': self.request.host, 'PATH_INFO': self.request.path, 'QUERY_STRING': self.request.query, 'HTTPS': False, } request = FakeRequest(headers) self.piwik = PiwikTracker(config.get("piwik", "app_id", 1), request) self.piwik.set_api_url(config.get("piwik", "app_url", "")) self.piwik.set_ip(self.request.remote_ip) # Optional, to override the IP self.piwik.set_token_auth(config.get("piwik", "token_auth", "")) # Optional, to override the IP self.tracked = False def set_default_headers(self): self.set_header("Access-Control-Allow-Credentials", "true") self.set_header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS") self.set_header("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, Cache-Control") def remote_get(self, path, call_back): """ @summary: 获取指定文件远程路径文件 @param path: 远程文件名称 @param call_back: 获取后回调函数地址 @return: """ raise NotImplementedError("Should have implemented this" ) def send_result(self, status, **kwargs): """ @summary: 输出结果 @param status: 操作成功标志 @param **kwargs: 辅助描述内容 @return: """ ret_json = {"status": status} for key, value in kwargs.iteritems(): ret_json[key] = value callbackParae = self.get_argument("callbackparam", None) if callbackParae is None: self.set_header('Content-Type', 'application/json') self.finish(json.dumps(ret_json)) return # # 使用jsonp实现跨域返回 # self.set_header('Content-Type', 'text/plain') self.finish(callbackParae + '(' + json.dumps(ret_json) + ')') def on_finish(self): """Called after the end of a request. Override this method to perform cleanup, logging, etc. This method is a counterpart to `prepare`. ``on_finish`` may not produce any output, as it is called after the response has been sent to the client. """ if not self.application.enable_piwik: return if not self.tracked: title = self.request.path.replace("/", ".") if title.startswith("."): title = title[1:] self.piwik.do_track_page_view(title)
def func_wrapper(request, *args, **kwargs): if check_piwik_settings(): try: piwik = PiwikTracker(settings.PIWIK['SITE_ID'], request) piwik.set_api_url('%s/piwik.php' % settings.PIWIK['URL']) piwik.set_ip(get_client_ip(request)) piwik.set_token_auth(settings.PIWIK['AUTH_TOKEN']) if request.user.is_authenticated: piwik.set_visitor_id(request.user.unique_visitor_id) piwik.do_track_page_view(title) except HTTPError as e: sys.stderr.write(str(e)) sys.stderr.write('\n') return func(request, *args, **kwargs)
from urllib import unquote from functools import wraps import thread import sqlite3 import json from db import connect_db from config import * from piwikapi.tracking import PiwikTracker from piwikapi.tests.request import FakeRequest # Tracking method piwikrequest = FakeRequest(headers) piwiktracker = PiwikTracker(PIWIK_SITE_ID, piwikrequest) piwiktracker.set_api_url(PIWIK_TRACKING_API_URL) def do_piwik(ip, url, title): piwiktracker.set_ip(ip) piwiktracker.set_token_auth(AUTH_TOKEN_STRING) piwiktracker.set_url("http://" + url) title = title.encode("ascii", "ignore") piwiktracker.do_track_page_view(title) # Decoration to support jsonp # See: https://gist.github.com/aisipos/1094140 def support_jsonp(f): """Wraps JSONified output for JSONP"""
def do_track_page_view(self, headers, reqtime=None, title=None, userid=None, session_key=None, status_code=None, fail_silently=PIWIK_FAIL_SILENTLY): """ Actually record the page view with Piwik: do the actual work with kwargs from self.get_track_kwargs """ request = FakeRequest(headers) pt = PiwikTracker(PIWIK_SITEID, request) pt.set_api_url(PIWIK_URL) pt.set_token_auth(PIWIK_TOKEN) pt.set_ip(get_real_ip(request)) if session_key: visitor_id = hashlib.sha512( session_key).hexdigest()[0:PiwikTracker.LENGTH_VISITOR_ID] pt.set_visitor_id(visitor_id) if reqtime: dt = datetime.datetime.fromtimestamp(reqtime, pytz.utc) pt.set_force_visit_date_time(dt) if userid: pt.set_custom_variable(1, 'userid', userid, scope='visit') if status_code: pt.set_custom_variable(2, 'status_code', status_code, scope='page') if PIWIK_FORCE_HOST: pt._set_host(PIWIK_FORCE_HOST) try: pt.do_track_page_view(title) except urllib_errors: if not fail_silently: raise
def do_track_page_view(self, headers, reqtime=None, title=None, userid=None, session_key=None, status_code=None, fail_silently=PIWIK_FAIL_SILENTLY): """ Actually record the page view with Piwik: do the actual work with kwargs from self.get_track_kwargs """ request = FakeRequest(headers) pt = PiwikTracker(PIWIK_SITEID, request) pt.set_api_url(PIWIK_URL) pt.set_token_auth(PIWIK_TOKEN) pt.set_ip(get_real_ip(request)) if session_key: visitor_id = hashlib.sha512(session_key).hexdigest()[0:PiwikTracker.LENGTH_VISITOR_ID] pt.set_visitor_id(visitor_id) if reqtime: dt = datetime.datetime.fromtimestamp(reqtime, pytz.utc) pt.set_force_visit_date_time(dt) if userid: pt.set_custom_variable(1, 'userid', userid, scope='visit') if status_code: pt.set_custom_variable(2, 'status_code', status_code, scope='page') if PIWIK_FORCE_HOST: pt._set_host(PIWIK_FORCE_HOST) try: pt.do_track_page_view(title) except urllib_errors: if not fail_silently: raise
def post_dispatch(self, plugin_api, methodname, action_metadata, log_data): """ Sends the tracking information to the tracking backend """ if not self.is_tracking_allowed(methodname): return server_names = plugin_api.get_from_environ('HTTP_X_FORWARDED_HOST', '').split(', ') server_name = server_names[0] if server_names else '' https = plugin_api.get_from_environ('HTTP_X_FORWARDED_PROTOCOL', '') == 'https' remote_addrs = plugin_api.get_from_environ('HTTP_X_FORWARDED_FOR', plugin_api.get_from_environ('REMOTE_ADDR', '')).split(', ') remote_addr = remote_addrs[0] if remote_addrs else '' path_info = self.context_path.rstrip('/') + plugin_api.get_from_environ('PATH_INFO', '') headers = { 'HTTP_USER_AGENT': plugin_api.get_from_environ('HTTP_USER_AGENT', ''), 'REMOTE_ADDR': remote_addr, 'HTTP_REFERER': plugin_api.get_from_environ('HTTP_REFERER', ''), 'HTTP_ACCEPT_LANGUAGE': plugin_api.get_from_environ('HTTP_ACCEPT_LANGUAGE', ''), 'SERVER_NAME': server_name, 'PATH_INFO': path_info, 'QUERY_STRING': plugin_api.get_from_environ('QUERY_STRING', ''), 'HTTPS': https, } request = FakeRequest(headers) site_id = self.get_site_id(methodname) piwiktracker = PiwikTracker(site_id, request) piwiktracker.set_api_url(self.tracking_api_url) piwiktracker.set_ip(remote_addr) if self.is_rest_call(methodname): piwiktracker.set_custom_variable(1, 'source', 'rest-python', 'page') if self.is_authentication_required(): piwiktracker.set_token_auth(self.auth_token) piwiktracker.do_track_page_view("KonText")