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 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 post_dispatch(self, plugin_ctx, methodname, action_metadata): """ Sends the tracking information to the tracking backend """ if not self.is_tracking_allowed(methodname): return server_names = plugin_ctx.get_from_environ('HTTP_X_FORWARDED_HOST', '').split(', ') server_name = server_names[0] if server_names else '' https = plugin_ctx.get_from_environ('HTTP_X_FORWARDED_PROTOCOL', '') == 'https' remote_addrs = plugin_ctx.get_from_environ( 'HTTP_X_FORWARDED_FOR', plugin_ctx.get_from_environ('REMOTE_ADDR', '')).split(', ') remote_addr = remote_addrs[0] if remote_addrs else '' path_info = self.context_path.rstrip( '/') + plugin_ctx.get_from_environ('PATH_INFO', '') headers = { 'HTTP_USER_AGENT': plugin_ctx.get_from_environ('HTTP_USER_AGENT', ''), 'REMOTE_ADDR': remote_addr, 'HTTP_REFERER': plugin_ctx.get_from_environ('HTTP_REFERER', ''), 'HTTP_ACCEPT_LANGUAGE': plugin_ctx.get_from_environ('HTTP_ACCEPT_LANGUAGE', ''), 'SERVER_NAME': server_name, 'PATH_INFO': path_info, 'QUERY_STRING': plugin_ctx.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")
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)
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")
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
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)