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

By default support replaying tracking requests to both piwik.php and matomo.php. #235

Merged
merged 2 commits into from
Dec 1, 2018
Merged
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
29 changes: 22 additions & 7 deletions import_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ def _create_parser(self):
help="This URL will be used to send API requests (use it if your tracker URL differs from UI/API url), "
"eg. http://other-example.com/matomo/ or http://analytics-api.example.net",
)
option_parser.add_option(
'--tracker-endpoint-path', dest='matomo_tracker_endpoint_path', default='/piwik.php',
help="The tracker endpoint path to use when tracking. Defaults to /piwik.php."
)
option_parser.add_option(
'--dry-run', dest='dry_run',
action='store_true', default=False,
Expand Down Expand Up @@ -691,9 +695,9 @@ def _create_parser(self):
help="Replay piwik.php requests found in custom logs (only piwik.php requests expected). \nSee https://matomo.org/faq/how-to/faq_17033/"
)
option_parser.add_option(
'--replay-tracking-expected-tracker-file', dest='replay_tracking_expected_tracker_file', default='piwik.php',
help="The expected suffix for tracking request paths. Only logs whose paths end with this will be imported. Defaults "
"to 'piwik.php' so only requests to the piwik.php file will be imported."
'--replay-tracking-expected-tracker-file', dest='replay_tracking_expected_tracker_file', default=None,
help="The expected suffix for tracking request paths. Only logs whose paths end with this will be imported. By default "
"requests to the piwik.php file or the matomo.php file will be imported."
)
option_parser.add_option(
'--output', dest='output',
Expand Down Expand Up @@ -1454,7 +1458,7 @@ def _call(path, args, headers=None, url=None, data=None):

if auth_user is not None:
base64string = base64.encodestring('%s:%s' % (auth_user, auth_password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header("Authorization", "Basic %s" % base64string)

# Use non-default SSL context if invalid certificates shall be
# accepted.
Expand Down Expand Up @@ -1953,7 +1957,7 @@ def _record_hits(self, hits):
args['debug'] = '1'

response = matomo.call(
'/piwik.php', args=args,
config.options.matomo_tracker_endpoint_path, args=args,
expected_content=None,
headers={'Content-type': 'application/json'},
data=data,
Expand Down Expand Up @@ -2522,8 +2526,8 @@ def filtered_line(line, reason):

if config.options.replay_tracking:
# we need a query string and we only consider requests with piwik.php
if not hit.query_string or not hit.path.lower().endswith(config.options.replay_tracking_expected_tracker_file):
invalid_line(line, 'no query string, or ' + hit.path.lower() + ' does not end with piwik.php')
if not hit.query_string or not self.is_hit_for_tracker(hit):
invalid_line(line, 'no query string, or ' + hit.path.lower() + ' does not end with piwik.php/matomo.php')
continue

query_arguments = urlparse.parse_qs(hit.query_string)
Expand Down Expand Up @@ -2552,6 +2556,17 @@ def filtered_line(line, reason):
if len(hits) > 0:
Recorder.add_hits(hits)

def is_hit_for_tracker(self, hit):
filesToCheck = ['piwik.php', 'matomo.php']
if config.options.replay_tracking_expected_tracker_file:
filesToCheck = [config.options.replay_tracking_expected_tracker_file]

lowerPath = hit.path.lower()
for file in filesToCheck:
if lowerPath.endswith(file):
return True
return False

def _add_custom_vars_from_regex_groups(self, hit, format, groups, is_page_var):
for group_name, custom_var_name in groups.iteritems():
if group_name in format.get_all():
Expand Down