Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AV-2247: if there is a dnt header, supply it in api tracking #61

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)