Skip to content

Commit

Permalink
Merge pull request #61 from vrk-kpa/AV-2247_do_not_track_api_use_if_t…
Browse files Browse the repository at this point in the history
…here_is_dnt_header

AV-2247: if there is a dnt header, supply it in api tracking
  • Loading branch information
Zharktas authored Aug 7, 2024
2 parents 4c22b74 + 6483abe commit 7eb0129
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions ckanext/matomo/matomo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,17 @@ 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())

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]:
Expand Down
14 changes: 11 additions & 3 deletions ckanext/matomo/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 7eb0129

Please sign in to comment.