From 41fd53ba77461eeafb92487a6ae0757b6c4d1fc9 Mon Sep 17 00:00:00 2001 From: Jari Voutilainen Date: Thu, 1 Aug 2024 15:42:24 +0300 Subject: [PATCH 1/4] AV-2247: if there is a dnt header, supply it in api tracking --- ckanext/matomo/matomo_api.py | 7 +++++-- ckanext/matomo/tracking.py | 14 +++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ckanext/matomo/matomo_api.py b/ckanext/matomo/matomo_api.py index 4da6a1f..6d2b4d5 100644 --- a/ckanext/matomo/matomo_api.py +++ b/ckanext/matomo/matomo_api.py @@ -158,7 +158,9 @@ def date_range(cls, start, end): end = end.strftime('%Y-%m-%d') return '{},{}'.format(start, end) - def tracking(self, extra_params): + def tracking(self, extra_params, extra_headers=None): + if extra_headers is None: + extra_headers = {} params = self.tracking_params.copy() params.update(extra_params) params['rand'] = str(uuid.uuid4()) @@ -166,7 +168,8 @@ def tracking(self, extra_params): if self.token_auth is not None: params['token_auth'] = self.token_auth - return requests.get(self.tracking_url, params=params) + log.info(extra_headers) + return requests.get(self.tracking_url, params=params, headers=extra_headers) def _process_one_or_more_dates_result(data, handler) -> Dict[str, Any]: diff --git a/ckanext/matomo/tracking.py b/ckanext/matomo/tracking.py index af2e4c8..5991489 100644 --- a/ckanext/matomo/tracking.py +++ b/ckanext/matomo/tracking.py @@ -55,12 +55,20 @@ def post_analytics(category, action, name, download=False): if download: event['download'] = event['url'] + + headers = {} + if toolkit.request.headers.get('DNT'): + headers = {'dnt': toolkit.request.headers.get('DNT')} + log.info('Logging tracking event: %s', event) - tracking_executor.submit(matomo_track, event) + tracking_executor.submit(matomo_track, event, headers) # Required to be a free function to work with background jobs -def matomo_track(event): +def matomo_track(event, extra_headers=None): + if extra_headers is None: + extra_headers = {} + # Gather events to send log = logging.getLogger('ckanext.matomo.tracking') test_mode = toolkit.config.get('ckanext.matomo.test_mode', False) @@ -74,7 +82,7 @@ def matomo_track(event): matomo_site_id = toolkit.config.get(u'ckanext.matomo.site_id') token_auth = toolkit.config.get('ckanext.matomo.token_auth') api = MatomoAPI(matomo_url, matomo_site_id, token_auth=token_auth) - r = api.tracking(event) + r = api.tracking(event, extra_headers=extra_headers) if not r.ok: log.warn('Error when posting tracking events to matomo: %s %s' % (r.status_code, r.reason)) log.warn('With request: %s' % r.url) From cc218ba97819542064f3fcd0fd4b285d75046e9e Mon Sep 17 00:00:00 2001 From: Jari Voutilainen Date: Thu, 1 Aug 2024 15:43:11 +0300 Subject: [PATCH 2/4] remove debug --- ckanext/matomo/matomo_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ckanext/matomo/matomo_api.py b/ckanext/matomo/matomo_api.py index 6d2b4d5..5e97f94 100644 --- a/ckanext/matomo/matomo_api.py +++ b/ckanext/matomo/matomo_api.py @@ -168,7 +168,6 @@ def tracking(self, extra_params, extra_headers=None): if self.token_auth is not None: params['token_auth'] = self.token_auth - log.info(extra_headers) return requests.get(self.tracking_url, params=params, headers=extra_headers) From 5165fccddfa03d5caac3a847b8769e9562445885 Mon Sep 17 00:00:00 2001 From: Jari Voutilainen Date: Thu, 1 Aug 2024 15:48:37 +0300 Subject: [PATCH 3/4] Fix codecov upload --- .github/workflows/test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 63b28f6..24660ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,7 +63,13 @@ jobs: ckan -c test.ini matomo init_db - name: Run tests run: pytest --ckan-ini=test.ini --cov=ckanext.matomo --disable-warnings ckanext/matomo/tests + + - name: install codecov requirements + run: | + apk add gpg gpg-agent + - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} + os: alpine From 6483abe799040f71f432dbb69b4c2128b8925a06 Mon Sep 17 00:00:00 2001 From: Jari Voutilainen Date: Thu, 1 Aug 2024 15:55:24 +0300 Subject: [PATCH 4/4] use test image from ckan organization --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 24660ff..7b6487c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,7 @@ jobs: name: CKAN ${{ matrix.ckan-version }} runs-on: ubuntu-latest container: - image: openknowledge/ckan-dev:${{ matrix.ckan-version }} + image: ckan/ckan-dev:${{ matrix.ckan-version }} services: solr: image: ckan/ckan-solr:${{ matrix.ckan-version }}