diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 63b28f6..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 }} @@ -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 diff --git a/ckanext/matomo/matomo_api.py b/ckanext/matomo/matomo_api.py index 4da6a1f..5e97f94 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,7 @@ 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) + 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)