# Path: zenodo/modules/records/serializers/schemas/common.py
# def ui_link_for(tpl, **kwargs):
#     """Create an UI link using specific template."""
#     return link_for(current_app.config['THEME_SITEURL'], tpl, **kwargs)


import json
import requests
from dateutil.parser import parse as dateutil_parse
from elasticsearch_dsl import Search
from flask import current_app
from invenio_cache import current_cache
from invenio_pidstore.errors import PIDDeletedError
from invenio_search import current_search_client
from invenio_search.utils import build_alias_name
from six.moves.urllib.parse import urlencode, urlsplit, urlunsplit
from zenodo.modules.records.serializers.schemas.common import ui_link_for
from zenodo.modules.stats.errors import PiwikExportRequestError
from zenodo.modules.stats.utils import chunkify, fetch_record
            .get('id_site', None)
        url = ui_link_for('record_html', id=event.recid)
        visitor_id = event.visitor_id[0:16]
        _, record = fetch_record(event.recid)
        oai = record.get('_oai', {}).get('id')
        cvar = json.dumps({'1': ['oaipmhID', oai]})
        action_name = record.get('title')[:150]  # max 150 characters
        urlref = None
        if event.referrer:
            try:
                scheme, netloc, path, _, _ = urlsplit(event.referrer)
                urlref = urlunsplit((scheme, netloc, path, None, None))
            except Exception:
                pass

        params = dict(
            idsite=id_site,
            rec=1,
            url=url,
            _id=visitor_id,
            cid=visitor_id,
            cvar=cvar,
            cdt=event.timestamp,
            urlref=urlref,
            action_name=action_name
        )

        if event.to_dict().get('country'):
            params['country'] = event.country.lower()
        if event.to_dict().get('file_key'):

# Path: zenodo/modules/records/serializers/schemas/common.py
# def ui_link_for(tpl, **kwargs):
#     """Create an UI link using specific template."""
#     return link_for(current_app.config['THEME_SITEURL'], tpl, **kwargs)


# Path: zenodo/modules/stats/exporters.py
import json
import requests
from dateutil.parser import parse as dateutil_parse
from elasticsearch_dsl import Search
from flask import current_app
from invenio_cache import current_cache
from invenio_pidstore.errors import PIDDeletedError
from invenio_search import current_search_client
from invenio_search.utils import build_alias_name
from six.moves.urllib.parse import urlencode, urlsplit, urlunsplit
from zenodo.modules.records.serializers.schemas.common import ui_link_for
from zenodo.modules.stats.errors import PiwikExportRequestError
from zenodo.modules.stats.utils import chunkify, fetch_record

            .get('id_site', None)
        url = ui_link_for('record_html', id=event.recid)
        visitor_id = event.visitor_id[0:16]
        _, record = fetch_record(event.recid)
        oai = record.get('_oai', {}).get('id')
        cvar = json.dumps({'1': ['oaipmhID', oai]})
        action_name = record.get('title')[:150]  # max 150 characters
        urlref = None
        if event.referrer:
            try:
                scheme, netloc, path, _, _ = urlsplit(event.referrer)
                urlref = urlunsplit((scheme, netloc, path, None, None))
            except Exception:
                pass

        params = dict(
            idsite=id_site,
            rec=1,
            url=url,
            _id=visitor_id,
            cid=visitor_id,
            cvar=cvar,
            cdt=event.timestamp,
            urlref=urlref,
            action_name=action_name
        )

        if event.to_dict().get('country'):
            params['country'] = event.country.lower()
        if event.to_dict().get('file_key'):

            params['url'] = ui_link_for('record_file', id=event.recid,